// JavaScript Document

$(document).ready(function(){
	//alert('Ready!');
	
	//PRELOAD DELLE IMMAGINI INCLUSE NEI CSS E NELLA PAGINA
	/*cssImagesToPreload = [url+'site/template/images/logo.png',url+'site/template/images/frame.png',url+'site/template/images/flowers.png',url+'site/template/images/accordion.png'];
	pageImagesToPreload = $('img');
	
	$.preload(cssImagesToPreload,{
		onFinish: function(){ 
			$.preload(pageImagesToPreload,{
				onFinish: function(){ 
					//INIZIALIZZAZIONE DELLE ANIMAZIONI
					startAnimations(); 
				}				 
			});
		}				  
	});*/

// TREEVIEW
$("#navigationtree").treeview({
    persist: "location",
    collapsed: true,
    unique: false 
    });

	//TARGET _BLANK
	$("a._blank").click(function(){this.target="_blank";});
	
	//NASCONDI ADVICE
	$("span.close").click(function(){$(this).parent().parent().hide("slow");});
	$("div.info").click(function(){$("div.info").hide("slow");});
	if ($("div.info").html()){setTimeout('$("div.info").fadeOut("slow");',5000);}
	
	//DISABILITO IL TASTO DESTRO PER LE IMMAGINI DELLA PAGINA
	$("img").bind("contextmenu",function(e){return false;});
	
	//FANCYBOX
	/*$("a.fancyimage").fancybox({
		overlayShow: true,
		overlayOpacity: 0.7,
		overlayColor: '#000'
    });*/


	

	//FANCYBOX
		$("a.fancyimage").fancybox({
		'padding'		:	0,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		//'title'			: 	getTitle,
		'overlayShow'	: 	true,
		'overlayOpacity': 	0.7,
		'overlayColor'	: 	'#1A1A1A',
		'cyclic'		:	true,
		'titlePosition'	: 	'over',
		'titleFormat'	: 	function getTitle(title, currentArray, currentIndex, currentOpts) {			
			if (title == "") {
				title = $(currentArray[currentIndex]).find(".caption").html();
			}
			return '<div class="fancybox-title-over" id="fancybox-title" style="bottom: 0px; left: 0px; right: 0px; display: block;"><span id="fancybox-title-over">'+ title +'</span></div>';
		},
		'onComplete'	:	function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").fadeIn("fast");
			}, function() {
				$("#fancybox-title").fadeOut("fast");
			});
		}
	});

	
	
	$("a.fancycontent").fancybox();
	$("a.fancyplayer").fancybox();
	$("a.fancyyoutube").fancybox();
	$("#credits").fancybox({
		'autoDimensions':	false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	200,
		'height'		:	380
	});
	$("#privacy_text_link,#privacy_link,#policy_link").fancybox({
		'autoDimensions':	false,
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'overlayOpacity':	0.7,
		'overlayColor'	:	'#000000',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	550,
		'height'		:	600
	});
	
		
	//PAGINAZIONE
	//$("#gallery").paginate();
	/*$('#gallery, #category').pager('div.pager', {
		navId: 'pagination',
		highlightClass: 'current'
	});*/

	//EFFETTI DELLA GALLERY
	$("a.image_frame").hoverIntent(
	  function () {
		$(this).animate({
    		opacity: 1
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		//$(this).find(".caption").slideDown('fast');
		
		$(this).find("img").animate({
    		width: '78px',
			height: '78px',
			top: '0px',
			left: '0px'
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".greyscale").fadeOut();
	  }, 
	  function () {
		$(this).animate({
    		opacity: 0.7
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		//$(this).find(".caption").slideUp('fast');
		
		$(this).find("img").animate({
    		width: '180px',
			height: '180px',
			top: '-73px',
			left: '-73px'
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".greyscale").fadeIn();
	  }
	);
	
	//VALIDAZIONE DELLE FORM
	$("form").each(function(){
			$(this).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();
    	}
	);
	
	//SCROLLTO
	$("#services_menu a").click(function(event){
		event.preventDefault;
		$.scrollTo($(this).attr("href"),1000,{offset:-15});
		return false;
	});
	
	//NEWSTICKER
	//cache the ticker
	var ticker = $("#ctl00_cphNews_divNews");
	  
	//wrap dt:dd pairs in divs
	ticker.children().filter("dt").each(function() {
	  
	  var dt = $(this),
		container = $("<div>");
	  
	  dt.next().appendTo(container);
	  dt.prependTo(container);
	  
	  container.appendTo(ticker);
	});
			
	//hide the scrollbar
	ticker.css("overflow", "hidden");
	
	//animator function
	function animator(currentItem) {
		
	  //work out new anim duration
	  var distance = currentItem.height();
		duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;

	  //animate the first child of the ticker
	  currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
		
		//move current item to the bottom
		currentItem.appendTo(currentItem.parent()).css("marginTop", 0);

		//recurse
		animator(currentItem.parent().children(":first"));
	  }); 
	};
	
	//start the ticker
	animator(ticker.children(":first"));
			
	//set mouseenter
	ticker.mouseenter(function() {
	  
	  //stop current animation
	  ticker.children().stop();
	  
	});
	
	//set mouseleave
	ticker.mouseleave(function() {
			  
	  //resume animation
	  animator(ticker.children(":first"));
	  
	});

	
	//HSLIDES
	/*if($(".slide").length > 0) {
		var numOfSlides = $(".slide").length;
		
		var orignialSlideWidth = $(".slide").css("width");
		orignialSlideWidth = orignialSlideWidth.replace("px","");
		var orignialSlideHeight = $(".slide").css("height");
		orignialSlideHeight = orignialSlideHeight.replace("px","");
		
		var maxSlideWidth = orignialSlideWidth * 2;
		var minSlideWidth = ((orignialSlideWidth * numOfSlides) - maxSlideWidth) / (numOfSlides - 1);
			
		$(".slide").hoverIntent(
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({width: maxSlideWidth},500,function(){
					//$(this).children(".caption").stop().slideDown(150,function(){$(this).css("display","block")});
					//$(this).children(".slide_arrow").stop().fadeIn(300,function(){$(this).css("display","block")});
				});
			
			$(".slide").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({width: minSlideWidth},500);
				}
			});
	
		  }, 
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({width: orignialSlideWidth},500,function(){});
			
			//$(this).children(".caption").stop().slideUp(150);
			//$(this).children(".slide_arrow").stop().fadeOut(150);
			
			$(".slide").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({width: orignialSlideWidth},500);
				}
			});
		  }
		);
	}*/
	
	
	//VSLIDES
	if($(".animated_box,").length > 0) {
		var numOfSlides = $(".animated_box").length;
		
		var orignialSlideWidth = $(".animated_box").css("width");
		orignialSlideWidth = orignialSlideWidth.replace("px","");
		var orignialSlideHeight = $(".animated_box").css("height");
		orignialSlideHeight = orignialSlideHeight.replace("px","");
		
		var maxSlideWidth = orignialSlideWidth * 2;
		var minSlideWidth = ((orignialSlideWidth * numOfSlides) - maxSlideWidth) / (numOfSlides - 1);
		
		var maxSlideHeight = orignialSlideHeight * 3;
		var minSlideHeight = 0;
			
		$(".animated_box").hoverIntent(
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({height: maxSlideHeight},500,function(){
					//$(this).children(".caption").stop().slideDown(150,function(){$(this).css("display","block")});
					//$(this).children(".slide_arrow").stop().fadeIn(300,function(){$(this).css("display","block")});
				});
			
			$(".animated_box").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({height: minSlideHeight},500);
				}
			});
	
		  }, 
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({height: orignialSlideHeight},500,function(){});
			
			//$(this).children(".caption").stop().slideUp(150);
			//$(this).children(".slide_arrow").stop().fadeOut(150);
			
			$(".animated_box").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({height: orignialSlideHeight},500);
				}
			});
		  }
		);
	}
	
	/*if($(".big_animated_box,").length > 0) {
		var numOfSlides2 = $(".big_animated_box").length;
		
		var orignialSlideWidth2 = $(".big_animated_box").css("width");
		orignialSlideWidth2 = orignialSlideWidth2.replace("px","");
		var orignialSlideHeight2 = $(".big_animated_box").css("height");
		orignialSlideHeight2 = orignialSlideHeight2.replace("px","");
		
		var maxSlideWidth2 = orignialSlideWidth2 * 2;
		var minSlideWidth2 = ((orignialSlideWidth2 * numOfSlides2) - maxSlideWidth) / (numOfSlides2 - 1);
		
		var maxSlideHeight2 = orignialSlideHeight2 * 2;
		var minSlideHeight2 = 0;
			
		$(".big_animated_box").hoverIntent(
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({height: maxSlideHeight2},500,function(){
					//$(this).children(".caption").stop().slideDown(150,function(){$(this).css("display","block")});
					//$(this).children(".slide_arrow").stop().fadeIn(300,function(){$(this).css("display","block")});
				});
			
			$(".big_animated_box").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({height: minSlideHeight2},500);
				}
			});
	
		  }, 
		  function () {
			theSlide = $(this);
			theSlide.stop().animate({height: orignialSlideHeight2},500,function(){});
			
			//$(this).children(".caption").stop().slideUp(150);
			//$(this).children(".slide_arrow").stop().fadeOut(150);
			
			$(".big_animated_box").each(function(index) {
				if($(this).get(0) === theSlide.get(0)) {
					//do something...
				} else {
					$(this).stop().animate({height: orignialSlideHeight2},500);
				}
			});
		  }
		);
	}*/
	$(".big_animated_box").hoverIntent(
	  function () {
		$('#' + $(this).attr("id") + '_rel').fadeIn(300);
	  }, 
	  function () {
		$('#' + $(this).attr("id")+'_rel').fadeOut(300);
	  }
	);
	
	
	
	//MENU
	$(".no_click").click(function(event){
		event.preventDefault;
		return false;	
	});
	
	var currentSubMenu = $("ul#mainmenu").find("ul.open");	
	$("ul#mainmenu > li").hoverIntent(
	  function () {
		var otherMenu = $(this).children("ul");
		if(otherMenu.get(0) !== currentSubMenu.get(0)) {
			$(currentSubMenu).fadeOut(300);
			$(otherMenu).fadeIn(300);
		} else {
			$(otherMenu).fadeIn(300);	
		}
	  }, 
	  function () {
		var otherMenu = $(this).children("ul");
		if(otherMenu.get(0) !== currentSubMenu.get(0)) {
			$(otherMenu).fadeOut(300);
			$(currentSubMenu).fadeIn(300);
		}
	  }
	);

	  $("a.youtube_1").youtube('video').click(function() { $(this).youtube('video', { autohide: true, autoplay: true, showinfo: true }); });
	
	//ACCORDION
	$(".service_item_title").next("ul").hide();
	$(".service_item_title").click(function(event){
		event.preventDefault;
		$(this).next("ul").slideToggle();
		return false;	
	});
	
	
	//SLIDESHOWS
	$(".slides_group img").imgpreload(function(){
		//ATTIVO LO SLIDESHOW
		//$("#inner_slides").cycle({  MarcoT
	    $("#ctl00_cphProgetti_inner_slides").cycle({ 
			fx:     	'fade', 
			speed:  	1000, 
			timeout: 	3000,
			cssAfter:   "currentSlide",
			//cleartype:     !$.support.opacity,
			cleartypeNoBg: false,
			sync: 1,
			after: function(curr,next,opts){
					if (opts.currSlide+1 == 1) {
						//$("#logo").fadeOut("fast");
						/*$("#logo").animate({
							top: '24',
							left: '218',
							width: '485',
							height: '403'
						  }, 700);*/
					} else {
						//$("#logo").fadeIn("fast");
						/*$("#logo").animate({
							top: '0',
							left: '10',
							width: '81',
							height: '67'
						  }, 700);*/
					}
				}
		});
	});
	
});

function formSubmit(form) {
	//var container = "#result";
	/*
	var container = $(form).find(".result");
	var str = $(form).serialize();
	$.ajax({
	   type: "POST",
	   url: "contatti.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).find("*").fadeOut("slow");
			}, 3000);
	   }
	})  */
	form.submit();
}

function showInlineLoader(container,msg) {
	loaderHTML = '<img src="images/loader.gif" alt="Loading..." /img>&nbsp;';
	messageHTML = '<span>' + msg + '</span>';
	
	$(container).html(loaderHTML + messageHTML);
}

function ltIE7() {
	var semaphore;
	semaphore = !$.browser.msie || ($.browser.msie && ($.browser.version > 6));
	
	return semaphore;
}

//FUNZIONI DI ANIMAZIONE DEGLI OGGETTI DELLA PAGINA
function startAnimations() {
	$("#loader").fadeOut(500);
	$("#outer_outer_canvas").fadeIn(500);
	animation1();
}

function animation1() {
	$("#logo").fadeIn(300);
	$("#haccordion").fadeIn(1500,function(){animation2()});
}

function animation2() {
	$("#haccordion").animate({"left": "-=275px"}, 2000, function(){finalizeAnimations()});
	
}

function finalizeAnimations() {
	//INIZIALIZZAZIONE DEL PRIMO PANNELLO
	openPanel($(".hpanel:first").attr("id"));
	
	//INIZIALIZZAZIONE DELL'EFFETTO DI PROSPETTIVA
	//loadPerspective();
}

