$(document).ready(function(){

		$("#newsletter_hp").find(":text, :password").each(function(){
				//#agenzie_hp, 
				this.default_value=$(this).val();
				$(this).focus(function(){
						if(this.default_value==$(this).val()){
							$(this).val('');
						}
					}).blur(function(){
						if($(this).val()==''){
							$(this).val(this.default_value);
						}
					});
			});
		
		$("#soggiorni_ricerca select").change(function(){
				   $("#frmsearch").submit();
				});
		$('.fancybox').fancybox();
		$('.fancyframe').fancybox({type:'iframe', width:650});			//$('.scorri_tabella_hover').hover(function(){$(this).css('overflow','visible');},function(){$(this).css('overflow','hidden');});
		$('#agenzie_hp form:eq(0) input').click(function(){location=this.form.action;});
		$("#bm_galleria_immagini div.scrollable ul").addClass("items");
		$("#bm_galleria_immagini div.scrollable").scrollable({vertical:true, size:4, prev:'.prev', next:'.next'});
		if($("#news_hp div.items dl").size()>2){
			$("#news_hp div.scrollable").scrollable({vertical:true, loop:true, size:1});
		}
		ws_news_hp.inizia_ciclo();
		if($.datepicker){
			$.datepicker.regional['it'] = {
				closeText: 'Chiudi',
				prevText: '&#x3c;Prec',
				nextText: 'Succ&#x3e;',
				currentText: 'Oggi',
				monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',
				'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
				monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu',
				'Lug','Ago','Set','Ott','Nov','Dic'],
				dayNames: ['Domenica','Luned&#236','Marted&#236','Mercoled&#236','Gioved&#236','Venerd&#236','Sabato'],
				dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'],
				dayNamesMin: ['Do','Lu','Ma','Me','Gio','Ve','Sa'],
				dateFormat: 'dd/mm/yy', firstDay: 1,
				isRTL: false};
			$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['it']));
			$("#cal_DATA_ANDATA").val(today("/"));
			//$("#cal_DATA_RITORNO").val(tomorrow("/"));
			$("#DATA_ANDATA").val(today("-"));
			//$("#DATA_RITORNO").val(tomorrow("-"));
			$("#cal_DATA_ANDATA").datepicker({minDate: +0, maxDate: '+1Y +1M', altField: '#DATA_ANDATA', altFormat: 'dd-mm-yy'});		
			$("#cal_DATA_RITORNO").datepicker({minDate: +1, maxDate: '+1Y +1M', altField: '#DATA_RITORNO', altFormat: 'dd-mm-yy'});		
		}
		$("#offerte_hp div.bm_paginatore").remove();
		$("#offerte_hp a.prev").click(function(){ws_offerte_hp.incremento = (-1);});
		$("#offerte_hp a.next").click(function(){ws_offerte_hp.incremento = (1);});
		
		/*if(! $.browser.msie){
			$("#offerte_hp div.scrollable .items dl:eq(1)").addClass("centrale");
		}*/
		$("#offerte_hp div.scrollable, #offerte_hp .prev, #offerte_hp .next").hover(function(){ws_offerte_hp.run=false;},function(){/*ws_offerte_hp.inizia_ciclo();*/});
		$("#offerte_hp div.scrollable").scrollable({
			   vertical:false, 
			   size:3, 
			   prev:'.prev', 
			   next:'.next', 
			   loop: false,
			   alert: false,
				 autopause: true,
			   onBeforeSeek: function(){
				   		var centrals, i;
						
					   if(! $.browser.msie){
						   i = this.getIndex();
						if(i==0 && ws_offerte_hp.incremento<0 ) return false;

						centrals = $("#offerte_hp dl.centrale");
						
						//centrals.find("a img").animate({width:"50px", height:"50px"}, 500, function (){centrals.find("dt.dt_ico").attr("style","");  });
						//centrals.find("dt.dt_ico").animate({width:"50px",height:"50px"},function (){ centrals.removeClass("centrale"); centrals.find("dt.dt_ico").attr("style","");} );
						centrals.find("dt").not(".dt_ico").animate({width:"200px"});
						centrals.find("dl.centrale dd").animate({width:"200px"});
			   }
				   },
			   onSeek: function(){
							var i, c;
							if( ( 1==0 ) && ( ! $.browser.msie ) ){
								i = 1 + ws_offerte_hp.incremento + this.getIndex();
								if (i==0) {i = 1; return false; }
								if (i==$(".items dl").size()){ i = 1; ws_offerte_hp.run = false;} 
								
								$("#offerte_hp dl:eq("+i+")").addClass("centrale");
								$("#offerte_hp dl:eq("+i+") dt").not(".dt_ico").animate({width:"170px"});
								$("#offerte_hp dl:eq("+i+") dd").animate({width:"170px"});
								$("#offerte_hp dl:eq("+i+") dt.dt_ico").animate({width:"80px", height:"80px", border: "none"});
								$("#offerte_hp dl:eq("+i+") a img").animate({width:"70px", height:"70px"}, 500);
							}
				   }
				});
		//$("#offerte_hp").hover(function(){ws_offerte_hp.run=false}, function(){ws_offerte_hp.run=window.setTimeout(ws_offerte_hp.cicla,3000);});
		/*
		
		.prev(1, function() {
					ws_offerte_hp.run = window.setTimeout(ws_offerte_hp.cicla,3);
					alert(ws_offerte_hp.run);
				})
		*/
		/*
		$("#offerte_hp div.scrollable").scrollable().prev(10, function() {
				ws_offerte_hp.run = window.setTimeout(ws_offerte_hp.cicla,3);
		});
		*/
		$("#btn_apri_google_map").not("[rel*=|]").remove(); // se mancano le coordinate viene rimosso il link
		$("#btn_apri_google_map").toggle(function(){
				var llz, ar_llz;
				llz = $(this).attr("rel")
				if(llz.length>3){
					$("#div_google_map").html("");
					ar_llz = llz.split("|");

					function carica_mappa(){
						if($("#div_google_map").size()>0){
							ws_load_google_map(ar_llz[0], ar_llz[1], parseInt(ar_llz[2]));
						}
					}
					window.setTimeout(carica_mappa,300);
				
			  	//$("#div_google_map").fadeIn('fast');
					$("#div_google_map").slideDown('fast');
					$(this).html('Chiudi Mappa');
				}else{
					return false;	
				}
			}, function(){
//				$("#div_google_map").fadeOut("fast");
				$("#div_google_map").slideUp("fast");
				$(this).html('Vedi Mappa');
			});
			$("a.togglechat").click(function(){$(".chat_client").slideToggle();return false;});
		if($.fancybox){$('.fancybox').fancybox();}
		gallery_nothick();
		ws_menu_apri_chiudi("#menu_apri_chiudi");
		ws_menu_top_slide("#menu");
		ws_offerte_hp.inizia_ciclo();
		$('ul.statico a[href$=' + location.search + ']').addClass('on');
		//modifica_filtri_ricerca();
		/* TEMPORANEAMENTE la form deve reindirizzare alla vecchia area riservata */
//		$("#agenzie_hp, #agenzie_hp input").click(function(){window.location.href='/prenota.asp'; return false;});
//		$("#agenzie_hp input").focus(function(){window.location.href='/prenota.asp'; return false;});
//		$("#agenzie_hp form").attr('action', '/prenota.asp');
	}); // fine document.ready

	function modifica_filtri_ricerca(){
		$('#modifica_filtri_ricerca').click(function(){
				if(String(document.referrer).match(location.hostname+'/soggiorni.asp')){
					history.go(-1);
				}else{
					location.href='/soggiorni.asp';
				}
			return false;
		});
	}

	ws_offerte_hp = {
		run: true,
		cicla: function (){if (! (ws_offerte_hp.run==false)){$("#offerte_hp div.scrollable").scrollable().next();  ws_offerte_hp.run = window.setTimeout(ws_offerte_hp.cicla,4500)};},
		incremento: 1,
		inizia_ciclo: function(){ws_offerte_hp.run = window.setTimeout(ws_offerte_hp.cicla,4500)}
		}
ws_news_hp = {
		run: true,
		cicla: function (){if (! (ws_news_hp.run==false)){$("#news_hp div.scrollable").scrollable().next();  ws_news_hp.run = window.setTimeout(ws_news_hp.cicla,5000)};},
		incremento: 1,
		inizia_ciclo: function(){ws_news_hp.run = window.setTimeout(ws_news_hp.cicla,4500)}
		}
/* MENU APRI CHIUDI
-------------------------------------------*/
function ws_menu_apri_chiudi(contenitore){
		$("ul.ul_reserved_area li a[href='#']").each(function (){
				if($(this).next("ul").size()==0)$(this).parent().remove();
			});
		$(contenitore + " ul li a.on").parents("ul").addClass("on");
		$(contenitore + " ul li a.on").parents("li").children('a').addClass("on");

		$(contenitore + " ul li a.on").next("ul").addClass("on");
		$(contenitore + " ul li ul").not(".on").hide();
		$(contenitore + " ul li a[href='#']").click(function(){
				$(contenitore + " ul li ul.on").not($(this).parents("ul")).not($(this).next("ul")).removeClass("on").slideUp("fast");
				$(this).next().toggleClass("on");
				$(this).next().slideToggle("fast");
				return false;
			});
	}

function ws_menu_top_slide(contenitore){
		$(contenitore + " ul li").each(function(){
				if($(this).children("ul").size()==1){
					$(this).hover(function(){
							var ul;
							ul = $(this).children("ul");
							$(ul).fadeIn("fast");
/*							if( typeof $(ul)[0].state !== "boolean" ){
								$(ul)[0].state = true;
								
							}else {
								$(ul)[0].state = !$(ul)[0].state;
							}
							if ($(ul)[0].state){
								$(ul).fadeIn("fast");
							}else{
								$(ul).fadeOut("fast"); // hide
							}*/
							return false;
						});	
				}
			});
		
			$(contenitore + " ul li ul *").hover(function(){
													  $(this).parents("ul")[0].can_close=false;
													},
												 function(){
													 $(this).parents("ul")[0].can_close=true;
													});
			$(contenitore + " ul li ul").hover(function(){
														$(this)[0].can_close=false;
													},
												function(){
													if($(this)[0].can_close==true){
														self.status = ($(this)[0].can_close);
														$(this).fadeOut('fast');
													}
												});
		
	}

/* GALLERY NOTHICK
-------------------------------------------*/
function gallery_nothick(){
	var tg, gscreen, current,top, playing;
	tg = $("a.nothickbox");
	top = $(tg).size()-1;
	current=0;
	playing=0;
	var partenza = false;
	gscreen = $("#galleria_img_zoom img"); //img.gallery_screen
	$(tg).click(function(){
						 	$(gscreen).attr("src",this.href); 
							current=$(tg).index(this); 
							$(tg).removeClass("current_img");
							$(this).addClass("current_img");
							return false;
							});
	$("a.gprevious").click(function(){
				var i, ni;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current>0){
					i=current-1;
				}else{
					i=top;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("a.gnext").click(function(){
				var ni,i;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current<top){
					i=current+1;
				}else{
					i=0;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("body").append("<img src='" + $(tg).eq(0).attr("href") + "' alt='' class='hidden_loader' style='display:none' /> ");
//	$("body img.hidden_loader").eq(0).load(function(){$(gscreen).attr("src",$(this).attr("src")); playNext();});
	nothick_playNext = function(){
		if(playing==1){
			if (current<=top){
					//$(gscreen).attr("src",$(tg).eq(current));
					$(tg).removeClass("current_img");
					current++;
					if (current>top){current=0;}
					$("body img.hidden_loader").eq(0).attr("src",$(tg).eq(current).attr("href"));
					$(tg).eq(current).addClass("current_img");
			}

			partenza=window.setTimeout(nothick_playNext,5000);
		}else{
			partenza=false;
		}
	}
	nothick_play=function(){
			playing=1;
			$("body img.hidden_loader").eq(0).load(activePlay);
			$(".nothick_play").removeClass("on");
			$(".nothick_stop").addClass("on");
			nothick_playNext();
			return false;
		}
	nothick_stop=function(){
			playing = 0;
			partenza = false;
			$("body img.hidden_loader").eq(0).unbind("load");
			$(".nothick_play").addClass("on");
			$(".nothick_stop").removeClass("on");
			return false;		
		}
	activePlay = function(){
		var ni=$(this).attr("src");
		$(gscreen).fadeOut("fast",function(){$(gscreen).attr("src",ni)}).fadeIn("fast");

	}
	$(gscreen).click(function(){if (playing==0){nothick_play();}else{nothick_stop()}});
	$(".nothick_play").click(nothick_play);
	$(".nothick_stop").click(nothick_stop);
}

		function ws_load_google_map(val1, val2, valzoom) 
		{	
			if (GBrowserIsCompatible()) 
			{
				function myGCopyright(a,b,c,d) 
				{
				this.id = a;
				this.minZoom = c;
				this.bounds = b;
				this.text = d;
				}
		
				var map = new GMap2(document.getElementById("div_google_map"));
				map.addControl(new GLargeMapControl());	 
				map.addControl(new GMapTypeControl());
				map.addControl(new GOverviewMapControl(new GSize(120,120)),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(10,10))); 
				map.setCenter(new GLatLng(val1,val2), valzoom);
	
				var icon = new GIcon();
				icon.image = "/bo/images/googlemaps_arrow.gif";
				icon.iconSize = new GSize(23, 34);
				//icon.shadowSize = new GSize(22, 20);
				icon.iconAnchor = new GPoint(12, 35);
				icon.infoWindowAnchor = new GPoint(5, 1);
				var point = new GLatLng(val1, val2);
				map.clearOverlays();
				map.addOverlay(new GMarker(point, icon));
				
			} //end if
		} // end function
		
function goPagina(npag, formname)
{
	document.getElementById('pag').value=npag;  //questa non funziona in Pocket PC
	//eval('document.THEFORM.pag').value = npag;
	document.form1.submit();
}

function scrollUP(){
	window.scroll(0,0);
	}
	function today(sep){
		var d;
		d = new Date();
		
		return $.datepicker.formatDate("dd"+sep+"mm"+sep+"yy", d);
		}
	function tomorrow(sep){
		var d;
		d = new Date();
		d.setTime(d.getTime() + (1000*3600*24));
		return $.datepicker.formatDate("dd"+sep+"mm"+sep+"yy", d);
		}
