$(document).ready(function() {

    $('ul.menu a').collapsor();

    // Highslide Gallery
    if (typeof hs != 'undefined') {
        hs.lang = {
            loadingText :     'Caricamento...',
            loadingTitle :    'Caricamento',
            focusTitle :      'Clicca per chiudere l\'immagine',
            fullExpandTitle : 'Dimensioni originali',
            fullExpandText :  'Dimensioni originali',
            previousText :    'Precedente',
            previousTitle :   'Precedente',
            nextText :        'Successivo',
            nextTitle :       'Successivo',
            moveTitle :       'Sposta',
            moveText :        'Sposta',
            closeText :       'Chiudi',
            closeTitle :      'Chiudi',
            resizeTitle :     'Ridimensiona',
            playText :        'Play',
            playTitle :       'Play',
            pauseText :       'Pausa',
            pauseTitle :      'Pausa',
            number :          'Foto %1/%2',
            restoreTitle :    'Clicca per chiudere l\'immagine. Clicca e trascina per spostare. Usa le frecce per scorrere le immagini.'

        };
        hs.showCredits = false;
        hs.graphicsDir = '/javascript/highslide/graphics/';
        hs.align = 'center';
        hs.transitions = ['expand', 'crossfade'];
        hs.outlineType = 'rounded-white';
        hs.fadeInOut = true;
        hs.numberPosition = 'caption';
        hs.dimmingOpacity = 0.75;

        // Add the controlbar
        if (hs.addSlideshow) hs.addSlideshow({
            //slideshowGroup: 'group1',
            interval: 5000,
            repeat: false,
            useControls: true,
            fixedControls: 'fit',
            overlayOptions: {
                opacity: .75,
                position: 'bottom center',
                hideOnMouseOut: true
            }
        });
    }

    $('div.hidden-photos').hide();

    // Flowplayer

    if (typeof flowplayer == 'function') {
        if ($('#player').length > 0) {
            flowplayer("player",{
                src: '/flash/flowplayer-3.1.1.swf',
                wmode: 'opaque'
            },

            {
                clip: {
                    autoPlay: false,
                    autoBuffering: true
                },
                plugins: {
                    controls: {
                        url: '/flash/flowplayer.controls-tube-3.1.1.swf'
                    }
                }
            });
        }
    }

    // display the hidden gallery
    $('a#open-gallery').click(function(event){
        event.preventDefault();
        $('a#thumb1').trigger('click');
    });

    // Filter option/form In the Products list page (frontend)
    $('select#filter').change(function(){
        $('#filterForm').submit();
    });

    // Search function/box home page (frontend)
    $('select#search-select').change(function(){
        $('#search-form').submit();
    });

    // News cycled through cycle plugin
    if (typeof $('#box_news').cycle == 'function') {
        $('#box_news').cycle({
            fx:    'fade',
            speed:  2500
        });
    }

    // Product page (frontend)
    $('div#player-container a').css({width:'425px', height: '300px'});
    $('div#player-container').hide();
    $('#open-video').toggle(function(event) {
        event.preventDefault();
        $('div#player-container').show('slow');
    },
    function(event){
        event.preventDefault();
        $('div#player-container').hide('slow');
    }
    );

    // Product page order form (hide it)
    $('div#order-form').hide();
    
    $('a#product-order').click(function(event){
        event.preventDefault();
        $('div.boxdettaglio').hide();
        $('div.statica h3').hide();
        $('div#order-form').show('slow');
        $('div#order-form h3').show('slow');
    });

    if ($('div.errorSummary').length > 0 && $('div#order-form').length > 0) {
        $('a#product-order').trigger('click');
    }

    $('a#show-product').click(function(event){
        event.preventDefault();
        $('div#order-form').hide();
        $('div.statica h3').show('slow');
        $('div.boxdettaglio').show('slow');
    });

    // Product And news or Stage page pdf (open pdf in a new window)
    $('a#pdf').click(function (event) {
        event.preventDefault();
        window.open($(this).attr('href'));
    });

    $('a.pdflink').attr('target', '_blank');
});
