$(document).ready(function(){
		
	//VARIABILI
	var sezione = "home";
	var selezionato = null;
	var easing = 'easeInOutSine';
	var distanza = 15;
	var vel_vfast = 100;
	var vel_fast = 400;
	var vel_tween = 700;
	var vel_slow = 1000;
	var glamocracy_status = "false";

    /*jQuery.fx.interval = 50;
	(function($) {
		$.fn.customFadeIn = function(speed, callback) {
			if (jQuery.browser.msie) {
				$(this).show();
				$(this).animate({"color": "#000000"}, function() {
					if(callback != undefined)
						callback();
				});
			} else {
				$(this).fadeIn(speed, function() {
					console.log("finito fadeIn")
					if(callback != undefined)
						callback();
				});
			}
		};
		$.fn.customFadeOut = function(speed, callback) {
			if (jQuery.browser.msie) {
				$(this).animate({"color": "#ffffff"}, function() {
					$(this).hide();
					if(callback != undefined)
						callback();
				});
			} else {
				$(this).fadeOut(speed, function() {
					console.log("finito fadeOut")
					if(callback != undefined)
						callback();
				});
			}
		};
	})(jQuery);*/

	jQuery.fx.interval = 50;
	(function($) {
		$.fn.customFadeIn = function(speed, callback) {
			if (jQuery.browser.msie) {
				$(this).css("background","#ffffff");
				$(this).fadeIn(speed, function() {
					$(this).css("background","");
					if ($(this).css("opacity") <= 1) {
						$(this).css("opacity", 1);
					};
					if(callback != undefined)
						callback();
				});
			} else {
				$(this).fadeIn(speed, function() {
					if ($(this).css("opacity") <= 1) {
						$(this).css("opacity", 1);
					};
					if(callback != undefined)
						callback();
				});
			}
		};
		$.fn.customFadeOut = function(speed, callback) {
			if (jQuery.browser.msie) {
				$(this).css("background","#ffffff");
				$(this).fadeOut(speed, function() {
					$(this).css("background","");
					if(callback != undefined)
						callback();
				});
			} else {
				$(this).fadeOut(speed, function() {
					if(callback != undefined)
						callback();
				});
			}
		};
	})(jQuery);


	
	



	//MEMORIZZO TUTTE LE POSIZIONI NECESSARIE PER GLI SPOSTAMENTI
	var posizioni = null;
	function aggiornaPosizioni() {
		centro = {
			"left": $(window).width()/2,
			"top": $(window).height()/2
		}; //serve a indicare il centro attoro al quale si disporrà il logo dopo aver cliccato su una barra
		posizioni = {
		"home": {
			"centro": { // Il centro attorno a cui si disporra il logo
		 		"left":$(window).width()/2,
				"top":$(window).height()/2,
				"dl":0,
				"dt":0,
				"on_fade":1,
				"off_fade":1
			},
			"top_bar": {
				"left": - ($('#top_bar').width()/2),
				"top": - ($('#left_bar').height()/2)
			},
			"right_bar": {
				"left": + ( $('#top_bar').width()/2)-($('#right_bar').width()),
				"top": - ($('#right_bar').height()/2)
			},
			"bottom_bar": {
				"left": - ($('#bottom_bar').width()/2),
				"top": + ($('#left_bar').height()/2) - ($('#bottom_bar').height())
			},
			"left_bar": {
				"left": - ($('#top_bar').width()/2),
				"top": - ($('#left_bar').height()/2)
			},
			"top_voice": {
				"left":100,
				"top":-60
			},
			"right_voice": {
				"left":70,
				"top":90
			},
			"bottom_voice": {
				"left":-30,
				"top":70
			},
			"left_voice": {
				"left":-130,
				"top":30
			}
		},
		"left": { //---Sezione LIVE
			"centro": { // Il centro attorno a cui si disporra il logo
				"left":$(window).width()/2 + 250,
				"top":$(window).height()/2 + 50,
				"dl":-distanza,
				"dt":0,
				"on_fade":1,
				"off_fade":0.5
			},
			"voice": {
				"left":$(window).width()/2-30,
				"top":$(window).height()/2-60,
				"source":[1.05, 1.05, 0, 0],
				"target":[-0.3, 0.1, 0, 0]
			},
			"title": {
				"left":$(window).width()/2-30,
				"top":$(window).height()/2-200
			},
			"text": {
				"left":$(window).width()/2-400,
				"top":$(window).height()/2+50
			},
			"subtitle": {
				"left":$(window).width()/2-400,
				"top":$(window).height()/2+30
			},
			"menu1": {
				"left_start":parseInt($('#left_voice').css('left'))-60,
				"top_start":parseInt($('#left_voice').css('top')),
				"left_end":$(window).width()/2-200,
				"top_end":$(window).height()/2-30,
				"source":[-0.05, 0.5, 0, 0],
				"target":[1, 0, 0, 0],
				"left_noact3":$(window).width()/2+10,
				"top_noact3":$(window).height()/2+10,
				"left_noact2":$(window).width()/2-110,
				"top_noact2":$(window).height()/2+15
			},
			"menu2": {
				"left_start":parseInt($('#left_voice').css('left'))-40,
				"top_start":parseInt($('#left_voice').css('top'))+40,
				"left_end":$(window).width()/2-140,
				"top_end":$(window).height()/2+15,
				"source":[-0.05, 0.5, 0, 0],
				"target":[1, 0, 0, 0],
				"left_noact3":$(window).width()/2-10,
				"top_noact3":$(window).height()/2+20,
				"left_noact1":$(window).width()/2-220,
				"top_noact1":$(window).height()/2-120
			},
			"menu3": {
				"left_start":parseInt($('#left_voice').css('left')),
				"top_start":parseInt($('#left_voice').css('top'))+20,
				"left_end":$(window).width()/2-20,
				"top_end":$(window).height()/2+20,
				"source":[-0.05, 0.5, 0, 0],
				"target":[0, 0, 0, 0],
				"left_noact2":$(window).width()/2-270,
				"top_noact2":$(window).height()/2-60,
				"left_noact1":$(window).width()/2-220,
				"top_noact1":$(window).height()/2-120
			}
		}, //---Chiusura LIVE
		"bottom": { //---Sezione FEEL
			"centro": { // Il centro attorno a cui si disporra il logo
				"left":$(window).width()/2 - 350,
				"top":$(window).height()/2 + 100,
				"dl":0,
				"dt":distanza,
				"on_fade":1,
				"off_fade":0.5
			},
			"voice": {
				"left":$(window).width()/2-430,
				"top":$(window).height()/2-50,
				"source":[0.5, 1.2, 0, 0],
				"target":[0.5, -0.2, 0, 0]
			},
			"title": {
				"left":$(window).width()/2-430,
				"top":$(window).height()/2-200
			},
			"text": {
				"left":$(window).width()/2-100,
				"top":$(window).height()/2-50
			},
			"subtitle": {
				"left":$(window).width()/2-100,
				"top":$(window).height()/2-70
			},
			"menu1": {
				"left_start":parseInt($('#bottom_voice').css('left'))+$('#bottom_voice').width()+10,
				"top_start":parseInt($('#bottom_voice').css('top'))+20,
				"left_end":$(window).width()/2-200,
				"top_end":$(window).height()/2,
				"source":[1.1, 0.5, 0, 0],
				"target":[-0.1, 0.5, 0, 0],
				"left_noact3":$(window).width()/2-250,
				"top_noact3":$(window).height()/2-150,
				"left_noact2":$(window).width()/2-200,
				"top_noact2":$(window).height()/2-100
			},
			"menu2": {
				"left_start":parseInt($('#bottom_voice').css('left'))+$('#bottom_voice').width()+10,
				"top_start":parseInt($('#bottom_voice').css('top')),
				"left_end":$(window).width()/2-180,
				"top_end":$(window).height()/2-50,
				"source":[1.1, 0.5, 0, 0],
				"target":[-0.1, 0.5, 0, 0],
				"left_noact3":$(window).width()/2-220,
				"top_noact3":$(window).height()/2-100,
				"left_noact1":$(window).width()/2-240,
				"top_noact1":$(window).height()/2+10
			},
			"menu3": {
				"left_start":parseInt($('#bottom_voice').css('left'))+$('#bottom_voice').width()+10,
				"top_start":parseInt($('#bottom_voice').css('top'))-20,
				"left_end":$(window).width()/2-200,
				"top_end":$(window).height()/2-90,
				"source":[1.1, 0.5, 0, 0],
				"target":[-0.1, 0.5, 0, 0],
				"left_noact2":$(window).width()/2-220,
				"top_noact2":$(window).height()/2,
				"left_noact1":$(window).width()/2-320,
				"top_noact1":$(window).height()/2-10
			}
		},//---Chiusura FEEL
		"right": { //---Sezione THINK
			"centro": { // Il centro attorno a cui si disporra il logo
				"left":$(window).width()/2 - 150,
				"top":$(window).height()/2 - 110,
				"dl":distanza,
				"dt":0,
				"on_fade":1,
				"off_fade":0.5
			},
			"voice": {
				"left":$(window).width()/2,
				"top":$(window).height()/2-100,
				"source":[-0.1, 0.5, 0, 0],
				"target":[1.3, 0.2, 0, 0]
			},
			"title": {
				"left":$(window).width()/2,
				"top":$(window).height()/2-190
			},
			"text": {
				"left":$(window).width()/2,
				"top":$(window).height()/2+10
			},
			"subtitle": {
				"left":$(window).width()/2,
				"top":$(window).height()/2-10
			},
			"menu1": {
				"left_start":parseInt($('#right_voice').css('left'))+$('#right_voice').width(),
				"top_start":parseInt($('#right_voice').css('top'))+15,
				"left_end":$(window).width()/2+60,
				"top_end":$(window).height()/2-30,
				"source":[1, 1.1, 0, 0],
				"target":[1, 0, 0, 0],
				"left_noact2":$(window).width()/2+120,
				"top_noact2":$(window).height()/2-30,
				"left_noact3":$(window).width()/2+170,
				"top_noact3":$(window).height()/2-50
			},
			"menu2": {
				"left_start":parseInt($('#right_voice').css('left'))+$('#right_voice').width(),
				"top_start":parseInt($('#right_voice').css('top')),
				"left_end":$(window).width()/2+140,
				"top_end":$(window).height()/2-40,
				"source":[1, 1.15, 0, 0],
				"target":[0, 0, 0, 0],
				"left_noact1":$(window).width()/2+120,
				"top_noact1":$(window).height()/2-30,
				"left_noact3":$(window).width()/2+190,
				"top_noact3":$(window).height()/2-60
			},
			"menu3": {
				"left_start":parseInt($('#right_voice').css('left'))+$('#right_voice').width(),
				"top_start":parseInt($('#right_voice').css('top'))-15,
				"left_end":$(window).width()/2+180,
				"top_end":$(window).height()/2-70,
				"source":[1, 1.1, 0, 0],
				"target":[0, 0, 0, 0],
				"left_noact1":$(window).width()/2+110,
				"top_noact1":$(window).height()/2-40,
				"left_noact2":$(window).width()/2+180,
				"top_noact2":$(window).height()/2-60
			}
		},//---Chiusura THINK
		"top": { //---Sezione SHARE
			"centro": { // Il centro attorno a cui si disporra il logo
				"left":$(window).width()/2 -370,
				"top":$(window).height()/2+50,
				"dl":0,
				"dt":-distanza,
				"on_fade":1,
				"off_fade":0.5
			},
			"voice": { 
				"left":$(window).width()/2-270,
				"top":$(window).height()/2-80,
				"source":[0, 0, 0, 0],
				"target":[0.9, 1.3, 0, 0]
			},
			"title": {
				"left":$(window).width()/2-450,
				"top":$(window).height()/2-200
			},
			"text": { //Il testo principale
				"left":$(window).width()/2-20,
				"top":$(window).height()/2-100
			},
			"subtitle": { //Il subemnu cliccato
				"left":$(window).width()/2-20,
				"top":$(window).height()/2-122
			},
			"menu1": {
				"left_start":parseInt($('#top_voice').css('left'))+$('#top_voice').width()+30,
				"top_start":parseInt($('#top_voice').css('top'))+$('#top_voice').height(),
				"left_end":$(window).width()/2-80,
				"top_end":$(window).height()/2,
				"source":[1, 1, 0, 0],
				"target":[-0.1, 0, 0, 0],
				"left_noact3":$(window).width()/2-150, 
				"top_noact3":$(window).height()/2+40,
				"left_noact2":$(window).width()/2-100,
				"top_noact2":$(window).height()/2+10
			},
			"menu2": {
				"left_start":parseInt($('#top_voice').css('left'))+$('#top_voice').width(),
				"top_start":parseInt($('#top_voice').css('top'))+$('#top_voice').height()+20,
				"left_end":$(window).width()/2-150,
				"top_end":$(window).height()/2+30,
				"source":[1, 1, 0, 0],
				"target":[-0.1, 0, 0, 0],
				"left_noact3":$(window).width()/2-110,
				"top_noact3":$(window).height()/2-10,
				"left_noact1":$(window).width()/2-90,
				"top_noact1":$(window).height()/2-150
			},
			"menu3": {
				"left_start":parseInt($('#top_voice').css('left'))+$('#top_voice').width()-50,
				"top_start":parseInt($('#top_voice').css('top'))+$('#top_voice').height(),
				"left_end":$(window).width()/2-240,
				"top_end":$(window).height()/2+10,
				"source":[1, 1, 0, 0],
				"target":[-0.1, 0, 0, 0],
				"left_noact1":$(window).width()/2-170,
				"top_noact1":$(window).height()/2+50,
				"left_noact2":$(window).width()/2-50,
				"top_noact2":$(window).height()/2+20
			}
		},//---Chiusura SHARE
		"contatti":{
			"centro": {
				"left":$(window).width()/2-200,
				"top":$(window).height()/2,
				"dl":0,
				"dt":0,
				"on_fade":1,
				"off_fade":1
			},
			"title": {
				"left":$(window).width()/2,
				"top":$(window).height()/2-200,
				"source":[0, 0, 0, 0],
				"target":[0.9, 1.3, 0, 0]
			},
			"text": { //Il testo principale
				"left":$(window).width()/2,
				"top":$(window).height()/2-170
			}
		},//---Chiusura CONTATTI
		"clienti":{
			"centro": {
				"left":$(window).width()/2-200,
				"top":$(window).height()/2,
				"dl":0,
				"dt":0,
				"on_fade":1,
				"off_fade":1
			},
			"title": {
				"left":$(window).width()/2,
				"top":$(window).height()/2-200,
				"source":[0, 0, 0, 0],
				"target":[0.9, 1.3, 0, 0]
			},
			"text": { //Il testo principale
				"left":$(window).width()/2,
				"top":$(window).height()/2-170
			}
		}//---Chiusura CONTATTI
		}//chiusura object posizioni
	}//chiusura function aggiornaPosizioni()
	aggiornaPosizioni()

	

	//POSIZIONI INIZIALI DEL QUADRATO E DELLE VOCI
	$('.menu_bar').each(function() {
		$(this).css({
			left:centro.left + posizioni[sezione][this.getAttribute('id')].left,
			top:centro.top + posizioni[sezione][this.getAttribute('id')].top,
			display:"none"
		})
		.fadeIn(vel_tween, function() {
			if ($(this).css("opacity") <= 1) {
				$(this).css("opacity", 1);
			};
		});
	});


	//FUNZIONE CHE AVVIA IL SITO AL CLICK
	function init() {
		$('.menu_voice').each(function() {
			$(this).css({
				left:centro.left + posizioni[sezione][this.getAttribute('id').replace('_voice','_bar')].left +  posizioni.home[$(this).attr('id')].left,
				top:centro.top + posizioni[sezione][this.getAttribute('id').replace('_voice','_bar')].top + posizioni.home[$(this).attr('id')].top,
				display:"none"
			})
			.customFadeIn(vel_tween, function() {	
			});
		});
		$(document).mousemove(mouse_move);
		$("#footer").fadeIn(vel_tween);
		$("#menu_head").fadeIn(vel_tween);
		stato = "home";
	}
	$(document).click(function() {
		if (stato == "pre")
		{
			init();
		}
	});
		

	//CLICK SULLE BARRE DEL QUADRATO
	var clicked = false;
	$('.menu_bar').click(function() {
			if (stato != "pre") {
			if (clicked == false) {
				clicked = true;
				sezione = this.getAttribute('id').replace('_bar', '');
				on_click();
			}
		}
	});
	$('.menu_bar').dblclick(function() {
		return false;
	});


	//CLICK SULLE VOCI DEL QUADRATO
	$('.menu_voice').click(function() {
		if (clicked == false) {
			clicked = true;
			sezione = this.getAttribute('id').replace('_voice', '');
			on_click();
		}
	});
	$('.menu_voice').dblclick(function() {
		return false;
	});
	function on_click() {
		if (glamocracy_status == "true") {
			$("#immagine_sfondo").css({
				"display":"none"
			})
			glamocracy_status == "false";
		};
		aggiornaPosizioni();
		if (sezione == "top") {
			creaSlide();
		}
		$("div:animated").stop();
		$('.moving').each(function() {
			$(this).removeClass('moving')
		});
		if ($('#main_text').css('display') != 'none')
		{
			$('#main_text').fadeOut(vel_tween);
		};
		titolo();
		jsPlumb.reset();
		spegni_menu();
		stato = 'transizione';
		$(document).unbind("mousemove");
		centro.left = posizioni[sezione].centro.left;
		centro.top = posizioni[sezione].centro.top;
		centraLogo();
		attiva_foot();
	};


	//FUNZIONE CHE ACCENDE E SPEGNE LE VOCI DEL FOOTER E DELL'HEADER
	function attiva_foot() {
		$('.foot').removeClass('f_selected')
		$('.head').removeClass('h_selected')
		$('#'+sezione+'_button').addClass('f_selected')
		$('#'+sezione+'_button').animate({
			'left': 6+'px',
			'top': -20+'px'
			}, 500, false, 'easeOutCirc');
		$('.foot_bg', $('#'+sezione+'_button')).fadeTo(500,1);
		$('.foot').not($('#'+sezione+'_button')).animate({
			'left': 0+'px',
			'top': 0+'px'
			}, 500, false, 'easeOutCirc');
		$('.foot_bg', $('.foot').not($('#'+sezione+'_button'))).fadeTo(500,0.3);
		$('.head').not($("#"+sezione)).animate({
			'left': -6+'px',
			'top': -22+'px'
		}, 500, false, 'easeOutCirc');
		$('.head_bg', $('.head').not($('#'+sezione))).fadeTo(500,0.3);
	};


	//FUNZIONE CHE CENTRA IL QUADRATO IN BASE AL CLICK
	function centraLogo() {
		$('.menu_bar').each(function(index){
			if ($(this).attr('id') == sezione+'_bar') {
				$(this)
				.stop()
				.fadeTo(vel_vfast, posizioni[sezione].centro.on_fade)
				.animate({
					left:centro.left + posizioni.home[$(this).attr('id')].left+posizioni[sezione].centro.dl+'px',
					top:centro.top + posizioni.home[$(this).attr('id')].top+posizioni[sezione].centro.dt+'px'
				}, vel_tween, easing)
			} else {
				$(this)
				.stop()
				.fadeTo(vel_vfast,posizioni[sezione].centro.off_fade)
				.animate({
					left:centro.left + posizioni.home[$(this).attr('id')].left+'px',
					top:centro.top + posizioni.home[$(this).attr('id')].top+'px'
				}, vel_tween, easing)
			}
		});
		$('.menu_voice').each(function() {
			if ($(this).attr('id') != sezione+'_voice') {
				$(this).stop().customFadeOut(vel_tween);
			} else {
				$(this)
				.stop()
				.animate({
					left:posizioni[sezione].voice.left+'px', 
					top:posizioni[sezione].voice.top+'px',
					fontSize:'15px'
				}, 	{duration: vel_fast, easing: easing, complete:function() {
						//menus()
					}
				})
				.customFadeIn(vel_tween, function() {
					/*if ($(this).css("opacity") <= 1) {
						$(this).css("opacity", 1);
					};*/
				})
			};
		});
	};


	//GESTIONE DEL MOUSE-MOVE
	function mouse_move(e){
		var parX = $(window).width()/400;
		var parY = $(window).height()/225;
		var x_mouse = e.pageX;
		var y_mouse = e.pageY;
		if(stato == 'home') {
			$('.menu_bar').each(function(k){
				var  divWidth = $(this).width();
				var  divHeight = $(this).height();
				var finX = x_mouse - divWidth/2;
				var finY = y_mouse - divHeight/2;
				var destX = centro.left+posizioni.home[this.getAttribute('id')].left + ((centro.left-divWidth/2) - finX)/((k+1)*parX);
				var destY = centro.top+posizioni.home[this.getAttribute('id')].top + ((centro.top-divHeight/2) - finY)/((k+1)*parY);
				$(this).css({left:destX,top:destY});	
			});
			$('.menu_voice').each(function(k){
				var  divWidth_voice = $(this).width();
				var  divHeight_voice = $(this).height();
				var finX_voice = x_mouse - divWidth_voice/2;
				var finY_voice = y_mouse - divHeight_voice/2;
				var destX_voice = centro.left + posizioni.home[this.getAttribute('id').replace('_voice','_bar')].left +  posizioni.home[$(this).attr('id')].left  + ((centro.left-divWidth_voice/2) - finX_voice)/((k+1)*(parX*0.8));
				var destY_voice = centro.top + posizioni.home[this.getAttribute('id').replace('_voice','_bar')].top + posizioni.home[$(this).attr('id')].top + ((centro.top-divHeight_voice/2) - finY_voice)/((k+1)*(parY*0.8));
				$(this).css({left:destX_voice,top:destY_voice});				
			});
		}
		else if (stato == 'primo_livello')
		{
			jsPlumb.repaintEverything(sezione+'_voice');
			$('#'+sezione+'_voice').css({
				left:posizioni[sezione].voice.left-(x_mouse - ($(window).width()/2))/(20),
				top:posizioni[sezione].voice.top-(y_mouse - ($(window).height()/2))/(20)
			});
			$('.moving').each(function(k){
				var numero = parseInt($(this).attr('id').replace('submenu', ''));
			$(this).css({
				left:posizioni[sezione]['menu'+numero].left_end-(x_mouse - ($(window).width()/2))/(10*numero),
				top:posizioni[sezione]['menu'+numero].top_end-(y_mouse - ($(window).height()/2))/(10*numero)
				});	
			});
		} else if (stato == 'secondo_livello'){
			if ($('#'+sezione+'_voice').hasClass('moving')) {
				jsPlumb.repaintEverything(sezione+'_voice');
			}
			$('#'+sezione+'_voice').css({
				left:posizioni[sezione].voice.left-(x_mouse - ($(window).width()/2))/(20),
				top:posizioni[sezione].voice.top-(y_mouse - ($(window).height()/2))/(20)
			});

			$('.submenu.moving').each(function(k){
				var numero = parseInt($(this).attr('id').replace('submenu', ''));
			
					$(this).css({
						left:posizioni[sezione]['menu'+selezionato.replace('submenu', '')]['left_noact'+parseInt($(this).attr('id').replace('submenu', ''))]-(x_mouse - ($(window).width()/2))/(10*numero),
						top:posizioni[sezione]['menu'+selezionato.replace('submenu', '')]['top_noact'+parseInt($(this).attr('id').replace('submenu', ''))]-(y_mouse - ($(window).height()/2))/(10*numero)
					});	
				
			});
		};
	};


	//FUNZIONE CHE CAMBIA IL TESTO DEL TITOLO
	function titolo() {
		$('#title_text')
		.customFadeOut(1000, function() {
			$('#title_text')
			.css({
				left:posizioni[sezione].title.left+'px',
				top:posizioni[sezione].title.top+'px'
			})//close css
			.html ($("#contenuti "+file+' #'+sezione+' #title').html())
			.stop()
			.customFadeIn(1000, function() {
				/*if ($('#title_text').css("opacity") <= 1) {
					$('#title_text').css("opacity", 1);
				};*/
			});
			clicked = false;
		});//close fadeout
	};//close titolo


	//FUNZIONE CHE CAMBIA IL TESTO PRINCIPALE
	function testo() {	
		$('#main_text')
			.css({
				left:posizioni[sezione].text.left+'px',
				top:posizioni[sezione].text.top+'px'
			})
			.customFadeOut(vel_fast, function() {
				$('#main_text').html ($("#contenuti "+loaded).html());
				$('#slider_wrap').css('width', $('.immagine_button').length*($('.immagine_button').outerWidth())+'px');
				if ($('.immagine_button').length > 3) {
					$('#r_arrow').addClass('active');
					$('#l_arrow').addClass('unactive');
				}else{
					$('#r_arrow').addClass('unactive');
					$('#l_arrow').addClass('unactive');
				};
				$('#main_text').customFadeIn(vel_fast, function() {
					
				});
			});
	};


	//FUNZIONE CHE CARICA LE VOCI DEI SOTTOMENU
	function menus() {
		if (sezione == "contatti" || sezione == "clienti") {
			return;
		};
		aggiornaPosizioni();
		$('.submenu').each(function(index) {
			var k = index+1
			$(this).css({
				left: posizioni[sezione]['menu'+k].left_start+'px',
				top: posizioni[sezione]['menu'+k].top_start+'px',
				fontWeight:'normal'
			})
			.html ($("#contenuti "+file+' #'+sezione+' #item'+k+' #menu').html());
			if (k == 3) {
				move_menus();
			}
		});//close each
	};//close menus function


	function spegni_menu() {
		$('.submenu').each(function() {
			$(this).customFadeOut(vel_tween);
		});
		setTimeout(menus, 1000);
	};


	//FUNZIONE CHE ANIMA LE VOCI DEI SOTTOMENU
	function move_menus() {
		jsPlumb.reset();
		$('.submenu').each(function(index) {
			var k = index+1
			$(this).customFadeIn(vel_tween, function() {
				/*if ($(this).css("opacity") <= 1) {
					$(this).css("opacity", 1);
				};*/
			});
			$(this).addClass("moving");
			$(this).animate({
				left:posizioni[sezione]['menu'+k].left_end+'px',
				top:posizioni[sezione]['menu'+k].top_end+'px'
			}, {duration: vel_tween, easing: easing, complete: function() {
				stato = 'primo_livello';
				jsPlumb.connect({ 
				source:sezione+'_voice',
				target:'submenu'+k, 
				paintStyle:{ 
					lineWidth:1,
					strokeStyle:"#666666"
					},
					anchors:[posizioni[sezione]['menu'+k].source, posizioni[sezione]['menu'+k].target],
					connector: "Straight",
					endpoint: ["Dot", {radius:1}]
					});
				}
			});//close animate
			$(this).click(function(){
				loaded = file+' #'+sezione+' #item'+k+' #text';
				selectedSub($(this).attr('id'));
				if ($(this).text() != "Glamocracy") {
					testo();
				} else {
					if ($('#main_text').css('display') != 'none')
					{
						$('#main_text').css({
							"display":"none"
						});
					};
					$("#immagine_sfondo").stop().fadeIn(vel_fast, function() {
						if ($(this).css("opacity") <= 1) {
							$(this).css("opacity", 1);
						};
					});
					glamocracy_status = "true";
				}
			})// chiude click
		});
		setTimeout(function() {
			$(document).mousemove(mouse_move)
		}, 1200);
	};


	//FUNZIONE SELEZIONE VOCI SOTTOMENU
	function selectedSub(cliccato) {
		if (glamocracy_status == "true") {
			$("#immagine_sfondo").css({
				"display":"none"
			})
			glamocracy_status == "false";
		};
		qu = 0;
		$(document).unbind("mousemove");
		$("div:animated").stop();
		selezionato = cliccato;
		if ($('#title_text').css('display') != 'none')
		{
			$('#title_text').customFadeOut(vel_tween);
		};
		$('.submenu').each(function() {
			$(this).removeClass('moving');
			if ($(this).attr('id') != cliccato) {
				$(this)
				.css({
					fontWeight:'normal'
				})
				jsPlumb.animate(
				$(this),
					{
						left:posizioni[sezione]['menu'+cliccato.replace('submenu', '')]['left_noact'+parseInt($(this).attr('id').replace('submenu', ''))]+'px',
						top:posizioni[sezione]['menu'+cliccato.replace('submenu', '')]['top_noact'+parseInt($(this).attr('id').replace('submenu', ''))]+'px'	
					}, {duration: vel_tween, easing: easing, complete:function() {
						$(this).addClass('moving');
						$('#'+sezione+'_voice').addClass('moving');
						stato = 'secondo_livello';
						$(document).mousemove(mouse_move);
					}}
				);
			} else {
				$(this).css({
					fontWeight:'bold'
				})
				jsPlumb.animate(
					$('#'+cliccato),
					{
						left:posizioni[sezione].subtitle.left+'px',
						top:posizioni[sezione].subtitle.top+'px'
					}, {duration: vel_tween, easing: easing}
				);			
			};
		})
	};


	//SEZIONE RELATIVA AL FOOTER
	//FOOTER MOUSEOVER MOUSEOUT
	$('.foot').hover(
		function() {
			if ($(this).is('.f_selected') == false) {
				$(this).stop().animate({
					'left': 6+'px',
					'top': -20+'px'
				}, 500, false, 'easeOutCirc');
				$('.foot_bg', $(this)).stop().fadeTo(500,1);
			}
		},
		function() {
			if ($(this).is('.f_selected') == false) {
				$(this).stop().animate({
					'left': 0+'px',
					'top': 0+'px'
				}, 500, false, 'easeOutCirc');
				$('.foot_bg', $(this)).stop().fadeTo(500,0.3);
			}
		}
	);
	//FOOTER CLICK
	$('.foot').click(function() {
		sezione = $(this).attr('id').replace('_button', '');
		on_click();
	});


	//SEZIONE RELATIVA ALL'HEADER
	$('.head').each(function() {
		$(this).html ($("#contenuti "+file+' #head '+'#'+$(this).attr('id')+'_b').html());
	});
	//HEADER HOVER
	$('.head').hover(
		function() {
			if ($(this).is('.h_selected') == false) {
				$(this).stop().animate({
					'left': 0+'px',
					'top': 0+'px'
				}, 500, false, 'easeOutCirc');
				$('.head_bg', $(this)).stop().fadeTo(500,1);
			}
		},
		function() {
			if ($(this).is('.h_selected') == false) {
				$(this).stop().animate({
					'left': -6+'px',
					'top': -22+'px'
				}, 500, false, 'easeOutCirc');
				$('.head_bg', $(this)).stop().fadeTo(500,0.3);
			}
		}
	);
	//HEADER CLICK
	$('.head').click(function() {
		sezione = $(this).attr('id');
		on_click();
		head();
		$(this).addClass('h_selected');
	});
	function head() {
		loaded = file+' #'+sezione+'_tot';
		$('.menu_voice').customFadeOut(vel_fast);
		$('#main_text').fadeOut(vel_slow, function() {
			testo();
		})
	};


	//SLIDESHOW INTERNI AL TESTO
	$('.immagine_button').live('click', function() {
		$('#interno_text').html ($("#contenuti "+file+' #'+$(this).attr('id')+'_text').html());
	});
	var qu = 0;
	function creaSlide() {
		$('.slider_arrow').live('click', function() {
			var loghi_width  = $('#main_text .immagine_button').outerWidth(); /* larghezza singolo logo bordo incluso */
			var steps = 3; /* di quanti loghi si avanza al click */
			var loghi_totali = $('#main_text .immagine_button').length; /* loghi totali */
			var able_steps = (Math.floor(loghi_totali/steps)); //* pezzettoni completi */
			var loghi_extra = loghi_totali - (able_steps*steps); /* loghi spariggi */
			if($(this).attr('id') == 'l_arrow') {
				if (qu > 0 && qu != able_steps ) {
				$('#slider_wrap').animate({left: '+='+loghi_width*steps+'px'}, 500);
				qu--
				} else if (qu == able_steps) {
					$('#slider_wrap').animate({left: '+='+loghi_width*loghi_extra+'px'},500);
					qu--
				}
				if (qu == 0 && $(this).hasClass('active')) {
					$(this).addClass('unactive');
					$(this).removeClass('active');
					$(this).removeClass('hover');
				} 
				if( $('#r_arrow').hasClass('unactive') ) {
					$('#r_arrow')
					.removeClass('unactive')
					.addClass('active')
				};
			}
			if($(this).attr('id') == 'r_arrow') {
				if( qu < able_steps-1) {
					$('#slider_wrap').animate({left: '-='+loghi_width*steps+'px'},500);
					qu++
				}else if( qu == able_steps-1 && loghi_extra != 0 ) {
					$('#slider_wrap').animate({left: '-='+loghi_width*loghi_extra+'px'},500);
					qu++
				}
				if( $('#l_arrow').hasClass('unactive') ) {
					$('#l_arrow')
						.removeClass('unactive')
						.addClass('active')
				};
				if (qu == able_steps && $(this).hasClass('active') || loghi_extra == 0) {
					$(this).addClass('unactive');
					$(this).removeClass('active');
					$(this).removeClass('hover');
				}
			}
	});
	$('.slider_arrow').live('mouseover mouseout', function(event) {
		if( event.type == 'mouseover') {
			if ($(this).hasClass('active') ) {
				$(this).addClass('hover');
			}else{
				$(this).removeClass('hover');
			}
		}else if( event.type == 'mouseout') {
			$(this).removeClass('hover');
		}
	});
	};


	//GLAMOCRACY
	$("#immagine_sfondo").click(function() {
		glamocracy();
	});
	$('#immagine_sfondo').mouseenter(function() {
			$('#immagine_sfondo > img')
			.css({
				"display":"block"
			})
	});
	$('#immagine_sfondo').mouseleave(function() {
			$('#immagine_sfondo > img')
			.css({
					"display":"none"
			});
	});
	function glamocracy() {
		$("#glamocracy")
		.fadeIn(vel_tween, function() {
			if ($(this).css("opacity") <= 1) {
				$(this).css("opacity", 1);
			};
		});
	};
	$("#close").click(function() {
		$("#glamocracy").fadeOut();
	});
	$("#glamocracy").click(function() {
		$("#glamocracy").fadeOut();
	});
	function resize_glamocracy() {
		var w = $(window).width();
		var h = $(window).height();
		if (h >= 687) {
			$("#glamocracy")
			.css({
				width: w+"px",
				height: h+"px",
				top: "0",
				left: "0"
			});
			$("#g_frame")
			.css({
				width:"850px",
				height:"685px",
				top:"50%",
				left:"50%",
				margin:"-340px 0 0 -425px"
			});
			$("#g_img").css({
				width:"800px",
				height:"637px",
				margin:"0 25px"
			});
			$("#close").css({
				margin:"5px 0 5px 830px"
			});
		} else {
			$("#glamocracy")
			.css({
				width: w+"px",
				height: h+"px",
				top: "0",
				left: "0"
			});
			$("#g_frame")
			.css({
				height:h+"px",
				width:((h*850)/685)+60+"px",
				top:"50%",
				left:"50%",
				margin:"-"+h/2+"px 0 0 -"+((h*850)/687)/2+"px"
			});
			$("#g_img")
			.css({
				height:(h-50)+"px",
				width:(h*800)/637+"px"
			});
			$("#close")
			.css({
				margin:"5px 0 5px "+(40+((h*850)/685))+"px"
			});
		}
	}
	resize_glamocracy()


	//RESIZING DELLA PAGINA
	function resizing() {
		aggiornaPosizioni();
		$('.menu_bar').each(function(index){
			if ($(this).attr('id') == sezione+'_bar') {
				$(this).css({
					left:posizioni[sezione].centro.left + posizioni.home[$(this).attr('id')].left+posizioni[sezione].centro.dl+'px',
					top:posizioni[sezione].centro.top + posizioni.home[$(this).attr('id')].top+posizioni[sezione].centro.dt+'px'
				})
			} else {
				
				$(this).css({
					left:posizioni[sezione].centro.left + posizioni.home[$(this).attr('id')].left+'px',
					top:posizioni[sezione].centro.top + posizioni.home[$(this).attr('id')].top+'px'
				})
			}
		});
		if (sezione == "home") {
			$('.menu_voice').each(function(index){
					$(this).css({
						left:centro.left + posizioni[sezione][$(this).attr('id').replace('_voice','_bar')].left +  posizioni.home[$(this).attr('id')].left,
						top:centro.top + posizioni[sezione][$(this).attr('id').replace('_voice','_bar')].top + posizioni.home[$(this).attr('id')].top
					})
			});
		} else {
			$('.menu_voice').each(function() {
				if ($(this).attr('id') == sezione+'_voice') {
					$(this).css({
							left:posizioni[sezione].voice.left+'px', 
							top:posizioni[sezione].voice.top+'px'
					})
					}
				})
				$('#title_text').css({
					left:posizioni[sezione].title.left+'px',
					top:posizioni[sezione].title.top+'px'
				});
				$('#main_text').css({
					left:posizioni[sezione].text.left+'px',
					top:posizioni[sezione].text.top+'px'
				});
				if (!selezionato) {
					$('.submenu').each(function(index) {
						var k = index+1;
						$(this).css({
								left:posizioni[sezione]['menu'+k].left_end+'px',
								top:posizioni[sezione]['menu'+k].top_end+'px'
							});
					});
				} else {
					$('.submenu').each(function(index) {
						if ($(this).attr('id') != selezionato) {
							$(this).css({
								left:posizioni[sezione]['menu'+selezionato.replace('submenu', '')]['left_noact'+parseInt($(this).attr('id').replace('submenu', ''))]+'px',
								top:posizioni[sezione]['menu'+selezionato.replace('submenu', '')]['top_noact'+parseInt($(this).attr('id').replace('submenu', ''))]+'px'
							})
						} else {
							$(this).css({
								left:posizioni[sezione].subtitle.left+'px',
								top:posizioni[sezione].subtitle.top+'px'
							})
						}
					})
				}
			jsPlumb.repaintEverything(sezione+'_voice');
		};
		resize_glamocracy();
	};
	$(window).resize(resizing);
	
	// ATTIVA SEZIONE DA LINK
	if (sez == "left" || sez == "right" || sez=="top" || sez == "bottom") {
		sezione = sez;
		on_click();
		$("#footer").fadeIn(vel_tween);
		$("#menu_head").fadeIn(vel_tween);
		
	} else if (sez == "clienti" || sez == "contatti") {
		sezione = sez;
		on_click();
		$("#footer").fadeIn(vel_tween);
		$("#menu_head").fadeIn(vel_tween);
		head();
		$("#"+sezione).addClass('h_selected');
		$("#"+sezione).stop().animate({
			'left': 0+'px',
			'top': 0+'px'
		}, 500, false, 'easeOutCirc');
		$('.head_bg', $("#"+sezione)).stop().fadeTo(500,1);
	;} else if (stato != "pre") {
		init();
	};


}); // IMPORTANTE ----> CHIUSURA READY -----> TUTTO PRIMA DI QUESTO
