var CurrentSlide = 0;
var TotalSlide = 0;
var Tempo = 5;//secondi
var Speed = "slow";//velocità fade (slow, normal, fast)
var counter = 0;
var Manual = false;
var Altezza = 0;//Altezza del Fullscreen slideshow aggiornata al ridimensionamento finestra
Shadowbox.init({overlayOpacity:0.85});
var SlideshowH = 1239;//Altezza Pixel delle foto
var SlideshowW = 1920;// Larghezza Pixel delle foto

$(document).ready(function(){ 	
	try{
		//Box in Home Page
		$(".box-home").hover(function(){
			var w = $(this).find("img:first").width()-35;
			var h = $(this).find("img:first").height()-35;
			$(this).find(".over").css("opacity",0.8).css("width",w).css("height",h).fadeIn("normal");
		});
		$(".box-home").mouseleave(function(){
			$(this).find(".over").fadeOut("normal");
		});
		$('#hp-boxes').masonry({
			itemSelector: '.box-home'
		});
		$('#slideshow img').each(function(index) {
			if(index!=CurrentSlide){
				$(this).css("display","none");
			}
			TotalSlide = index;
		});
		$("#slideshow").height($(window).height());
		if($("#slideshow img").first().height()<$(window).height()){
			$("#slideshow img").height($(window).height());
		}
		var imgH = $("#slideshow img").first().height();
		var imgW = (imgH*SlideshowW)/SlideshowH;
		if(imgW!=$(window).width()){
			var NewH = ($(window).width()*SlideshowH)/SlideshowW;
			$("#slideshow img").height(NewH);
		}
		if($("#slideshow img").first().height()<$(window).height()){
			$("#slideshow img").height($(window).height());
		}
		setInterval(function(){SliderNext(false);$('#hp-boxes').masonry('reload');},Tempo*1000);
	}catch(e){}
});
$(window).resize(function(){
	$('#hp-boxes').masonry('reload');
	$("#slideshow").height($(window).height());
		if($("#slideshow img").first().height()<$(window).height()){
			$("#slideshow img").height($(window).height());
		}
		var imgH = $("#slideshow img").first().height();
		var imgW = (imgH*SlideshowW)/SlideshowH;
		if(imgW!=$(window).width()){
			var NewH = ($(window).width()*SlideshowH)/SlideshowW;
			$("#slideshow img").height(NewH);
		}
		if($("#slideshow img").first().height()<$(window).height()){
			$("#slideshow img").height($(window).height());
		}
});
function SliderNext(manual){
	if(manual){Manual=true;}
	if((Manual==false && manual==false) || (Manual==true && manual==true)){
		var add = false;
		$('#slideshow img').each(function(index) {
			if(CurrentSlide==TotalSlide){
				if(index==CurrentSlide){
					$(this).fadeOut(Speed);
				}
				if(index==0){
					$(this).fadeIn(Speed);
					add = false;
				}
			} else {
				if(index==CurrentSlide){
					$(this).fadeOut(Speed);
				}
				if(index==(CurrentSlide+1)){
					$(this).fadeIn(Speed);
					add = true;
				}
			}
  		});
  		if(add){CurrentSlide++;}else{CurrentSlide=0;}
  		counter++;
	}
}
function SliderPrev(manual){
	if(manual){Manual=true;}
	if((Manual==false && manual==false) || (Manual==true && manual==true)){
		var add = false;
		$('#slideshow img').each(function(index) {
			if(CurrentSlide==0){
				if(index==CurrentSlide){
					$(this).fadeOut(Speed);
				}
				if(index==TotalSlide){
					$(this).fadeIn(Speed);
					add = true;
				}
			} else {
				if(index==CurrentSlide){
					$(this).fadeOut(Speed);
				}
				if(index==(CurrentSlide-1)){
					$(this).fadeIn(Speed);
					add = true;
				}
			}
  		});
	if(add){CurrentSlide--;}else{CurrentSlide=TotalSlide+1;SliderPrev(false);}
	}
}
function Slider(num){
	$('#slideshow img').each(function(index) {
    	if(index==CurrentSlide){
			$(this).fadeOut(Speed);
		}
		if(index==num-1){
			$(this).fadeIn(Speed);
		}
  	});
	CurrentSlide = num-1;
	Manual = true;
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function ValidaForm(formName) {
	var email = document.forms[formName].email.value;
	var nome = document.forms[formName].nome.value;
	var telefono = document.forms[formName].telefono.value;
	var messaggio = document.forms[formName].messaggio.value;
	var datapartenza = document.forms[formName].datapartenza.value;
	var dataarrivo = document.forms[formName].dataarrivo.value;
	//var privacy = document.forms[formName].privacy.checked;
	var email_exp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	var validation = true;
	if (nome == "" || nome == "Nome:") {
	validation = false;
		alert("Inserire un Nome valido!");
		document.forms[formName].nome.focus();
		return false;
	}
	if (email.search(email_exp) == -1) {
		validation = false;
		alert("Inserire un\'indirizzo E-mail valido!");
		document.forms[formName].email.focus();
		return false;
	}
	if (messaggio == "" || messaggio == "Messaggio:") {
		validation = false;
		alert("Inserire un Messaggio valido!");
		document.forms[formName].messaggio.focus();
		return false;
	}
	if(telefono == "Telefono:"){
		document.forms[formName].telefono.value = "";
	}
	if(datapartenza == ""){
		document.forms[formName].datapartenza.value = "";
		alert("Scegliere una data di partenza!");
		return false;
	}
	if(dataarrivo == ""){
		document.forms[formName].dataarrivo.value = "";
		alert("Scegliere una data di arrivo!");
		return false;
	}
	/*if (!privacy) {
		validation = false;
		alert("E' necessario accettare i Termini sulla Privacy!");
		document.forms[formName].privacy.focus();
		return false;
	}*/
	if (validation == true) {
		document.forms[formName]._utmz.value = getCookie('__utmz');
		document.forms[formName].submit();
	} else {
		return false;
	}
}
