window.addEvent('domready', function() {

	
	//------------------------------------------------------------
    //FAQ: Gestione accordion pagina faq
    //------------------------------------------------------------

    var containers = $$('.question');       // array contenitori
    var togglers = $$('.question h3');      // array togglers
    var hasDefault = $$('.question.opened-q').length > 0; // verifico se esiste una tab default da aprire

    // preparazione oggetti
    containers.each(function(el, index) {

        var t = el.getFirst('h3');

        // calcolo altezze elementi
        var initHeight = 0;
        el.store('init', initHeight = t.getSize().y);
        el.store('final', el.getStyle('height').toInt());

        // imposto stili base, primo aperto
        el.setStyle('overflow', 'hidden');
        if (index == 0 && !hasDefault) {
            el.store('open', 1);
        }
        else if (el.hasClass('opened-q')) {
            el.store('open', 1);
        }
        else {
            el.setStyle('height', initHeight);
            el.store('open', 0);
        }

        // preparazione effetti
        el.set('tween', { duration: 700 });

    });

    // gestione click dei togglers
    togglers.addEvent('click', function() {

        var p = this.getParent();
        var closed = Number(p.retrieve('open')) <= 0;

        // porto i container in altezza iniziale
        containers.each(function(c) {
            c.store('open', 0);
            c.tween('height', c.retrieve('init'));
        });

        // se chiuso, apro il corrente
        if (closed) {
            p.tween('height', p.retrieve('final'));
            p.store('open', 1);
        }

    });

    //------------------------------------------------------------
    //INFO: Mostro caratteristiche o descrizione del prodotto
    //------------------------------------------------------------
    $$('div.info ul li a').addEvent('click', function(e) {
        // Fermo la propagazione dell'evento click base
        e.stop();
		
        // Nascondiamo gli elementi div discendenti diretti di div.info
        $$('div.info > div').setStyle('display', 'none');
        // Verifico che esista il # dell'ancora
        if (this.href.indexOf('#') >= 0) {
            // Ricavo l'id del box dall'ancora
            var boxid = this.href.split('#').length >= 2 ? this.href.split('#')[1] : '';
            // Verifico che il box esista nel DOM
            if ($(boxid)) {
                // Mostro l'elemento che corrisponde all'ancora
                $(boxid).setStyle('display', 'block');
                // Eliminiamo la classe current a tutti i li
                $$('div.info ul li').removeClass('current');
                // Assegniamo la classe al li corrente (padre di a)
                this.getParent().addClass('current');
				force_realign() ;
            }
        }
    });

    //------------------------------------------------------------
    //NAV: Mostro e nascondo il sotto-menù di NAV
    //------------------------------------------------------------
    if ($('nav')) {
        $$('#nav > ul > li').addEvent('mouseenter', function() {
            this.addClass('selected');
            // Mostro il submenu
            if (this.getFirst('ul')) {
                this.getFirst('ul').setStyle('visibility', 'visible');
            }
        }).addEvent('mouseleave', function() {
            this.removeClass('selected');
            // Nascondo il submenu
            if (this.getFirst('ul')) {
                this.getFirst('ul').setStyle('visibility', 'hidden');
            }
        });
    }

    //------------------------------------------------------------
    //NAV SUB: Nascondo e mostro il sotto-menù interno alle pagine
    //------------------------------------------------------------
    if ($('sub_nav')) {

        // Se esistono elementi focused devo 
        if ($$('div#sub_nav div.focused').length > 0) {
            $('sub_nav').setStyle('height', 0);
            $$('#sub_nav ul').setStyle('opacity', 0);
        }

        var offsetul = 6;

        // Se esistono elementi focused devo impostare l'altezza di sub_nav e l'opacity dei menu
        if ($$('div#sub_nav div.focused').length > 0) {
            $('sub_nav').setStyle('height', 0);
            $$('#sub_nav ul').setStyle('opacity', 0);
        }

        // Misurazione altezza elementi
        $$('div#sub_nav div ul').setStyle('display', 'block');
        $$('div#sub_nav div ul').each(function(el) {
            el.store('hel', el.getStyle('height').toInt() + el.getStyle('padding-top').toInt() - offsetul);
        });
        $$('div#sub_nav div ul').setStyle('display', 'none');
        $$('div#sub_nav div.current ul', 'div#sub_nav div.focused ul').setStyle('display', 'block');

        // Click tasto "Visualizza/nasconsi prodotti"
        $$('#sub_nav h2').addEvent('click', function() {
            if ($('sub_nav').getStyle('height').toInt() <= 0) {
                var current = $$('#sub_nav div.current ul')[0] ? $$('#sub_nav div.current ul')[0] : $$('#sub_nav div.focused ul')[0];
                $('sub_nav').set('morph', {
                    onComplete: function() {
                        if ($$('#sub_nav div.focused ul')[0]) {
                            $$('#sub_nav div.focused ul')[0].setStyle('display', 'block');
                        }
                        $$('#sub_nav ul').tween('opacity', 1);
                        $('sub_nav').removeClass('closed');
                    }
                });
                var hel = current.retrieve('hel');
                $('sub_nav').morph({ height: hel });
            }
            else {

                $('sub_nav').set('morph', { onComplete: function() { } })
                $('sub_nav').morph({ height: 0 });
                $$('#sub_nav ul').tween('opacity', 0);
                $('sub_nav').addClass('closed');
            }
        });

        //------------------------------------------------------------
        //NAV SUB: Mostro lista sotto-categorie o lista-marche del prodotto
        //------------------------------------------------------------
        $$('div#sub_nav div h3').addEvent('click', function(e) {

            // Se è già corrente non eseguire nulla
            if (this.getParent().hasClass('current') || this.getParent().hasClass('focused')) {
                return;
            }

            // Nascondiamo tutti i menu
            $$('div#sub_nav div ul').setStyle('display', 'none');

            // Prendo il menu corrente da mostrare e lo mostro ma avrà opacity 0
            var nMenu = this.getNext('ul');
            nMenu.setStyle('display', 'block');

            // Rimuovo classi current e focused
            $$('div#sub_nav div').removeClass('current');
            $$('div#sub_nav div').removeClass('focused');

            // Segnalo il menu corrente
            this.getParent().addClass('current');

            // Calcolo l'altezza dell'elemento interno per aprire sub_nav
            var hel = nMenu.retrieve('hel');

            // Se sub_nav è chiuso allora morpho l'apertura
            if ($('sub_nav').getStyle('height').toInt() <= 0) {
                $('sub_nav').set('morph', {
                    onComplete: function() {
                        // al completamento mostro anche il menù
                        $$('#sub_nav ul').tween('opacity', 1);
                        $('sub_nav').removeClass('closed');
                    }
                });
                $('sub_nav').morph({ height: hel });
            }
            else {
                // Se sub_nav già aperto mostro semplicemente l'altezza giusta
                $('sub_nav').setStyle('height', hel);
            }
        });

    } // END: if($('sub_nav'))

	 
	 
 
    // Chiusura del DOMREADY
});

 //------------------------------------------------------------
//INPUT: Campi di testo che si cancellano al :focus
//------------------------------------------------------------
function pulisciCampo(thefield) { if (thefield.defaultValue == thefield.value) thefield.value = ""; }
function riempiCampo(thefield) { if (thefield.value == "") thefield.value = thefield.defaultValue; }


