//var addDomain="http://192.168.0.100/HLP_HotelLidoPerugia/VERS2_DICEMBRE2011/";
var addDomain="http://www.hotellidoperugia.com/";

var supportTouch = ("ontouchend" in document);
var touchStartEvent = supportTouch ? "touchstart" : "mousedown";

var colorePrimario="rgb(139,166,218)";
var coloreOrigineFooter="rgb(112,119,158)";
var frameShowed;
var frameLm=1;
var frameGb=1;
var frameAwd=1;
var avviaSlideLm;
var avviaSlideGb;
var avviaSlideAwd;

$(document).ready(function(){
	
	if ($(".fancy").length>0) {
		$(".fancy").fancybox({
			'padding'		: 0,
			'margin'		: 0,
			'hideOnOverlayClick' : false
		});;
	}
	
	var altezza = $(document).height();
	var larghezza = $(document).width();

	if ($(".camposelect").length>0) {
		$(".camposelect").select_skin();
	}

	if($("#booking-bar").length>0) {
		$(".datepicker").datepicker({
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: addDomain+'/img/datepicker.gif',
			monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
			dayNamesMin: ['Do','Lu','Ma','Me','Gi','Ve','Sa'],
			minDate: '+1d',
			maxDate: '+1y',
			firstDay: 1
		});
	}
	
	if ($("header #abstracts").length>0) {
		$("header #abstracts dt").animate({opacity: 0},0);
		$("header #abstracts dt").css("display","block");
		var tmpActualFrame=$("header #abstracts").attr("class").split("-");
		var actualFrame=tmpActualFrame[1];
		frameShowing(0,actualFrame);
				
		$("header nav ul li a").mouseenter(function() {
			$(this).stop(true, false).animate({color: colorePrimario}, 200);
			var frameNum=$(this).attr("rel");
			if (frameShowed!=frameNum) {
				frameShowing(frameShowed,frameNum);
			}
		})
		
		$("header nav ul li a").mouseleave(function() {
			if ($(this).parent().attr("class")!="act") {
				$(this).stop(true, false).animate({color: "rgb(255,255,255)"}, 200);									 
			}
		})
				
	}
	
	$("#dragger").draggable({
		axis: 			"x",
		containment:	"parent"
	});
	
	$("#frameAbs-1").droppable({
		drop:	function(event, ui) {
			frameShowing(frameShowed,1);
		}
	});
	
	$("#frameAbs-2").droppable({
		drop:	function(event, ui) {
			frameShowing(frameShowed,2);
		}
	});

	$("#frameAbs-3").droppable({
		drop:	function(event, ui) {
			frameShowing(frameShowed,3);
		}
	});

	$("#frameAbs-4").droppable({
		drop:	function(event, ui) {
			frameShowing(frameShowed,4);
		}
	});

	$("footer li.recapiti").css("height",($("footer nav").height()-20)+"px");
	
	$("footer dt a, .breadcrumb a, .menu-interno li a, .allegati dt a, a.paginate").mouseenter(function() {
		$(this).stop(true, false).animate({color: colorePrimario}, 200);
	})
	
	$("footer dt a, .breadcrumb a, .menu-interno li a, .allegati dt a, a.paginate").mouseleave(function() {
		$(this).stop(true, false).animate({color: coloreOrigineFooter}, 200);
	})

	$(".lm-dx a, footer p a, #abstracts dt a, #posts-cnt .abstract a, #contatti-cnt aside ul li a, #extended-cnt article p a").mouseenter(function() {
		$(this).stop(true, false).animate({color: coloreOrigineFooter}, 200);
	})
	
	$(".lm-dx a, footer p a, #abstracts dt a, #posts-cnt .abstract a, #contatti-cnt aside ul li a").mouseleave(function() {
		$(this).stop(true, false).animate({color: colorePrimario}, 200);
	})

	$("#extended-cnt article p a").mouseleave(function() {	
		$(this).stop(true, false).animate({color: "rgb(160,160,160)"}, 200);
	})

	if ($("#home-sprite").length>0) {
		playSprite();
	}


	if ($("#lastminute").length>0) {
		
		$("#lastminute ul li").animate({opacity: 0}, 0);
		var numSlide=$("#lastminute ul li").length;
		
		if ($("#offerte-cnt").length<=0) {
			
			if (numSlide>4 && $("#home-cnt").length>0) {
				avviaSlideLm=setInterval("playSlideLm()",5000);	
			
				if (navigator.userAgent.match(/Android/i) ||navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
					$("#lastminute ul").draggable({
						handle: "li",
						cancel: "a.lmLink",
						start: function() {
							clearInterval(avviaSlideLm);
						},
						stop: function() {
							//alert($(this).position().left);
							//alert($(this).children("li").length);
							var newLeft=$(this).position().left;
							
							if ($(this).parent().parent().attr("id")=="colonna-dx") {
								var maxLeft="-"+(($(this).children("li").length*250)-250);
							} else {
								var maxLeft="-"+(($(this).children("li").length*250)-1000);
							}
							
							//alert(maxLeft);
							if (newLeft>0) {
								$(this).animate({left: "0px"}, 500);
							} else if (newLeft<maxLeft) {
								$(this).animate({left: maxLeft+"px"}, 500);
							}
							
							$('#lastminute ul li a').bind(touchStartEvent, function() {
								var myLink=$(this).attr("href");
								window.location.href=myLink;
							});

							
						},
						axis: "x"
					});
					
				}
							
			}

			if (numSlide>1 && $("#home-cnt").length<=0) {
				avviaSlideLm=setInterval("playSlideLm()",5000);	
			}

			setTimeout(function() {
				$("#lastminute ul li").css("visibility", "visible");
				defineLastminuteHome();					
			},800);


		} else {
			
			setTimeout(function() {
				$("#lastminute ul li").css("visibility", "visible");
				defineLastminuteInt();
			},800);
		
		}
		
	}



	if ($("#guestbook").length>0) {

		var numSlide=$("#guestbook ul li").length;
		$("#guestbook ul li").hide().animate({opacity: 0}, 0);

		defineGuestbook();
		setTimeout("defineGuestbook()",1000);

		if (numSlide<=1) {
			$("#guestbook ul li").css('display','block');			
		} else {
			$("#guestbook ul li#commento-"+frameGb).show().animate({opacity: 1}, 500);
			avviaSlideGb=setInterval("playSlideGb()",6000);	
		}
		
	}
	
	if ($(".riconoscimenti").length>0) {
		var numSlide=$(".riconoscimenti dt img").length;
		var listaSlideLen=205*numSlide;
		$(".riconoscimenti dt").css('width',listaSlideLen+'px');
		
		avviaSlideAwd=setInterval("playSlideAwd()",4000);	
	}
	
	if ($("#extended-cnt article p").length>0) {
		$("#extended-cnt article p:first").css("padding-top","0px");
		$("#extended-cnt article p:first").css("margin-top","0px");
	}

	if ($(".galleria ul").length>0) {
		$('.galleria ul').roundabout({
			shape:	'tearDrop'
		});	
	}	

	if (navigator.userAgent.match(/Android/i) ||navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
		if ($(".galleria ul").length>0) {
			$('.galleria ul').css("overflow","hidden");	
		}	
	}
	
	
	if ($("#tariffe-cnt").length>0) {
		
		var numGruppi=$("#tariffe-cnt .gruppo").length;
		//alert(numGruppi);
		
		for (i=1; i<=numGruppi; i++) {
		
			var numIntestazioni=$("#listinoPrezzi-"+i+" .td-intest").length;
			//alert(numIntestazioni);
			
			if (numIntestazioni>3) {
				
				var myWidth=numIntestazioni*100+150;
				
				$("#listinoPrezzi-"+i+" .listino").css("width",myWidth+"px");
				//$("#listinoPrezzi-"+i+" .)				
				
				$("#listinoPrezzi-"+i+" .listino-area p").css("display", "block");
				
				$("#listinoPrezzi-"+i+" .listino").css("cursor", "move");
				
				$("#listinoPrezzi-"+i+" .listino").draggable({
					axis: 			"x",
					stop: function() {
						//alert($(this).position().left);
						//alert($(this).children("li").length);
						
						//alert(myWidth);
						var newLeft=$(this).position().left;
						var maxLeft="-"+(myWidth-445);
						
						//alert(maxLeft);
						if (newLeft>0) {
							$(this).animate({left: "0px"}, 500);
						} else if (newLeft<maxLeft) {
							$(this).animate({left: maxLeft+"px"}, 500);
						}
					}
					
				});
			
			}
		
		}
		
		
	}
	
	if ($(".mappaNormale").length>0) {
		loadmap();
	}

	if ($(".mappaStreetView").length>0) {
		loadStreetView();
	}

});

$(window).scroll(function() {
	if ( /*navigator.userAgent.match(/Android/i) ||navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) ||*/ navigator.userAgent.match(/iPad/i)) {
		posizionaBooking();
	}
})

//$(window).resize(function(){})

function defineLastminuteHome() {
	var numSlide=$("#lastminute ul li").length;
		
	var listaSlideLen=250*numSlide;
	$("#lastminute ul").css('width',listaSlideLen+'px');
	
	$("#lastminute ul li").animate({height: ($("#lastminute ul").height()-30)+"px"}, 200);		
	$("#lastminute ul li div").animate({height: ($("#lastminute ul").height()-30)+"px"}, 200, function() {
		$("#lastminute ul li").animate({opacity: 1}, 300);																								   
	});
		
}

function defineLastminuteInt() {
	var numSlide=$("#lastminute ul li").length;
		
	var tmpHeight=150;
	var singleHeight;
	
	for (i=1; i<=numSlide; i++) {
		singleHeight=$("#lastminute ul li#lsm-"+i).height();
		//alert(singleHeight);
		if (singleHeight>tmpHeight) {
			tmpHeight=singleHeight;	
		}
	}
	
	$("#lastminute ul li, #lastminute ul li div").animate({height: tmpHeight+"px"}, 200, function() {
		$("#lastminute ul li").animate({opacity: 1}, 300);																								   
	});	

}



function defineGuestbook() {
	$("#guestbook ul li").css("height",($("#guestbook ul").height()-140)+"px");				
}

function posizionaBooking() {
	$("#booking-bar").stop(true, false).animate({top: window.pageYOffset+"px"}, 200);
}

function frameShowing(toHide,toShow) {
	//alert(toHide);
	//alert(toShow);
	frameShowed=toShow;
	frameMoveImage(toHide,toShow);
	if (toHide!=0) {
		$("header #abstracts dt#frameAbs-"+toHide).stop(true, false).animate({opacity: 0}, 200);
		$("header #abstracts dt#frameAbs-"+toShow).stop(true, false).animate({opacity: 1}, 300);
	} else {
		$("header #abstracts dt#frameAbs-"+toShow).stop(true, false).animate({opacity: 1}, 0);
	}
}

function frameMoveImage(durata,num) {
	tmpMisura=(num*330)-330;
	if (durata!=0) {
		$("header #images dl").stop(true, false).animate({top: "-"+tmpMisura+"px"}, 700, "easeInOutExpo");
		$("#dragger").stop(true, false).animate({left: num*246-246+"px"}, 200);
	} else {
		$("header #images dl").stop(true, false).animate({top: "-"+tmpMisura+"px"}, 0);
		$("#dragger").stop(true, false).animate({left: num*246-246+"px"}, 0);
	}
}

function playSprite() {
	var el1=$("#home-sprite #divano");	
	var el2=$("#home-sprite #bicchiere");	
	var el3=$("#home-sprite #cappello");	
	var el4=$("#home-sprite #timone");
	
	el1.animate({opacity: 0}, 0);
	el2.animate({opacity: 0, top: "-200px", left: "39px"}, 0);
	el3.animate({opacity: 0, top: "-200px", left: "119px"}, 0);
	el4.animate({opacity: 0, top: "-80px", left: "28px"}, 0);
	
	setTimeout(function() {
		el1.animate({opacity: 1}, 1500, function() {
			el3.animate({opacity: 1, top: "57px"}, 400, function() {
				el4.animate({opacity: 1, top: "46px"}, 600, function() {
					el2.animate({opacity: 1, top: "7px"}, 400);												 
				});										 
			});										 
		});
	}, 1500);
}

function slideLm(tipo,num) {
	var numSlide=$("#lastminute ul li").length;
	var tmpMisura;
	
	if ($("#home-cnt").length>0) {
		var lastFrame=numSlide-3;
		
		if (tipo==1) {
			if (frameLm<lastFrame) {
				num=frameLm+1;
			} else if (frameLm==lastFrame) {
				num=1;
			}
		}
	} else {
		if (tipo==1) {
			if (frameLm<numSlide) {
				num=frameLm+1;
			} else if (frameLm==numSlide) {
				num=1;
			}
		}
	}

	tmpMisura=(num*250)-250;
	$("#lastminute ul").stop(true, false).animate({left: "-"+tmpMisura+"px"}, 800, "easeInOutExpo");
	frameLm=num;
}

function playSlideLm() {
	slideLm(1,0);
}



function slideAwd(tipo,num) {
	var numSlide=$(".riconoscimenti dt.awards img").length;
	var tmpMisura;	
	
	if (tipo==1) {
		if (frameAwd<numSlide) {
			num=frameAwd+1;
		} else if (frameAwd==numSlide) {
			num=1;
		}
	}

	tmpMisura=(num*205)-205;
	$(".riconoscimenti dt.awards").stop(true, false).animate({left: "-"+tmpMisura+"px"}, 800, "easeInOutExpo");
	frameAwd=num;
}

function playSlideAwd() {
	slideAwd(1,0);
}



function slideGb(tipo,num) {
	var numSlide=$("#guestbook ul li").length;
	var tmpMisura;	
		
	if (frameGb<numSlide) {
		num=frameGb+1;
	} else if (frameGb==numSlide) {
		num=1;
	}

	$("#guestbook ul li#commento-"+frameGb).show().animate({opacity: 0}, 500, function() {
		$("#guestbook ul li#commento-"+frameGb).hide();
		$("#guestbook ul li#commento-"+num).show().animate({opacity: 1}, 500);
		frameGb=num;
	});
}

function playSlideGb() {
	slideGb(1,0);
}

function checkEmail(email){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(email)){ return false; }else{ return true; }
}


function iscriviNewsletter(myPath,lang){
	var newNome = $("#newslNome").val();
	var newEmail = $("#newslEmail").val();
	var errore = 0;
	
	if (lang=="it") {
		langNum=1;	
	} else if (lang=="en") {
		langNum=2;	
	} else if (lang=="de") {
		langNum=3;	
	} 
	
	errorNome=new Array();
	errorNome[1]="Attenzione, e' necessario inserire il proprio nome";
	errorNome[2]="Attenzione, e' necessario inserire il proprio nome";
	errorNome[3]="Attenzione, e' necessario inserire il proprio nome";

	errorEmailNull=new Array();
	errorEmailNull[1]="Attenzione, e' necessario inserire la propria email";
	errorEmailNull[2]="Attenzione, e' necessario inserire la propria email";
	errorEmailNull[3]="Attenzione, e' necessario inserire la propria email";

	errorEmail=new Array();
	errorEmail[1]="Attenzione, e' necessario inserire un'email valida";
	errorEmail[2]="Attenzione, e' necessario inserire un'email valida";
	errorEmail[3]="Attenzione, e' necessario inserire un'email valida";

	responsoOK=new Array();
	responsoOK[1]="<strong>Congratulazioni!</strong><br />Iscrizione alla newsletter avvenuta con successo";
	responsoOK[2]="<strong>Congratulazioni!</strong><br />Iscrizione alla newsletter avvenuta con successo";
	responsoOK[3]="<strong>Congratulazioni!</strong><br />Iscrizione alla newsletter avvenuta con successo";

	responsoKO=new Array();
	responsoKO[1]="<b>Attenzione!</b><br />Errore durante l'iscrizione alla newsletter. Provare di nuovo";
	responsoKO[2]="<b>Attenzione!</b><br />Errore durante l'iscrizione alla newsletter. Provare di nuovo";
	responsoKO[3]="<b>Attenzione!</b><br />Errore durante l'iscrizione alla newsletter. Provare di nuovo";

	responsoPres=new Array();
	responsoPres[1]="<b>Attenzione!</b><br />Email gi&agrave; presente nella nostra mailing-list.";
	responsoPres[2]="<b>Attenzione!</b><br />Email gi&agrave; presente nella nostra mailing-list";
	responsoPres[3]="<b>Attenzione!</b><br />Email gi&agrave; presente nella nostra mailing-list";

	
	if(newNome == ""){ errore = 1; alert(errorNome[langNum]); $("#newslNome").focus();
	}else if(newEmail == ""){ errore = 1; alert(errorEmailNull[langNum]);$("#newslNome").focus();
	}else if(!checkEmail(newEmail)){ errore = 1; alert(errorEmail[langNum]); $("#newslNome").focus(); }
	
	if(errore == 0){
		//$("#loadingImg").show();
		//$("#newsletterBox").hide();
		$.post(
			myPath+"inc/funzioniAjax.php", {ope:"iscriviNewsletter", nome:newNome, email:newEmail},
			function(data){
				var messaggio = "";
				if(data == "ok"){ messaggio = responsoOK[langNum];
				}else if(data == "ko"){ messaggio = responsoKO[langNum];
				}else if(data == "pres"){ messaggio = responsoPres[langNum]; }
				$("#newsletterBox").html(messaggio);
			}
		);
	}
}


function openFoto(fotoName,myPath) {
	var myLink=myPath+"upload/"+fotoName;
	$.fancybox({
		'padding'		: 0,
		'margin'		: 0,
		'href'			: myLink,
		'hideOnOverlayClick' : false
	});
}


function loadmap() {
	map = new google.maps.Map(document.getElementById("mappa"), {
		//center: new google.maps.LatLng(40.8085487,14.2012888),
		center: latlng,
		zoom: 11,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: true,
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
		streetViewControl: 0
	});
	infoWindow = new google.maps.InfoWindow;
	var marker = new google.maps.Marker({
		position: latlng,
		map: map
		//title: "La mia localita"
	});
}

function loadStreetView(){
	//var sview = new google.maps.LatLng(40.8085487,14.2012888);
	var sview = latlng;

	var panoramaOptions = {position:sview,pov: {heading: -100,pitch:10,zoom:1}};
	var myPano = new google.maps.StreetViewPanorama(document.getElementById("mappa"), panoramaOptions);

}	

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

