var scroller = null;
var fxScroll;
var onScroll = 0;

var currPosition;
var currFont;

// Paremtri font
var minFont = 0.65;
var maxFont = 0.9;

// Tempi effetti
var timeOpenLang = 500;
var timeScrollTab = 1000;
var timeDragMenu = 800;
var timeFadeTopbox = 600;

//
var preContainerTab = 'containerTab';

// Parametri cookies
var cookieMenu_Name = 'menuPosition';
var cookieMenu_Duration = 365; // days
var cookieFont_Name = 'fontSize';
var cookieFont_Duration = 365; // days

// Funzioen che ridimensiona il menù
function checkColumnsHeight(force) {
	var altezzaContent = $('content').getStyle('height').toInt();
	var altezzaMenu = $('absoluteMenu').getStyle('height').toInt();
	
	if (force == 1) { altezzaMenu = 0; }
	
	if (altezzaMenu < altezzaContent) {
		$('absoluteMenu').setStyle('height', altezzaContent-30);
		$('backmenu').setStyle('height', altezzaContent-319);
	}
	else $('backmenu').setStyle('height', altezzaMenu-269);
}

window.addEvent('load', function() {
	
	$$('div.element').each(function(el){
			el.set('oHeight', el.getStyle('height').toInt());
		});
	$$('div.element').setStyle('height', 0);
	$$('h1.caption').addEvent('click', function(){
		$$('div.element').tween('height', 0);
		var next = this.getNext('div');
		next.tween('height', next.get('oHeight'));
		})

// Chiusura del LOAD
});


window.addEvent('domready', function() {
	
	/////////////////////////////////////////////////// CONTROLLO DIMENSIONE FONT INIZIALE
	currFont = Cookie.get(cookieFont_Name);
	if (currFont == null)
        currFont = '0.65';
	$(document.body).setStyle('font-size',currFont+'em');
	// Aggiorno il cookie
	Cookie.set(cookieFont_Name, currFont, {duration: cookieFont_Duration, path: '/' });
	
	//////////////////////////////////////////////////// CONTROLLO POSIZIONE INIZIALE MENU
	currPosition = Cookie.get(cookieMenu_Name);
	if (currPosition == 'dx')
	{
		$('menu').setStyle('margin-left',725);
		$('container').setStyle('padding-left',0);
		$('topboxContainer').setStyle('margin-left',0);
		$('footer').setStyle('left',0);
	}
	else currPosition = 'sx';
	// Aggiorno il cookie
	Cookie.set(cookieMenu_Name, currPosition, {duration: cookieMenu_Duration, path: '/' });
	
	//////////////////////////////////////////////////////////// CONTROLLO ALTEZZA COLONNE
    $('topmenu').setStyle('opacity', '0.8');
	checkColumnsHeight();

	////////////////////////////////////////////////////// MOTORE PER IL MENU DELLE LINGUE
	    
//    var altezzaLang = $('langhidden').getStyle('height').toInt();
//    $('langhidden').setStyle('height', '0px');
//    
//    $('langMenu').addEvent('mouseenter', function(){
//    	
//    	$('langhidden').set('tween', {duration: timeOpenLang});
//		$('langhidden').tween('height', altezzaLang+1);
//    	    
//    }).addEvent('mouseleave', function(){
//   		$('langhidden').tween('height', '0');
//    });
    
    ////////////////////////////// MOTORE LA GESTIONE DEI TIPS SU IMMAGINI, DIV ECC ECC...

	var lifeTips = new Tips($$('.tips'), {
	    initialize:function(){
		    this.toolTip.setStyle('opacity',0);
	    },
	    onShow: function(toolTip) {
		    toolTip.set('tween', {duration: 400});
            toolTip.tween('opacity', '1');
	    },
	    onHide: function(toolTip) {
		    toolTip.set('tween', {duration: 400});
            toolTip.tween('opacity', '0');
	    },
	    showDelay: 100
	});
    
    //////////////////////////////////////////////////// MOTORE PER LA GESTIONE DELLE TABS
    
	var tabs = $$('.tabsTitle');
	if (tabs.length > 0) { tabs[0].addClass('currenttabsTitle'); }
    
    if($('containerTabs') && $('containerBig'))
    {
        fxScroll = new Fx.Scroll('containerTabs', {
            onComplete: function(){
            	onScroll = 0;
            },
            duration: timeScrollTab,
            transition: Fx.Transitions.Back.easeInOut
        });
	
  		// Scrollo subito alla prima tab
      	fxScroll.set($('containerBig').getStyle('padding-left').toInt(),0);
    }
    
    /////////////////////////////////////////// MOTORE PER LA GESTIONE DEL MENU DRAGGABILE

    $('dragArea').addEvent('mousedown', function() {
    
    	$('menu').addEvent('mousemove', function(ev) {
        	ev = new Event(ev).stop();
      	
            switch (Browser.Engine.name) {
                case 'webkit': $('menu').onselectstart = function(){return false;}; break;
                case 'trident': $('menu').onselectstart = function(){return false;}; break;
                case 'gecko': $('menu').style.MozUserSelect = "none"; break;
            }

        	$('menu').removeEvents('mousemove');
        	
	        // Creo il ghost
	        var ghost;
	        if(currPosition == 'sx')
	        	ghost = new Element('div',{ 'class' : 'ghost-menu-dx' });
	       	else
	       		ghost = new Element('div',{ 'class' : 'ghost-menu-sx' });
			
			switch (Browser.Engine.name) {
                case 'webkit':  $(ghost).onselectstart = function(){return false;}; break;
                case 'trident':  $(ghost).onselectstart = function(){return false;}; break;
                case 'gecko':  $(ghost).style.MozUserSelect = "none"; break;
            }
			
	        $(ghost).setStyle('height',$('menu').getStyle('height'));
	        $(ghost).setStyle('opacity','0.2');
	        ghost.inject($('top'));
	        	        
			// Creo il clone
	        var clone;
	        if(currPosition == 'sx')
	        	clone = this.clone().setStyles($('menu').getCoordinates())
	        else clone = this.clone().setStyles($('menu').getCoordinates()).setStyle('margin-left','0px');

			if(clone)
			{	            
	            $(clone).setStyles({'opacity': 0.4, 'position': 'absolute'})
	            .addEvent('emptydrop', function() {
	                this.remove();
	                ghost.remove();
	                $('menu').removeEvents('mousemove');
	            }).inject(document.body);
			}
				
			// Creo il drop
			$(ghost).addEvents({
	            'drop': function() {
	                clone.remove();
	                $(ghost).setStyle('opacity','0.2');
	                
	                // sposto gli elementi
	                
	                $('menu').set('tween', {
	                	onComplete:function(){
	                		ghost.fade(0);
	                		switchPosition();
	                	},
	                	duration: timeDragMenu
	                });
	                
	                $('container').set('tween', {
	                	duration: timeDragMenu
	                });
	                $('topboxContainer').set('tween', {
	                	duration: timeDragMenu
	                });
	                
	                if(currPosition == 'sx') {
						$('menu').tween('margin-left', 725);
						$('container').tween('padding-left', 0);
						$('topboxContainer').tween('margin-left',0);
						$('footer').tween('left',0);
					}
					else {
						$('menu').tween('margin-left', 29);
						$('container').tween('padding-left', 281);
						$('topboxContainer').tween('margin-left',225);
						$('footer').tween('left',-56);
					}
					
	            },
	            'over': function() {
	                $(ghost).setStyle('opacity','0.4');
	            },
	            'leave': function() {
	                $(ghost).setStyle('opacity','0.2');
	            }
	        }); // itemToDrop.addEvents	
				
			// Avvio il drag
			var drag = clone.makeDraggable({
	            droppables: [ghost]
	        }); // this returns the dragged element
		    drag.start(ev); // start the event manual
	    });
	});
});

	function switchPosition(){
		if(currPosition == 'sx')
			currPosition = 'dx';
		else currPosition = 'sx';

		// Scrivo il cookie
		Cookie.set(cookieMenu_Name, currPosition, {duration: cookieMenu_Duration, path: '/' });
	}

    //////////////////////////////////////// FUNZIONE PER LA GESTIONE DEL CLICK DELLE TABS
	function scrollTabs(el,tab){
		if(onScroll == 0)
		{
			if($(preContainerTab+tab))
			{
				onScroll = 1;
				$$('.currenttabsTitle').removeClass('currenttabsTitle');
		    	fxScroll.toElement($(preContainerTab+tab));
		    	if($(el)) {	$(el).addClass('currenttabsTitle'); }
		    }	
		    else alert("Contenuti inesistenti!");
		}
	}
	
	////////////////////////////////////////////////// FUNZIONE PER LA GESTIONE DEL TOPBOX
	function showTopBox(contentID,linguaID){
		
            var qryStr = 'ContentID='+contentID+'&LinguaID='+linguaID

            var myRequest = new Request({
                onRequest: function(){
                    //showLoader();
                },
                onCancel: function(){
                    //hideLoader();
                },
                onSuccess: function(responseText, responseXML){
                    var result = 0
                    if (responseXML.getElementsByTagName("result").item(0))
                        result = responseXML.getElementsByTagName("result").item(0).firstChild.data;
                    if (result == 1)
                    {
                        var contentTitle = "";
                        var contentText = "";
                        
                        if (responseXML.getElementsByTagName("title").item(0))
                            contentTitle = responseXML.getElementsByTagName("title").item(0).firstChild.data;
                        if (responseXML.getElementsByTagName("text").item(0))
                            contentText = responseXML.getElementsByTagName("text").item(0).firstChild.data;
                        
                        $('topboxContainer').empty();
                        
                        var topboxTitle = new Element('div', { 'class': 'topboxTitle', 'html': contentTitle });
                        $(topboxTitle).inject($('topboxContainer'));
                        var topboxText = new Element('div', { 'html': contentText });
                        $(topboxText).inject($('topboxContainer'));
                        
                        $('topboxContainer').setStyle('opacity',0);
                        $('topboxContainer').setStyle('display','block');
                        $('topboxContainer').set('tween',{duration:timeFadeTopbox});
                        $('topboxContainer').tween('opacity','0.8');
                    }
                    else alert("Errore di comunicazione!");
                },
                onFailure: function(instance){
                    //hideLoader();
                    alert("Errore di comunicazione!");
                },
                method: 'get',
                url: '/source/getContent.asp'
            }).send(qryStr);
	}
	
	/////////////////////////////////////// FUNZIONE PER LA GESTIRE LA DIMENSIONE DEL FONT
	function changeFontSize(offset){
	    var single = 0.05;
	    currFont = parseFloat(currFont)+(offset*single);
	    if (currFont < minFont) { currFont = minFont }
	    if (currFont > maxFont) { currFont = maxFont }
	    currFont = roundNumber(currFont,2);
	    $(document.body).setStyle('font-size',currFont+'em');
	    // Scrivo il cookie
	    Cookie.set(cookieFont_Name, currFont, {duration: cookieFont_Duration, path: '/' });
	}
	
	function roundNumber(num, dec) {
	    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }