function getLang() {
    if (document.body.className == 'ita')
        return 'ita';
    else if (document.body.className == 'eng')
        return 'eng';
    else if (document.body.className == 'deu')
        return 'deu';
}

function MyElasticFunction(p) {
   return (Math.pow(10,10*(p-1))*Math.cos(20*p*Math.PI*1/2));
};
function MyElasticFunction2(p) {
   return (Math.pow(10,8*(p-1))*Math.cos(10*p*Math.PI*1/1));
};
var myTransitionElastic2 = new Fx.Transition(MyElasticFunction2);
var myTransitionSfondo = new Fx.Transition(MyElasticFunction);
var myDurationSfondo = 4000;
var myTransition = new Fx.Transition(Fx.Transitions.Elastic);
var myDuration = 1000;
var myTransitionExpo = new Fx.Transition(Fx.Transitions.Expo);
var myDurationExpo = 500;

var shakeDuration = 120;
var shakeMode = 'horizontal';
var shakeTimes = 3;

var urlSfondo = 'img/menu_main/sfondo_menu.png';
var urlSfondoActive = 'img/menu_main/sfondo_menu_active.png';

var widthEnterSfondoVillaggio = 255;

var widthSfondo = 174;
var topSfondo = 0;
var leftSfondo = 0;

var topEnter = 0;
var topLeave = 0;
if (navigator.userAgent.match(/MSIE/gi)) { /* IE */
	var widthEnter = 50;
	var widthLeave = 40;
	var leftEnter = 17;
	var leftLeave = 22;
	
	var widthEnter_top = 46;
	var widthLeave_top = 40;
	var widthEnter_topFondazione = 98;
	var widthLeave_topFondazione = 90;
} else {
	var widthEnter = 50;
	var widthLeave = 40;
	var leftEnter = 17;
	var leftLeave = 22;
	
	var widthEnter_top = 46;
	var widthLeave_top = 40;
	var widthEnter_topFondazione = 98;
	var widthLeave_topFondazione = 90;
}

var indiceButton = 0;
var elementi = new Array();
var numButtons = 0;

var MenuMain = new Class({
	initialize: function(elements){
		this.elements = elements;

		elementi = this.elements;
		numButtons = this.elements.length;

		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();

				switch (i) {
					case 0:topEnter=17;topLeave=22;break;
					case 1:topEnter=9;topLeave=14;break;
					case 2:topEnter=16;topLeave=21;break;
					case 3:topEnter=10;topLeave=15;break;
					case 4:topEnter=18;topLeave=23;break;
					default:topEnter=0;topLeave=0;
				}

				if (el.hasClass('active')) {
				} else {
					el.getElement('h1').set('morph',{duration:myDuration,transition:myTransition.easeOut});
					el.getElement('h1').morph({'top':topEnter,'left':leftEnter});
					el.getElement('h1 img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
					el.getElement('h1 img').morph({'width':widthEnter});
				};
			});
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
					el.getElement('h1').set('morph',{duration:myDuration,transition:myTransition.easeOut});
					el.getElement('h1').morph({'top':topLeave,'left':leftLeave});
					el.getElement('h1 img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
					el.getElement('h1 img').morph({'width':widthLeave});
				};
			});
			el.addEvent('click', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
					var shake = new Fx.Shake(el.getElement('h1 img'), {duration:shakeDuration,mode:shakeMode,times:shakeTimes});
					shake.start();
				} else {
					showHint('main.php?page='+i+'&lang='+getLang());
					resetMenuTop('all');
					removeActiveFooter('all');
					resetMenuMain(i);
					if (i<4) setActiveFooter(i);
				};
			});
		});
	}
})

var indiceButton_top = 0;
var elementi_top = new Array();
var numButtons_top = 0;

var MenuTop = new Class({
	initialize: function(elements){
		this.elements = elements;

		elementi_top = this.elements;
		numButtons_top = this.elements.length;

		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
					if (el.getElement('h1').hasClass('fondazione')) {
						el.getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
						el.getElement('img').morph({'width':widthEnter_topFondazione});
					} else {
						el.getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
						el.getElement('img').morph({'width':widthEnter_top});
					};
				};
			});
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
					if (el.getElement('h1').hasClass('fondazione')) {
						el.getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
						el.getElement('img').morph({'width':widthLeave_topFondazione});
					} else {
						el.getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
						el.getElement('img').morph({'width':widthLeave_top});
					};
				};
			});
			el.addEvent('click', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
					var shake = new Fx.Shake(el.getElement('img'), {duration:shakeDuration,mode:shakeMode,times:shakeTimes});
					shake.start();
				} else {
					var index = i+5;
					if (index == 11) {
						//showHint('main.php?page=4');
						if (getLang()=='ita') popup_normalmode('img/mappa_'+getLang()+'.jpg');
						//resetMenuMain(4);
					} else {
						resetMenuMain('all');
						removeActiveFooter('all');
						showHint('main.php?page='+index+'&lang='+getLang());
						resetMenuTop(i);
					}
				};
			});
		});
	}
})

function resetMenuMain(i){
	var indice = i;
	var menu = $$('#menu_main li');
	if (indice == 'all') {
		menu.removeClass('active');
		for (var j=0;j<5;j++) {
			switch (j) {
				case 0:topLeave=22;break;
				case 1:topLeave=14;break;
				case 2:topLeave=21;break;
				case 3:topLeave=15;break;
				case 4:topLeave=23;break;
				default:topLeave=0;
			}
			menu[j].getElement('h1').set('morph',{duration:myDuration,transition:myTransition.easeOut});
			menu[j].getElement('h1').morph({'top':topLeave,'left':leftLeave});
			menu[j].getElement('h1 img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
			menu[j].getElement('h1 img').morph({'width':widthLeave});
			menu[j].getElement('img.sfondo').setProperty('src',urlSfondo);
		}
	} else {
		resetMenuMain('all');
		menu[indice].addClass('active');
		menu[indice].getElement('img.sfondo').setProperty('src',urlSfondoActive);
		indiceButton = indice;
	}
}

function resetMenuTop(i){
	var indice = i;
	var menu = $$('#menu_top li');
	if (indice == 'all') {
		menu.removeClass('active');
		for (var j=0;j<8;j++) {
			if (j==7) {
				menu[j].getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
				menu[j].getElement('img').morph({'width':widthLeave_topFondazione});
			} else {
				menu[j].getElement('img').set('morph',{duration:myDuration,transition:myTransition.easeOut});
				menu[j].getElement('img').morph({'width':widthLeave_top});
			}
		}		
	} else {
		resetMenuTop('all');
		menu[indice].addClass('active');
		indiceButton_top = indice;
	}
}

function resetMenuVillaggio(i){
	var indice = i;
	var menu = $$('#menu_villaggio li');
	menu.each(function(el,i) {
		if (i == indice) {
			menu[i].addClass('active');
		} else {
			menu[i].getElement('img.sfondo').set('morph',{duration:myDurationExpo,transition:myTransitionExpo.easeOut});
			menu[i].getElement('img.sfondo').morph({'width':0});
			menu[i].removeClass('active');
		}
	});
	indiceButton_villaggio = indice;
}

function setActiveFooter(i){
	var indice = i;
	var footer = $$('#footer span.left a');
	removeActiveFooter('all');
	footer[indice].addClass('active');
	if (indice<13 && indice>5) indiceButton_top = indice;
	else indiceButton = indice;
}

function removeActiveFooter(i){
	var indice = i;
	var footer = $$('#footer span.left a');
	if (indice == 'all') {
		footer.removeClass('active');
	} else {
		footer[indice].removeClass('active');
	}
}


var indiceButton_villaggio = 0;
var elementi_villaggio = new Array();
var numButtons_villaggio = 0;

var MenuVillaggio = new Class({
	initialize: function(elements){
		this.elements = elements;

		elementi_villaggio = this.elements;
		numButtons_villaggio = this.elements.length;

		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
					el.getElement('img.sfondo').set('morph',{duration:myDuration,transition:myTransitionElastic2.easeOut});
					el.getElement('img.sfondo').morph({'width':widthEnterSfondoVillaggio});
				};
			});
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
					el.getElement('img.sfondo').set('morph',{duration:myDurationExpo,transition:myTransitionExpo.easeOut});
					el.getElement('img.sfondo').morph({'width':0});
				};
			});
			el.addEvent('click', function(e){
				new Event(e).stop();
				resetMenuVillaggio(i);
				showHint_2('villaggio_right.php?id='+i+'&lang='+getLang(),"villaggio_right");
			});
		});
	}
})
/*
var thumbs = new Array();
var numThumbs = 0;
var thumb = 0;

var ThumbVillaggio = new Class({
	initialize: function(elements){
		this.elements = elements;

		thumbs = this.elements;
		numThumbs = this.elements.length;

		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				if (el.hasClass('activeThumb')) {
				} else {
					el.getElement('img').set('morph',{duration:myDurationExpo,transition:myTransitionExpo.easeOut});
					el.getElement('img').morph({'width':60});
				};
			});
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				if (el.hasClass('activeThumb')) {
				} else {
					el.getElement('img').set('morph',{duration:myDurationExpo,transition:myTransitionExpo.easeOut});
					el.getElement('img').morph({'width':45});
				};
			});
			el.addEvent('click', function(e){
				new Event(e).stop();
				resetThumbs(i);
				thumb = i;
			});
		});
	}
})

function resetThumbs(i){
	var indice = i;
	var menu = $$('#thumbsWrapper ul li');
	menu.each(function(el,i) {
		if (i == indice) {
			menu[i].addClass('activeThumb');
		} else {
			if (menu[i].hasClass('activeThumb')) menu[i].removeClass('activeThumb');
			menu[i].getElement('img').set('morph',{duration:myDurationExpo,transition:myTransitionExpo.easeOut});
			menu[i].getElement('img').morph({'width':45});
		}
	});
}
*/

function resetMenuVideo(i){
	var indice = i;
	var menu = $$('#menu_video li');
	menu.each(function(el,i) {
		if (i == indice) {
			menu[i].addClass('active');
		} else {
			menu[i].removeClass('active');
		}
	});
	indiceButton_video = indice;
}

var indiceButton_video = 0;
var elementi_video = new Array();
var numButtons_video = 0;

var MenuVideo = new Class({
	initialize: function(elements){
		this.elements = elements;

		elementi_video = this.elements;
		numButtons_video = this.elements.length;

		this.elements.each(function(el,i){
			el.addEvent('mouseenter', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
				};
			});
			el.addEvent('mouseleave', function(e){
				new Event(e).stop();
				if (el.hasClass('active')) {
				} else {
				};
			});
			el.addEvent('click', function(e){
				new Event(e).stop();
				resetMenuVideo(i);
				showHint_2('video_left.php?id='+i+'&lang='+getLang(),"video_left");
			});
		});
	}
})
