var frameHeader=1;
var autoSlideHeader=1;
var avviaSlideHeader;
var stopSlideHeader;
var ricerca=0;
var mainColor="#00b4e0";
var slaveColor="#f0a815";
var headStruttura=0;
var frameGallery=1;
var movingHeaderComplete=0;


$(document).ready(function(){

	var altezza = $(document).height();
	var larghezza = $(document).width();
	
	/*
	var ancoraggio=window.location.hash;
	if (ancoraggio!="") {
		$.scrollTo(ancoraggio, 600);
	}
	*/
	
	if ($("#home-slide ul").length>0) {
		var numSlide=$("#home-slide ul li").length;
		var listaSlideLen=980*numSlide;
		$("#home-slide ul").css('width',listaSlideLen+'px');
		
		$("#home-slide .sx").click(function(event) {
			slideHeader(2,0);
			autoSlideHeader=0;
			stopSlideHeader=clearInterval(avviaSlideHeader);
			event.preventDefault();
		});

		$("#home-slide .dx").click(function(event) {
			slideHeader(1,0);
			autoSlideHeader=0;
			stopSlideHeader=clearInterval(avviaSlideHeader);
			event.preventDefault();
		});
		
		avviaSlideHeader=setInterval("playSlideHeader()",5000);	
	}

	$("#mappaPortale").mouseenter(function() {
		$("#mappaPortale .mappa").stop(true, false).animate({top: "0px", left: "490px", width: "386px", height: "300px"}, 500, "easeInOutExpo");
	});

	$("#mappaPortale").mouseleave(function() {
		$("#mappaPortale .mappa").stop(true, false).animate({top: "-150px", left: "10px", width: "900px", height: "700px"}, 500, "easeInOutExpo");
	});


	if ($("#searchForm").length>0) {
		$("#searchForm").hide().animate({opacity: 0}, 0);	
	}

	$(".apriCerca").click(function(event) {
		if (ricerca==0) {
			$("#ricerca .contenuti").stop(true, false).animate({height: "460px"}, 1000, "easeInOutExpo", function() {
				$("#searchForm").show().animate({opacity: 1}, 400);
				ricerca=1;	
			});
		} else if (ricerca==1) {
			$("#searchForm").animate({opacity: 0}, 400, function() {
				ricerca=0;
				$("#searchForm").hide();
				$("#ricerca .contenuti").stop(true, false).animate({height: "41px"}, 600, "easeOutExpo")	
			});			
		};
		event.preventDefault();								   
	});

	$(".goToLista").click(function(event) {
		$.scrollTo(960, 600);
		event.preventDefault();								   
	});


	if ($(".sottomenu").length>0) {
		
		//$(".sottomenu").animate({opacity: 0},0);
		
		setTimeout(function() {
			
			//$(".sottomenu").css("visibility","visible");
			
			for (i=1; i<=8; i++) {
								
				leftMb=Math.floor($("#mb-"+i).position().left);
				
				//alert(leftMb);
				
				if ($("#menu-"+i).length>0) {
										
					$("#menu-"+i).animate({left: "-"+leftMb+"px"}, 200);
					
					/*var SMnum=$("#menu-"+i+" ul li").length;
					var SMtmpHeight=14;
					
					for (v=1; v<=SMnum; v++) {
						var SMcurrHeight=$("#menu-"+i+" ul li.voceNr-"+v+" a").height();
						//alert(SMcurrHeight)
						if (SMcurrHeight>SMtmpHeight) {
							SMtmpHeight=SMcurrHeight;	
						}
					}
					$("#menu-"+i+" ul li a").css("height",SMtmpHeight+"px");*/
				
				}				
				
			}
		}, 300);
				
	}
	
	$("#menubar li.exp a.linkMb").mouseenter(function() {
		//alert("ciao!");
		var SMtmpHeight=14;
		var idName=$(this).parent().attr("id");
		var tmp=idName.split("-");
		id=tmp[1];
		//alert(id);
		var SMnum=$("#menu-"+id+" ul li").length;
		for (v=1; v<=SMnum; v++) {
			var SMcurrHeight=$("#menu-"+id+" ul li.voceNr-"+v+" a").height();
			if (SMcurrHeight>SMtmpHeight) {
				SMtmpHeight=SMcurrHeight;	
			}
		}
		$("#menu-"+id+" ul li a").css("height",SMtmpHeight+"px");
		//event.preventDefault();
	});
	
	/*$("#menubar li.exp").mouseenter(function() {
		var idName=$(this).attr("id");
		var tmp=idName.split("-");
		id=tmp[1];
		$(this).children("a").css("background",mainColor);		
		$("#menu-"+id).show().css("display","block");
		//$(this).parent().stop(true, false).animate({backgroundColor: "#ffdb00"}, 200);
	});

	$("#menubar li.exp").mouseleave(function() {
		var idName=$(this).attr("id");
		var tmp=idName.split("-");
		id=tmp[1];
		$(this).children("a").css("background","");
		$("#menu-"+id).hide();
		//$(this).parent().stop(true, false).animate({backgroundColor: "#ffffff"}, 200);
	});*/

	$("li.struttura").mouseenter(function() {
		$(this).children("a").children("span").stop(true, false).animate({bottom: "75px"}, 300, "easeOutExpo");		
	});

	$("li.struttura").mouseleave(function() {
		$(this).children("a").children("span").stop(true, false).animate({bottom: "0px"}, 300, "easeOutExpo");		
	});
	
	$(".geo-dx li").mouseenter(function() {
		$(this).children("a").stop(true, false).animate({color: mainColor}, 300, "easeOutExpo");		
	});

	$(".geo-dx li").mouseleave(function() {
		$(this).children("a").stop(true, false).animate({color: "#000"}, 300, "easeOutExpo");		
	});
	
	if ($("#structure").length>0) {
		
		/*	
		setTimeout(function(){
			$("#header").animate({top: "-175px"}, 1000, "easeOutExpo");
			$(".struttura-up").animate({top: "100px"}, 1000, "easeOutExpo", function() {
				$("#structure").animate({marginTop: "-168px"}, 300, "easeOutExpo", function() {
					movingHeaderComplete=1;	
				});		
			});
		}, 1500);
		
		$(".sliding").click(function(event) {
			if (headStruttura==0) {
				$(".sliding img").animate({top: "0px"}, 300);
				$("#header").animate({top: "0px"}, 1000, "easeOutExpo");
				$(".struttura-up").animate({top: "275px"}, 1000, "easeOutExpo", function() {
					$("#structure").animate({marginTop: "0px"}, 300, "easeOutExpo", function() {
						headStruttura=1;	
					});		
				});
			} else if (headStruttura==1) {
				$(".sliding img").animate({top: "-42px"}, 300);
				$("#header").animate({top: "-175px"}, 1000, "easeOutExpo");
				$(".struttura-up").animate({top: "100px"}, 1000, "easeOutExpo", function() {
					$("#structure").animate({marginTop: "-168px"}, 300, "easeOutExpo", function() {
						headStruttura=0;	
					});		
				});
			}
			
			event.preventDefault();								   
		});
		*/
	
		
		$(".mb-struttura ul li a.linkFirst").animate({backgroundColor: slaveColor}, 300)
		$("#descrizione .frame-sx").stop(true, false).animate({borderColor: slaveColor}, 300);
		
		
		$(".mb-struttura ul li a").click(function(event) {
			var myAnchor=$(this).attr("rel");
			$(".mb-struttura ul li a").stop(true, false).animate({backgroundColor: mainColor}, 300);
			$(".frame-sx").stop(true, false).animate({borderColor: mainColor}, 300);
			$(this).stop(true, false).animate({backgroundColor: slaveColor}, 300, function() {
				$("#"+myAnchor+" .frame-sx").stop(true, false).animate({borderColor: slaveColor}, 300);
				var offsetAnchor=$("#"+myAnchor).offset();
				var distFromTop=offsetAnchor.top-300;
				$.scrollTo(distFromTop, 400);
			});
			event.preventDefault();
		});


		/*
		var distFromTop;

		$(".mb-struttura ul li a").click(function(event) {
			var myAnchorNum=$(this).attr("rel");
			$(".mb-struttura ul li a").stop(true, false).animate({backgroundColor: mainColor}, 300);
			$(".frame-sx").stop(true, false).animate({borderColor: mainColor}, 300);
			distFromTop=framePosTop[myAnchorNum];		
			$(".headline h1 span").html(distFromTop);
			$(".struttura-dettagliArea").stop(true, false).animate({top: "-"+distFromTop+"px"}, 400, "easeInExpo");
			event.preventDefault();
		});
		*/


		$(".goTop").click(function(event) {
			$(".mb-struttura ul li a").stop(true, false).animate({backgroundColor: mainColor}, 300);
			$(".mb-struttura ul li a.linkFirst").animate({backgroundColor: slaveColor}, 300);
			$(".frame-sx").stop(true, false).animate({borderColor: mainColor}, 300);
			$("#descrizione .frame-sx").stop(true, false).animate({borderColor: slaveColor}, 300);
			$.scrollTo("#structure", 600);
			event.preventDefault();
		});


		$(".goRichiesta").click(function(event) {
			$(".mb-struttura ul li a").stop(true, false).animate({backgroundColor: mainColor}, 300);
			$(".mb-struttura ul li a.linkDisp").animate({backgroundColor: slaveColor}, 300);
			$(".frame-sx").stop(true, false).animate({borderColor: mainColor}, 300);
			$("#preventivo .frame-sx").stop(true, false).animate({borderColor: slaveColor}, 300);
			var offsetAnchor=$("#preventivo").offset();
			var distFromTop=offsetAnchor.top-300;
			$.scrollTo(distFromTop, 400);
			event.preventDefault();
		});
		
	}
	

	if ($(".linkLightbox").length>0) {
		$(".linkLightbox").fancybox();
	}
	
	
	if ($("#gallery").length>0) {
		var numSlide=$("#gallery ul li").length;
		var listaSlideLen=700*numSlide;
		$("#gallery ul").css('width',listaSlideLen+'px');
		
		$("#gallery .sx").click(function(event) {
			slideGallery(2,0);
			event.preventDefault();
		});

		$("#gallery .dx").click(function(event) {
			slideGallery(1,0);
			event.preventDefault();
		});
	}
	
	
	$("#lastminute .frame-dx ul li").mouseenter(function() {
		$(this).stop(true, false).animate({borderColor: mainColor}, 300, "easeInOutExpo");
	});

	$("#lastminute .frame-dx ul li").mouseleave(function() {
		$(this).stop(true, false).animate({borderColor: "#f0f0f0"}, 300, "easeInOutExpo");
	});


	if ($(".linkOfferta").length>0) {
		$(".linkOfferta").fancybox({
			width : 600,
			height : 500,
			padding : 0,
			margin : 0,
			titleShow : false,
			opacity : false,
			modal : false,
			cyclic : false,
			scrolling : 'no',	// 'auto', 'yes' or 'no'
			autoScale : false,
			autoDimensions : false,
			centerOnScroll : true,
			overlayShow : true,
			overlayOpacity : 0.9,
			overlayColor : '#000'
		});	
	}


	if ($(".datepicker").length>0) {
		$(".datepicker").datepicker({
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: '/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 ($("#ricerca-opzioni").length>0) {
		//alert("!");
		$(".autoStrutture").autocomplete({
			minLength: 3,
			source:function(request, response){
				myId = $(this).attr('element').attr('id');
				$.ajax({
					url: "/admin/funzioni_completamento.php",
					dataType: "json",
					data:{ query_type:"strutture", term:request.term },
					success: function(data) {
						response($.map(data, function(item) {
							return{ label:item.label, value:item.label, idval:item.value }
						}));
					}
				});
			},
			select: function(event, ui){ $("#"+myId+"_res").val(ui.item.idval);this.close; }
		});
		
		$(".autoLocalita").autocomplete({
			minLength: 3,
			source:function(request, response){
				myId = $(this).attr('element').attr('id');
				$.ajax({
					url: "/admin/funzioni_completamento.php",
					dataType: "json",
					data:{ query_type:"localita", term:request.term },
					success: function(data) {
						response($.map(data, function(item) {
							return{ label:item.label, value:item.label, idval:item.value }
						}));
					}
				});
			},
			//alert(item.label);
			select: function(event, ui){
				//alert(ui.item.label);
				$("#"+myId+"_value").val(ui.item.idval);this.close;					
			}
		});
	}
	
	$(".myDist").click(function(event) {
		if ($("#ric_localita").val()=="") {
			alert("Attenzione! Per scegliere una distanza, e' necessario prima definire una localita' di riferimento.");
			event.preventDefault();	
		}
	});
	
	
	/*
	if ($("#banner-req").length>0) {
		if (larghezza<1200) {
			$("#banner-req").css("display","none");
		} else {
			$("#banner-req").animate({top: 0}, 600);		
		}
	}
	*/
	
	if ($(".mb-struttura .linkDisp").length>0) {
		setInterval("blinkDisp()", 1000);
	}
		
		
	

});


/*function posizioneBanner() {
	scrollAmount=$(window).scrollTop();
	$("#banner-req").stop(true, false).animate({"top": scrollAmount + "px"}, 200);
}

$(window).scroll(function() {
	posizioneBanner();
});*/


function slideHeader(tipo,num) {
	var numSlide=$("#home-slide ul li").length;
	var tmpMisura;	
	
	if (tipo==1) {
		if (frameHeader<numSlide) {
			num=frameHeader+1;
		} else if (frameHeader==numSlide) {
			num=1;
		}
	}

	if (tipo==2) {
		if (frameHeader>1) {
			num=frameHeader-1;
		} else if (frameHeader==1) {
			num=numSlide;
		}
	}

	tmpMisura=(num*980)-980;
	$("#home-slide ul").stop(true, false).animate({left: "-"+tmpMisura+"px"}, 1000, "easeInOutExpo");
	frameHeader=num;
}

function playSlideHeader() {
	//alert("vado");
	slideHeader(1,0);
}

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

	if (tipo==2) {
		if (frameGallery>1) {
			num=frameGallery-1;
		} else if (frameGallery==1) {
			num=numSlide;
		}
	}

	tmpMisura=(num*700)-700;
	$("#gallery ul").stop(true, false).animate({left: "-"+tmpMisura+"px"}, 1000, "easeInOutExpo");
	frameGallery=num;
}


function blinkDisp() {
	$(".mb-struttura .linkDisp font").animate({opacity: 0}, 400, function() {
		$(".mb-struttura .linkDisp font").animate({opacity: 1}, 200);
	});
}

function loadmap() {
	map = new google.maps.Map(document.getElementById("boxMappa"), {
		//center: new google.maps.LatLng(40.8085487,14.2012888),
		center: latlng,
		zoom: 15,
		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: 0,pitch:0,zoom:-5}};
	var myPano = new google.maps.StreetViewPanorama(document.getElementById("boxMappaStreetView"), panoramaOptions);

}


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


