//NUMERO DELLA VOCE DEL MENU DA APRIRE (impostata su ogni pagina)
var num_display = 0;
//ID DELLA VOCE DEL SOTTOMENU DA APRIRE (impostata su ogni pagina)
var sottomenu;
window.addEvent('domready', function() {

        $('accordion').setStyle('position', 'fixed');
        $('menu_fx').setStyle('position', 'fixed');
        $('top_menu').setStyle('position', 'fixed');

        //EVENTI AL CLICK DEL MENU
        var location_div;
        var chiudi = 0;
        $$('.azi, .pro, .tec, .gal, .new, .con').addEvent('click',function(){
            location_div = this.getParent('.cont_title').getNext('.group_section').getElement('.file').get('href');
            window.location = location_div;
            //lo imposto a 1 per poi verificare se e stato premuto sul menu
            chiudi = 1;
        });
        
        //MENU     
        var arrow_act = new Element('div',{'class': 'freccia'});
        var classe_act,classe_spl;
        new Fx.Accordion($('accordion'), '.section', '.group_section',{ display: num_display,
                onActive: function(toggler, el){
                    try{
                       var eleme = el.getElement('#'+sottomenu);
                    }catch(err){}             
                        //imposto la classe _act al menu .section attivo
                        classe_act = toggler.className.split(' ')[1]+'_act';
                        toggler.addClass(classe_act);
                        //inserisco il div contenente la freccetta dentro il div .select selezionato
                        arrow_act.inject(toggler.getParent('.cont_title'), 'bottom');
                        //el contiene il div .group_section
                        var elem = el.getElements('.file');
                        //al cambio pagina non fa aprire due volte il menu
                        if(chiudi==1){
                            $$(elem).each(function(e){
                                e.destroy(); //elimina i .file cosi non si apre
                            });
                        }
                        //per ogni sottomenu applico effetti
                        $$(elem).each(function(element){
                            //new Fx.Tween(element, {duration: 1000,transition: 'quad:Out', property: 'letter-spacing'}).start('80','0');

                            if(eleme){
                                eleme.setStyle('color','#2eafe4')
                                new Fx.Tween(eleme, {duration: 1000,transition: 'quad:Out', property: 'margin-left'}).start('-30','7');
                            }/*else{
                                new Fx.Tween(element, {duration: 1000,transition: 'quad:Out', property: 'margin-left'}).start('-30','0');
                            }*/

                            //imposto gli eventi del mouse nei sottomenu
                            var click = false;
                            element.addEvents({
                                click: function(){
                                    click = true;
                                    //per sicurezza
                                    if(element.getStyle('margin-left').toInt()==0){
                                        new Fx.Tween(element, {duration: 200,transition: 'linear', property: 'margin-left'}).start(0,7);
                                    }
                                    element.setStyle('color', '#2eafe4');
                                },
                                mouseover: function(){
                                    if(!click && element.get('id')!=sottomenu) {
                                        new Fx.Tween(element, {duration: 140,transition: 'linear', property: 'margin-left'}).start(0,7);
                                    }
                                    element.setStyle('color', '#2eafe4');
                                },
                                mouseleave: function(){
                                    if(!click && element.get('id')!=sottomenu) {
                                        new Fx.Tween(element, {duration: 540,transition: 'bounce:Out', property: 'margin-left'}).start(7,0);
                                        element.setStyle('color', '#585756');
                                    }
                                }
                            });
                        });
                },
                //onBackground scorre tutti i div .section tranne quello attivo
                onBackground: function(toggler, el){
                        classe_spl = toggler.get('class').split(' ')[2];
                        if(toggler.hasClass(classe_spl)) toggler.removeClass(classe_spl);
                }
        });//FINE ACCORDION

         //MENU GENERALE AJAX
        $$('#menu_fx a.cms').each(function(element,index) {
                if(element.get('href').test('^http://')){
                    element.set('target','_new');
                    element.addEvent('click', function(e){
                        try{
                            e.stopPropagation();
                        }catch(err){}
                    });
                } else {
                    if(element.get('href')!='#'){
                        element.addEvent('click', function(e){
                            try{
                                e.stop();
                            }catch(err){}
                            callLoadPage(element);
                        });
                    }
                }
        });
        //EVENTI DEL MENU ACCORDION SENZA SOTTOMENU
        $$('.hom, .bre, .dow').addEvent('click',function(){
            var cl_link = this.className.split(' ')[1];
            if (cl_link=='hom') window.location = 'index.jsp';
            if (cl_link=='bre') window.location = 'brevetti.jsp';
            if (cl_link=='dow') window.location = 'download.jsp';
        });
});

    function callLoadPage(element){
        var urlToLoad = element.get('href');

        myRE1 = new RegExp('^http://', "i");
        myRE2 = new RegExp('^<%=httpRoot%>', "i");

        if(element.get('href').toLowerCase().match('.pdf$')){
            atk_winPdf(element.get('href'));
        }else if(element.get('href').match(myRE1)){
            if(element.get('href').match(myRE2)){
                var sez = urlToLoad.substring(urlToLoad.lastIndexOf("/")+1,urlToLoad.lastIndexOf("."));
                try{
                    loadPage(element.get('href'));
                }catch(err){}
            }else{
                atk_winPdf(element.get('href'));
            }
        }else{
            var sez = urlToLoad.substring(urlToLoad.lastIndexOf("/"),urlToLoad.lastIndexOf("."));
            try{
                   loadPage(element.get('href'));
            }catch(err){}
        }
    }

    function loadPage(urlToLoad){
        var req = new Request.HTML({
            method: 'post',
            url: urlToLoad,
            onRequest: function() {
            },
            update: $('corpo_centrale'),
            onComplete: function(response) {
            },
            onError: function(response) {
            }
        }).send();
    }




