// JavaScript Document
$(window).bind('load', function() {
//$(document).ready(function(){
	//alert('Ready!');
			
			//PRELOAD DELLE IMMAGINI INCLUSE NEI CSS
			$.preloadCssImages();
			
			
			//$("#loader").fadeIn(300,function(){
					
				
						//ANIMAZIONI DELLA PAGINA
						$('#loader').fadeOut(1000);
						$('#start span').fadeIn(1000);
						$('#start').click(function(event) {
								event.preventDefault();
								startAnimations();
								return false;
					    	}
						);
			//}).fadeOut(300);
			
			
			//TARGET _BLANK
			$("a._blank").each(
				function() {
					this.target="_blank"; 
				}
			);
			
			
			//DISABILITO IL TASTO DESTRO PER GLI ELEMENTI SCELTI
			$("img").bind("contextmenu",function(e){
				return false;
			});
			
			
			//VALIDAZIONE DELLE FORM
			$("form").validate({
				submitHandler: function(form) {
				   //form.submit();		//submit in postback
				   formSubmit(form);	//submit in ajax
				   return false;
			 	}
			});
			
			
			//RESET DEI CAMPI DELLA FORM DEI CONTATTI
			$('#reset').click(
				function () {
					$(this).parent("form").clearForm();
		    	}
			);
			
			
			//INIZIALIZZAZIONE DEI FANCYBOX
			$("div.scrollable a").fancybox({
											'zoomSpeedIn': 300,
										   	'zoomSpeedOut': 300,
											'hideOnContentClick':false,
										   	'overlayShow': true,
										   	'overlayOpacity': 0.7,
											'padding': 8
									 });
			
			$("a.fancyissuu").fancybox({  'zoomSpeedIn': 300,
									   	  'zoomSpeedOut': 300,
										  'hideOnContentClick':false,
									   	  'overlayShow': true,
									   	  'overlayOpacity': 0.5,
										  'frameWidth': 760,
										  'frameHeight': 490,
										  'padding': 0
									 	});
			
			
			//INIZIALIZZAZIONE DEGLI ELEMENTI SCROLLABLE
			$("div.scrollable").scrollable({ 
									size: 3,
									easing: "easeOutBack",
									loop: true,
									speed: 1600,
									keyboard: true,
									clickable: false
			}).navigator();
			
			
				//INIZIALIZZAZIONE DEGLI ELEMENTI TOOLTIP
			/*$("div.scrollable a").tooltip({
		        tip: '.tooltip', 
		        position: 'bottom center', 
		        offset: [-130, 3], 
		 		effect: 'slide',
				relative: true
		    });*/
			$("div.item").hoverIntent(
				function() {
					$(this).find("div.tooltip").stop().fadeIn();
				},
				function() {
					$(this).find("div.tooltip").stop().fadeOut();
			});
			
			
			
			//$("div.panel:visible").filter("div.panel:first").hide("fast");
			
			
			//INIZIALIZZAZIONE DEL MENU PER ANIMAZIONE E GESTIONE CONTENUTI
			$("#main_menu ul li").hover(
				function() {
					//$(this).find("a").stop().animate({height:'50px'}, 220);
				},
				function() {
					//$(this).find("a").stop().animate({height:'35px'}, 220);
			});
			
			$("#main_menu ul li a").click(function (event) {
					
					event.preventDefault();
					
					var toOpenPanel = $($(this).attr("href"));
					var visiblePanels = $("div.panel:visible");
					
					visiblePanels.each(function(i){
						if ($(this).attr("id") != toOpenPanel.attr("id")) {
							$(this).fadeOut(300, function() {
									toOpenPanel.fadeIn(300, function() {});
								});
						}
					});
					
					return false;
				}
			);
			
			
			//startAnimations();
		
});

function formSubmit(form) {
	var container = "#result";
	var str = $(form).serialize();
	$.ajax({
	   type: "POST",
	   url: "index.php",
	   data: str,
	   beforeSend: showInlineLoader(container,"Attendere prego..."),
	   success: function(msg){
			//scrivo il messaggio di ritorno
			$(container).html(msg);
			//pulisco la form
			$(form).clearForm();
			//attendo e poi nascondo il messaggio di ritorno
			window.setTimeout(function() {
				$(container + " *").fadeOut("slow");
			}, 3000);
	   }
	});
}

function showInlineLoader(container,msg) {
	loaderHTML = '<img src="images/loader.gif" alt="Loading..." /img>&nbsp;';
	messageHTML = '<span class="loading">' + msg + '</span>';
	
	$(container).html(loaderHTML + messageHTML);
}

function IE() {
	var semaphore;
	semaphore = $.browser.msie;
	
	return semaphore;
}

function ltIE7() {
	var semaphore;
	semaphore = !$.browser.msie || ($.browser.msie && ($.browser.version > 6));
	
	return semaphore;
}

//FUNZIONI DI ANIMAZIONE DEGLI OGGETTI DELLA PAGINA
function startAnimations() {
	animation1();
}

function animation1() {	
	$("#page1").fadeOut(1400,function(){animation2()});
}

function animation2() {
	//$("#page2").fadeIn(1400,function(){animation3()});
	$("#page2").fadeIn(1400,function(){animation3()});
}

function animation3() {
	$("#logo").fadeIn(4200);
	$("#main_menu").slideDown(700,function(){finalizeAnimations()});
}

function finalizeAnimations() {
	animateLogo();
	$("#gallery img").each(function(){
		$(this).attr('src',$(this).attr('src').replace('___', ""));
								
	});
}

//ANIMAZIONE DEL LOGO
function animateLogo() {
	setInterval(function () {
		if ($("#logo").is(":visible")) {
			var first = "logo";
			var second = "stagione";
		} else {
			var first = "stagione";
			var second = "logo";  
		}
		
		$("#"+first).fadeOut(500,function(){
			$("#"+second).fadeIn(500);
		});
	}, 5000);
}
