function debug(text) {
	$('#trace').append(text);
}

function debug2(text) {
	$('#trace').html(text);
}

// Configurazione

movement_active = false;
flag_open = false;
act_section = "";
next_section = "";

openSlideWidth = 900;


// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function evo_send_mail() {
	document.getElementById('modulo').action = '../java/inviamail.php'; 
	submit();	
}

function check_elements() {
	
	// Altezza container 
	$('#container').height($(window).height() - 70);

	// Immagine di sfondo
	$('#background').width($(window).width());

	// Pannelli vari
	$('.panel, .slider, .sliderpanel, .panel .cover, .panel .separatore').height($('#container').height());
	
	// Menu principale
	$('nav').css('top',($('#container').height() - $('nav').height()) / 2 + 50 + 'px');
	
	// Controlli slider
	$('.es-controls').css('top',$('#container').height()/2 + 25 + 'px');
	
	// Sfondo pannelli e slides
	originalH = $('.panel-background').height();
	originalW = $('.panel-background').width();
	newHeight = $('#container').height();
	newWidth = newHeight / originalH * originalW;
	
	if (newWidth < openSlideWidth) { newWidth = openSlideWidth; newHeight = newWidth / originalW * originalH; }
	
	$('.panel-background').height(newHeight).width(newHeight / originalH * originalW);
	
	$('.sliderpanel img').height(newHeight).width(newHeight / originalH * originalW);
	$('.sliderpanel img').css('left',0 - (newWidth - openSlideWidth)/2 + 'px');
	
	// Titoli pannelli
	$('.panel .titolo').css('top',$('#container').height() / 2 + 40 + "px");
	
	// Contenuto shop
	$('#shop').height($('#container').height());
	$('#shop-content').height($('#container').height() - 80 - 40 - 60);
	$('#shop').css('left',($(window).width() - 920)/2 + 'px');

	//$('.panel .shop-content-internal').css('top','40px').height($('#container').height() - 120);
	
	beltWidth = 0;
	
	$('.panel').each(function() {
		imgW = $(this).children('.panel-background').width();
		$(this).children('.panel-background').css('left',0 - (imgW - $(this).width())/2 + 'px');
		
		// Larghezza pannelli
		beltWidth = beltWidth + $(this).outerWidth(true);
		
	});
	
	// Verifica la larghezza schermo in confronto alla belt per stabilire se fermare o attivare il movimento
	
	
	if (beltWidth + 200 > $('#container').width() && !movement_active && !flag_open) {		
		evonavigator.startTimer(0); //en_timerTemp[0] = setInterval("evonavigator.tweenTo('0')",en_timing[0]);
		movement_active = true;
	}
	
	if (beltWidth + 200 <= $('#container').width() && !flag_open) {
		if (movement_active) {
			evonavigator.stopTimer(0);
			movement_active = false;
		}
		$('.belt').animate({'left':0},700,'easeInOutQuart');
	}
	
	// Riposiziona centralmente una sezione se aperta
	
	if (flag_open) {
		posx2 = (($('#container').width() - openSlideWidth) / 2) - $('#' + act_section).position().left;
		$('.belt').css('left',posx2 + 'px');
	}
	
	// Riposiziona il contenuto di una sezione
	
	yPos = $('#container').height() - 200;
	$('.open .content').css('top',yPos);	
	
}

// Apertura della sezione

function open_section(id_sec) {
	
	$('nav a').removeClass('sel');
	$('nav a[name="' + id_sec + '"]').addClass('sel');
	
	flag_open = true;
	
	if (!act_section) {
		
		$('#' + id_sec).addClass('open');
		$('#' + id_sec).removeClass('opening');
		
		// Pannello
		$('#' + id_sec).animate({'width':openSlideWidth},600,'easeInOutQuart',function() { show_content(id_sec); });
		margin = ($('#' + id_sec).children('.panel-background').width() - openSlideWidth) / 2;
		
		// Background
		$('#' + id_sec).children('.panel-background').stop().animate({'left':0 - margin},600,'easeInOutQuart');
		
		// Background slides
		$('#' + id_sec + ' .sliderpanel img').css('left',0 - margin + 'px');
		
		// Nasconde titolo e cover
		$('#' + id_sec).children('.titolo').animate({'opacity':0},500,function() { $(this).hide(); });
		$('#' + id_sec).children('.cover').animate({'opacity':0},0,function(){ $(this).hide(); });
		
		// Riposiziona centralmente la belt
		posx = (($('#container').width() - openSlideWidth) / 2) - $('#' + id_sec).position().left;
		$('.belt').animate({'left':posx},600,'easeInOutQuart');	
		
		// Imposta la sezione attuale
		act_section = id_sec;
		
		// Ferma il movimento
		if (movement_active) { evonavigator.stopTimer(0); movement_active = false; }
		
	}
	
	if (act_section && id_sec != act_section) {
		close_section(act_section);
		next_section = id_sec;
	}
	
}

// Chiusura della sezione

function close_section(id_sec) {
	
	$('#' + id_sec).removeClass('open');
	hide_content(id_sec);
	
	$('#' + id_sec).animate({'width':230},600,'easeInOutQuart',function() {
		
		$(this).removeClass('opening');
		$('#' + id_sec).children('.cover').show().animate({'opacity':0.5},500);
		$('#' + id_sec).children('.titolo').show().animate({'opacity':0.4},500);
		//$('#' + id_sec).children('.panel-background').animate({'opacity':0.75},500);
		act_section = "";
		
		if (next_section) { open_section(next_section); } 
		
		else { 
			$('nav a').removeClass('sel');
			flag_open = false;
			//movement_active = true;
			//if (beltWidth + 200 > $('#container').width()) { evonavigator.startTimer(0); movement_active = true; }
			check_elements();
		}
		
	});
	
	margin = ($('#' + id_sec).children('.panel-background').width() - 230) / 2;
	
	// Ferma lo slider di background	
	if (id_sec != 'shop' && id_sec != 'contatti') {
		
		evoslider.stopTimer('#slider-' + id_sec);
		
		// Se atelier ferma anche il secondo slider
		if (id_sec == 'atelier') { evoslider.stopTimer('#slider-atelier-salo'); }
		
		$('#' + id_sec).children('.panel-background').animate({'left':0 - margin,'opacity':1},600,'easeInOutQuart',function() {
									
			$('#' + id_sec + ' .slider').hide();	
			evoslider.slideTo('#slider-' + id_sec,0);	
			
			// Se atelier azzera anche il secondo slider
			if (id_sec == 'atelier') { evoslider.slideTo('#slider-atelier-salo',0); }
			
		});
		
		// Nasconde i controlli dello slider
		$('#' + id_sec + ' .es-controls').animate({'opacity':0},600,function() { $('#' + id_sec + ' .es-controls').hide(); });
	}
	
}

// === MOSTRA IL CONTENUTO DEL PANNELLO ===

function show_content(id_sec) {
	
	var elem = '#' + id_sec;
	
	// Imposta il primo menu selezionato
	$(elem + ' .sub-menu a').removeClass('sel');
	$(elem + ' .sub-menu a:eq(0)').addClass('sel');
	
	// Mostra l'header
	$(elem + ' header').animate({'opacity':1},600);
	$(elem + ' .close').animate({'opacity':1},600);
	
	// Mostra il primo content
	showSubContent(1);
	
	// Verifica gli elementi e carica lo slider
	check_elements();
	
	// Se si tratta di una sezione con slider di background lo attiva
	if (id_sec != 'contatti' && id_sec != 'shop') {
		$('#' + id_sec + ' .panel-background').animate({'opacity':0},0);
		
		// Doppio slider nella sezione ATELIER
		if (id_sec == 'atelier') {

			$('#slider-atelier').show();
			evoslider.setTimer('#slider-atelier');
			evoslider.setTimer('#slider-atelier-salo');
			
			$('#controls-riva').show();
			$('#controls-riva').animate({'opacity':1},600);
			
		} else {

			$('#' + id_sec + ' .slider').show();
			evoslider.setTimer('#slider-' + id_sec);
			
			// Show controls
			$('#' + id_sec + ' .es-controls').show();
			$('#' + id_sec + ' .es-controls').animate({'opacity':1},600);
		}
	
	}
	
	if (id_sec == 'shop') {
		$('#' + id_sec + ' .panel-background').hide();
		$('#' + id_sec + ' .shopcontent').css('opacity',1);
		
		//show_shop_cat();
		loadexternal('shop.php');
	}
}

// === NASCONDE IL CONTENUTO DEL PANELLO ===

function hide_content(id_sec) {
	$('#' + id_sec + ' header').animate({'opacity':0},600);
	$('#' + id_sec + ' .close').animate({'opacity':0},600);
	$('#' + id_sec + ' .content').animate({'opacity':0},600);
	
	if (id_sec == 'shop') {
		$('#' + id_sec + ' .panel-background').show();
		$('.shop-content-internal').empty();
		
		// Resetta il menu categorie
		$('.shop-header .sel').removeClass('sel');
		$('.shop-header a:eq(0)').addClass('sel');
	}
}

// === MOSTRA LE SOTTOSEZIONI ===

function showSubContent(num) {
	
	// Seleziona il menu
	$('.open .sub-menu a').removeClass('sel');
	$('.open .sub-menu a:eq(' + (num -1) + ')').addClass('sel');
	
	if ($('.open .content[name="subcontent' + num + '"]').css('opacity') == 0) {
		yPos = $('#container').height() - 200;
		$('.open .content').animate({'opacity':0},0);
		$('.open .content[name="subcontent' + num + '"]').css('top',yPos + 200 + 'px').animate({'opacity':1,'top':yPos},2000,'easeInOutQuart');	
	}
	
	if ($('.open').attr('id') == 'atelier') {
		
		if (num == 1) { 
			$('#slider-atelier').show(); 
			$('#controls-riva').show();
			$('#slider-atelier-salo').hide(); 
			$('#controls-salo').hide();
		} else { 
			$('#slider-atelier').hide();
			$('#controls-riva').hide();
			$('#slider-atelier-salo').show();
			$('#controls-salo').show();
		}
		
	}
	
}

// === CARICA I CONTENUTI ESTERNI ===

function loadexternal(indirizzo) {
	
	$('.loading').show();
	$('.shop-content-internal').load(indirizzo + ' #shop-content', function() {
		
		$('.loading').hide();
		$('.external').click(function(event) {
			event.preventDefault();
			loadexternal($(this).attr('href'));
			
		});
		
		
		$('.category').click(function() {
		show_shop_cat();
		});
		
	});	
	
}

function set_extra_actions() {
	
	
}

// === MOSTRA E NASCONDE IL MENU CATEGORIE DELLO SHOP ===

function show_shop_cat() {
	$('.shop-header-container').animate({'height':40},500);
	
}

function hide_shop_cat() {
	$('.shop-header-container').animate({'height':0},500);
}

$(window).resize(function() {
	check_elements();
});

$(document).ready(function() {
	
	check_elements();	
	
	$('.panel').click(function() {
		$(this).addClass('opening');
		open_section($(this).attr('id'));
	});
	
	$('.panel').mouseenter(function() {
		
		if (!$(this).hasClass('opening') && !$(this).hasClass('open')) {
			$(this).children('.cover').animate({'opacity':0},500);
			$(this).children('.titolo').animate({'opacity':1},500);
		}

	});
	
	$('.panel').mouseleave(function() {
		if (!$(this).hasClass('opening') && !$(this).hasClass('open')) {
			$(this).children('.cover').animate({'opacity':0.5},500);
			$(this).children('.titolo').animate({'opacity':0.4},500);
		}
		//if (!$(this).hasClass('open')) { $(this).children('.panel-background').animate({'opacity':0.75},500); }
	});
	
	// Apertura e chiusura menu
	
	$('nav').mouseenter(function() {
		$(this).stop().animate({'left':-10},500,'easeInOutQuart');
		$(this).children('#scritta').stop().animate({'opacity':0},500);
		$(this).children('#menu').stop().animate({'left':40},500);
	});
	
	$('nav').mouseleave(function() {
		$(this).stop().animate({'left':-100},500,'easeInOutQuart');
		$(this).children('#scritta').stop().animate({'opacity':1},500);
		$(this).children('#menu').stop().animate({'left':-40},500);
	});
	
	// Menu principale
	
	$('nav a').click(function() {
		open_section($(this).attr('name'));
		
	});
	
	// Chiusura pannelli
	
	$('.panel .close').click(function() {
		next_section = "";
		close_section($(this).parents('.panel').attr('id'));
	});
	
	/*
	$('.external').click(function() {
		event.preventDefault();
		loadexternal($(this).attr('href'));	
	});
	*/
	
	// Imposta il menu categorie dello shop
	//$('.shop-header a').click(function() { $('.shop-header a').removeClass('sel'); $(this).addClass('sel'); });
	
	// Credits
	$('#credits').mouseenter(function() {
		$(this).children('.scritta').stop().animate({'left':300},500,'easeInOutQuart');
		$(this).children('.dettagli').stop().animate({'left':0},500,'easeInOutQuart');
	});
	
	$('#credits').mouseleave(function() {
		$(this).children('.scritta').stop().animate({'left':0},500,'easeInOutQuart');
		$(this).children('.dettagli').stop().animate({'left':300},500,'easeInOutQuart');
	});
	
});
