var set_hash_executing=0;
/* CONSTANTS */
var URL_SITE="www.keyou.it";
//var URL_SITE="localhost";
var HTTP_URL_SITE="http://" + URL_SITE + "/";
var HTTP_IMAGES_ROOT="http://" + URL_SITE + "/img/";

var NAVBAR_CATEGORY_PAGENAMES=new Array("hotel","bed-and-breakfast","appartamenti","affittacamere","residence","agriturismo","guest-house","ville","altri-alloggi","inn","hostal","pensione","studio");

var SEARCH_LENGTH_LIMIT=2;
var SEARCH_LENGTH_LIMIT_ERROR="Cercare parole di almeno " + (SEARCH_LENGTH_LIMIT+1) + " caratteri";
var SEARCH_DEFAULT_TEXT="Comune o provincia o regione o attrazione";

var CITY_HEAD_HOTELFOUND=" nella Mappa";
var CITY_HEAD_STRUCTURETYPES= new Array('Alloggi Trovati', 'Hotel Trovati', 'Bed & Breakfast Trovati', 'Appartamenti Trovati', 'Affittacamere Trovati', 'Residence Trovati', 'Agriturismo Trovati','Guest House Trovate','Ville Trovate','Altri Alloggi Trovati','Inn Trovati','Hostal Trovati','Pensioni Trovate','Studio Trovati');
var CITY_HOTEL_PRICEFROM="da: ";

var HOTEL_HOTELLIST_PRICEFROM="da: ";

var CITY_SERVICES_CLEAN='Rimuovi servizi scelti';

var HOMEPAGE_SEARCH_DEFAULT_TEXT='Cittą, via, museo, altro...';

var ATTRACTION_TEXT_MORE="Continua";

var ATTRACTION_LINK_HIDEALL="Nascondi";
var ATTRACTION_LINK_SHOWALL="Vedi Tutti";

var KYA_FILTER_SERVICES_ID= new Array(31,24,5,74,35,11,16,130,7,88); //array id for shown services, city_hash

//Map constants
var MAP_ROADMAP_CAPTION="Mappa";
var MAP_SATELLITE_CAPTION="Satellite";
var MAP_HYBRID_CAPTION="Ibrida";
var MAP_ZOOM_BARLEVEL= new Array(7,8,10,12,14,16,18,19); // default zoom level for each zoom bar
var MAP_PAGING_NEXT="Altri Hotel ";
var MAP_PAGING_PREV="Indietro ";

var NAVBAR_CATEGORIES= new Array('Hotel', 'Hotel', 'Bed & Breakfast', 'Appartamenti', 'Affittacamere', 'Residence', 'Agriturismo','Guest House','Ville','Altri Alloggi','Inn','Hostal','Pensioni','Studio');

var previous_search="";
/*           */

function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
} 

function in_array(thaArray, element){
 var res=false;
  for(var e=0;e<thaArray.length;e++){
     if(thaArray[e] == element){
       res=true;
       break;
     }
   }
 return res;
}

function getSelected(select) {
	//Ritorna il valore dell'elemento <option> selezionato in una lista
	return select.options[select.selectedIndex].value;
}

function GetXMLHttpRequest() {
	if (window.XMLHttpRequest) {
	// code for IE7+, Firefox, Chrome, Opera, Safari
	return new XMLHttpRequest();
	}
	if (window.ActiveXObject)  {
	// code for IE6, IE5
	return new ActiveXObject("Microsoft.XMLHTTP");	}
	return null;
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function homepage_mapbutton_click(button,latitude,longitude,zoom,button_number){
	//clears map search text
	document.homepage_search_form.search_string.value=HOMEPAGE_SEARCH_DEFAULT_TEXT; 
	$("#homepage_search_text").css('color','#b9b9b9');
	map.clearSearchMarker();
	//removes existing popup div
	delete_popup_div("homepage_map_popupdiv");
	change_map_center(latitude,longitude,zoom);
	markers_map(latitude,longitude,1,0);
	change_active_homepage_button(button,button_number);
}

function change_active_homepage_button(button,button_number){
	for (var i=0;i<button_number;i++) {
		$('#homepage_map_menuvoice_l'+i).removeClass("homepage_map_menuvoice_left_sel");
		$('#homepage_map_menuvoice_t'+i).removeClass("homepage_map_menuvoice_text_sel");
		$('#homepage_map_menuvoice_r'+i).removeClass("homepage_map_menuvoice_right_sel");
		$('#homepage_map_menuvoice_l'+i).removeClass("homepage_map_menuvoice_left");
		$('#homepage_map_menuvoice_t'+i).removeClass("homepage_map_menuvoice_text");
		$('#homepage_map_menuvoice_r'+i).removeClass("homepage_map_menuvoice_right");
		if (i==button) {
			$('#homepage_map_menuvoice_l'+i).addClass("homepage_map_menuvoice_left_sel");
			$('#homepage_map_menuvoice_t'+i).addClass("homepage_map_menuvoice_text_sel");
			$('#homepage_map_menuvoice_r'+i).addClass("homepage_map_menuvoice_right_sel");
		} else {
			$('#homepage_map_menuvoice_l'+i).addClass("homepage_map_menuvoice_left");
			$('#homepage_map_menuvoice_t'+i).addClass("homepage_map_menuvoice_text");
			$('#homepage_map_menuvoice_r'+i).addClass("homepage_map_menuvoice_right");
		}
	}
}

function homepage_ini_attractionscities(button,button_number,city_text){
	for (var i=0;i<button_number;i++) {
		$("#homepage_attractions_image_"+i).fadeTo(1,1);
		$("#homepage_attractions_image_"+i).bind('mouseover',function(){$(this).fadeTo('fast',0.4);});
		$("#homepage_attractions_image_"+i).bind('mouseout',function(){$(this).fadeTo('fast',1);});
		
		/*if (i == button) {
			document.getElementById("homepage_attractions_caption_cityname").innerHTML=city_text;
			//homepage_attractions_createlist(city_id,city_pagename);
		}*/
		//$("#homepage_attractions_image_"+i).bind('mouseover',function(){$(this).fadeTo('fast',1);});
	}
}

function homepage_attractions_city_click(city_id,city_text,city_pagename) {
	document.getElementById("homepage_attractions_caption_cityname").innerHTML=city_text;
	homepage_attractions_createlist(city_id,city_pagename);
}

function homepage_attractions_createlist(city_id,city_pagename){
	var ajaxRequest=GetXMLHttpRequest();
	//empty all the lists
	$('ul#homepage_attractions_list1').empty();
	$('ul#homepage_attractions_list2').empty();
	$('ul#homepage_attractions_list3').empty();
	//retrivies attractions data
	var queryString = "?city_id=" + city_id;
	//alert(queryString);
	ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_homepage_attractionslist.php" + queryString, true);
	//alert(HTTP_URL_SITE + "inc/ajax_homepage_attractionslist.php" + queryString);
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){		
			/*// show sql for debugging
			var xmlDoc = ajaxRequest.responseText;
			alert(xmlDoc);*/
			var xmlDoc = ajaxRequest.responseXML;
			var attractiondata = xmlDoc.getElementsByTagName("attraction");
			for (var i=0 ; i<attractiondata.length ; i++){
				var attraction = attractiondata[i];
				var attraction_id = attraction.getElementsByTagName("id")[0].firstChild.nodeValue;
				var attraction_name = attraction.getElementsByTagName("name")[0].firstChild.nodeValue;
				var attraction_pagename = attraction.getElementsByTagName("pagename")[0].firstChild.nodeValue;
				//create list text
				link = '<a href="http://www.keyou.it/hotel/' + city_pagename + '-c' + city_id + '/' + attraction_pagename + '-a' + attraction_id + '.html">' + attraction_name + '</a>';
				//chosing appropriate list
				if (i>=0) ul_id="homepage_attractions_list1";
				if (i>=10) ul_id="homepage_attractions_list2";
				if (i>=20) ul_id="homepage_attractions_list3";
				$('<li>' + link + '</li>').appendTo('ul#'+ul_id);
			} //END of [for (var i=0 ; i<hoteldata.length ; i++)]
		}// END of [if(ajaxRequest.readyState == 4)]
	};	
	ajaxRequest.send(null);
}

function homepage_mapsearch(search_text,zoom,error){
	change_active_homepage_button(100,6);
	//removes existing popup div
	remove_popup_div("homepage_map_popupdiv");
	remove_popup_div("homepage_map_popupdiv_click");
	if (trim(search_text)!=""){
		var ajaxRequest=GetXMLHttpRequest();
		var queryString = "?searchtext=" + search_text;
		ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_homepage_locationsearch.php" + queryString, true);
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var coords = ajaxRequest.responseText;
				if (coords.length>0) {
					if (coords=="[ERR]") alert (error);
					else {
						var coords_array = coords.split("#");
						if (zoom==0) zoom=parseInt(coords_array[2]);
						change_map_center(coords_array[0],coords_array[1],zoom);
						map.clearSearchMarker();				
						setTimeout("markers_map("+coords_array[0]+","+coords_array[1]+",1,0)",100);
						add_map_searchmarker(coords_array[0],coords_array[1],search_text);
					}
				}
			}
		};
		ajaxRequest.send(null);
	}	
	return false;
}

function Adjust(X,Y,x,y,z,w) {
	var offset=268435456;
	var radius=offset/Math.PI;

	function LToX(x) {
		return Math.round(offset+radius*x*Math.PI/180);
	}
	function LToY(y) {		return Math.round(offset-radius*Math.log((1+Math.sin(y*Math.PI/180))/(1-Math.sin(y*Math.PI/180)))/2);
	}
	function XToL(x) {
		return ((Math.round(x)-offset)/radius)*180/Math.PI;
	}
	function YToL(y) {
		return (Math.PI/2-2*Math.atan(Math.exp((Math.round(y)-offset)/radius)))*180/Math.PI;
	}
	if (w) {
		return {x:(LToX(X)-LToX(x))>>(21-z),y:(LToY(Y)-LToY(y))>>(21-z)};
	} else {
		return {x:XToL(LToX(x)+(X<<(21-z))),y:YToL(LToY(y)+(Y<<(21-z)))};
	}
}

function XYToLL(X,Y,x,y,z){return Adjust(X,Y,x,y,z,0)}//	X = X pixel offset of new map center from old map center//	Y = Y pixel offset of new map center from old map center//	x = Longitude of map center//	y = Latitude  of map center//	z = Zoom level//	result.x = Longitude of adjusted map center//	result.y = Latitude  of adjusted map center

function LLToXY(X,Y,x,y,z){return Adjust(X,Y,x,y,z,1)}//	X = Longitude of marker center//	Y = Latitude  of marker center//	x = Longitude of map center//	y = Latitude  of map center//	z = Zoom level//	result.x = X pixel offset of marker center from map center//	result.y = Y pixel offset of marker center from map center

function region_citylist_more(province_id){
	var ajaxRequest=GetXMLHttpRequest();
	var queryString = "?province_id=" + province_id;
	
	$("#region_provincelist_city" + province_id + "list1").empty();
	$("#region_provincelist_city" + province_id + "list2").empty();
	
	ajaxRequest.open("GET", HTTP_URL_SITE+"inc/ajax_region_citylist.php" + queryString, true);
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var xmlDoc = ajaxRequest.responseXML;
			var citydata = xmlDoc.getElementsByTagName("city");
			var half = citydata.length / 2;
			for (var i=0 ; i<citydata.length ; i++){
				var city = citydata[i];
				var city_id = city.getElementsByTagName("id")[0].firstChild.nodeValue;
				var city_name = city.getElementsByTagName("name")[0].firstChild.nodeValue;
				var city_pagename = city.getElementsByTagName("pagename")[0].firstChild.nodeValue;
				var city_hotelnumber = city.getElementsByTagName("hotelnumber")[0].firstChild.nodeValue;
				//create list text
				link = '<a href="http://www.keyou.it/hotel/' + city_pagename + '-c' + city_id + '/">' + city_name + ' <span>(' + city_hotelnumber + ')</span></a>';
				//chosing appropriate list
				if (i>=0) ul_id="region_provincelist_city" + province_id + "list1";
				if (i>=half) ul_id="region_provincelist_city" + province_id + "list2";
				$('<li>' + link + '</li>').appendTo('ul#'+ul_id);
			}
		}
	};

	$("#region_provincelist_more" + province_id).hide("fast");
	
	ajaxRequest.send(null);
}

function city_mapsmall_extend(maphotels){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#city_mapbig_lock').attr('checked', false);
	$("#threecols_city_mapbig").show();
	$("#threecols_city_mapbig_clear").show();
	ini_map('city_big',lat,lng,zoom,maphotels);
	setTimeout('city_markers_map('+lat+','+lng+',1,0)', 100);
	$("#city_map_mapbg").hide();
	
}

function city_mapbig_collapse(maphotels){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#city_mapsmall_lock').attr('checked', false);
	$("#city_map_mapbg").show();
	ini_map('city_small',lat,lng,zoom,maphotels);
	setTimeout('city_markers_map('+lat+','+lng+',1,0)', 100);
	$("#threecols_city_mapbig").hide();
	$("#threecols_city_mapbig_clear").hide();
}

function attraction_mapsmall_extend(maphotels,attraction_lat,attraction_lng,attraction_name){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#city_mapbig_lock').attr('checked', false);
	$("#threecols_city_mapbig").show();
	$("#threecols_city_mapbig_clear").show();
	ini_map('city_big',lat,lng,zoom,maphotels);
	setTimeout('city_markers_map('+lat+','+lng+',1,0)', 100);
	add_map_searchmarker(attraction_lat,attraction_lng,attraction_name);
	$("#city_map_mapbg").hide();
	
}

function attraction_mapbig_collapse(maphotels,attraction_lat,attraction_lng,attraction_name){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#city_mapsmall_lock').attr('checked', false);
	$("#city_map_mapbg").show();
	ini_map('city_small',lat,lng,zoom,maphotels);
	setTimeout('city_markers_map('+lat+','+lng+',1,0)', 100);
	add_map_searchmarker(attraction_lat,attraction_lng,attraction_name);
	$("#threecols_city_mapbig").hide();
	$("#threecols_city_mapbig_clear").hide();
}

function hotel_mapsmall_extend(maphotels,attraction_lat,attraction_lng,attraction_name){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#hotel_mapbig_lock').attr('checked', false);
	$("#twocols_hotel_mapbig").show();
	$("#twocols_hotel_mapbig_clear").show();
	ini_map('hotel_big',lat,lng,zoom,maphotels,map.ky_shown_hotel_id);
	setTimeout('hotel_markers_map('+lat+','+lng+',0)', 100);
	add_map_selhotelmarker(attraction_lat,attraction_lng,attraction_name);
	$("#hotel_map_mapbg").hide();
	
}

function hotel_mapbig_collapse(maphotels,attraction_lat,attraction_lng,attraction_name){
	var lat=map.getCenter().lat();
	var lng=map.getCenter().lng();
	var zoom=map.getZoom();
	
	$('#hotel_mapsmall_lock').attr('checked', false);
	$("#hotel_map_mapbg").show();
	ini_map('hotel_small',lat,lng,zoom,maphotels,map.ky_shown_hotel_id);
	setTimeout('hotel_markers_map('+lat+','+lng+',0)', 100);
	add_map_selhotelmarker(attraction_lat,attraction_lng,attraction_name);
	$("#twocols_hotel_mapbig").hide();
	$("#twocols_hotel_mapbig_clear").hide();
}

function city_map_lockresults(name,from){
	if (from=="a") {
		if ($('#'+name).is(':checked') == true) $('#'+name).attr('checked', false);
		else $('#'+name).attr('checked', true);
	}
	
	if ($('#'+name).is(':checked') == true) {
		map.ky_lockresults=1;
	} else {
		map.clearMarkers();
		var newcenter=map.getCenter();
		var newlatitude=newcenter.lat();
		var newlongitude=newcenter.lng();
		
		//removes existing popup div
		remove_popup_div("homepage_map_popupdiv");
		remove_popup_div("homepage_map_popupdiv_mouseclick");
		
		setTimeout("city_markers_map("+newlatitude+","+newlongitude+",1,0)", 100);
		
		map.ky_lockresults=0;
	}
}

function hotel_map_lockresults(name,from){
	if (from=="a") {
		if ($('#'+name).is(':checked') == true) $('#'+name).attr('checked', false);
		else $('#'+name).attr('checked', true);
	}
	
	if ($('#'+name).is(':checked') == true) {
		map.ky_lockresults=1;
	} else {
		map.clearMarkers();
		var newcenter=map.getCenter();
		var newlatitude=newcenter.lat();
		var newlongitude=newcenter.lng();
		
		//removes existing popup div
		remove_popup_div("homepage_map_popupdiv");
		remove_popup_div("homepage_map_popupdiv_mouseclick");
		
		setTimeout("hotel_markers_map("+newlatitude+","+newlongitude+",0)", 100);
		
		map.ky_lockresults=0;
	}
}

function city_paging(latitude,longitude,page){
	map.ky_mapbig_paging=page;
	update_map_hash();
	
	scroll(0,0);
}

function city_paging_next(){
	if (map.ky_mapbig_paging+1 <= map.ky_mapbig_maxpages){
		map.ky_mapbig_paging=map.ky_mapbig_paging+1;
		update_map_hash();
	}
}
function city_paging_prev(){
	if (map.ky_mapbig_paging>1) {
		map.ky_mapbig_paging=map.ky_mapbig_paging-1;
		update_map_hash();
	}
}

function city_filter_category(category_id) {
	map.hotel_filter_category=category_id;
	map.ky_mapbig_paging=1;
	update_map_hash();

	
	scroll(0,160);
}

function city_filter_price (price_id) {
	if(map.hotel_filter_price==0) map.hotel_filter_price=price_id;
	else {
		var hotel_filter_price= new String(map.hotel_filter_price);
		var existing_filters = hotel_filter_price.split("-");
		var flag_removed=0;
		var newfilter= new String;
		for (var i=0;i<existing_filters.length;i++) {
			if (existing_filters[i]==price_id) flag_removed=1;
			else newfilter=newfilter+existing_filters[i]+"-";
		}
		if (flag_removed==0) newfilter=newfilter+price_id+"-";

		newfilter=newfilter.substring(0,newfilter.length-1);

		if (newfilter=="") newfilter=0;
		
		map.hotel_filter_price=newfilter;
	}
	map.ky_mapbig_paging=1;
	update_map_hash();
}

function city_filter_stars (stars_id) {
	if(map.hotel_filter_stars==0) map.hotel_filter_stars=stars_id;
	else {
		var hotel_filter_stars= new String(map.hotel_filter_stars);
		var existing_filters = hotel_filter_stars.split("-");
		var flag_removed=0;
		var newfilter= new String;
		for (var i=0;i<existing_filters.length;i++) {
			if (existing_filters[i]==stars_id) flag_removed=1;
			else newfilter=newfilter+existing_filters[i]+"-";
		}
		if (flag_removed==0) newfilter=newfilter+stars_id+"-";

		newfilter=newfilter.substring(0,newfilter.length-1);

		if (newfilter=="") newfilter=0;
		
		map.hotel_filter_stars=newfilter;
	}
	map.ky_mapbig_paging=1;
	update_map_hash();
}

function city_filter_services (service_id) {
	if(map.hotel_filter_services==0) map.hotel_filter_services=service_id;
	else {
		var hotel_filter_services= new String(map.hotel_filter_services);
		var existing_filters = hotel_filter_services.split("-");
		var flag_removed=0;
		var newfilter= new String;
		for (var i=0;i<existing_filters.length;i++) {
			if (existing_filters[i]==service_id) flag_removed=1;
			else newfilter=newfilter+existing_filters[i]+"-";
		}
		if (flag_removed==0) newfilter=newfilter+service_id+"-";

		newfilter=newfilter.substring(0,newfilter.length-1);

		if (newfilter=="") newfilter=0;
		
		map.hotel_filter_services=newfilter;
	}
	map.ky_mapbig_paging=1;
	update_map_hash();
}

function city_sort(filter_type) {

	var current_sort_type=map.hotel_sort_type.substr(0,1);
	var current_sort_style=map.hotel_sort_type.substr(1,1);
	
	var new_sort_type=new String(filter_type);
	
	if (current_sort_type==filter_type) {
		if (current_sort_style=="a") {
			new_sort_type=new_sort_type+"d";
			$("#city_hotel_sort_image"+filter_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_down.png");
		} else {
			new_sort_type=new_sort_type+"a";
			$("#city_hotel_sort_image"+filter_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_up.png");
		}
	} else {
		switch (filter_type) {
			case 1: new_sort_type=new_sort_type+"d";$("#city_hotel_sort_image"+filter_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_down.png");break;
			case 2: new_sort_type=new_sort_type+"a";$("#city_hotel_sort_image"+filter_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_up.png");break;
			case 3: new_sort_type=new_sort_type+"a";$("#city_hotel_sort_image"+filter_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_up.png");break;
		}
	}
	
	switch (current_sort_type) {
		case 1: $("#city_hotel_sort_image"+current_sort_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_down.png");break;
		case 2: $("#city_hotel_sort_image"+current_sort_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_up.png");break;
		case 3: $("#city_hotel_sort_image"+current_sort_type).attr("src",HTTP_IMAGES_ROOT+"sort_arrow_up.png");break;
	}
	
	map.hotel_sort_type=new_sort_type;
	
	map.ky_mapbig_paging=1;
	update_map_hash();
}

function hotel_onload(hotel_id,text_check,partner_id){

	if (text_check==1) {
		var ajaxRequest=GetXMLHttpRequest();
		
		//retrivies attractions data
		var queryString = "?hotel_id=" + hotel_id;
		//alert(queryString);
		ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_hotel_textcheck.php" + queryString, true);
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){		
				var hotel_text = ajaxRequest.responseText;

				if (hotel_text!="#CORRECT#") document.getElementById("hotel_body_description").innerHTML=hotel_text;
			}// END of [if(ajaxRequest.readyState == 4)]
		};	
		ajaxRequest.send(null);
		
		//fading gallery images
		$(".hotel_gallery_image").bind('mouseover',function(){$(this).fadeTo('fast',0.4);});
		$(".hotel_gallery_image").bind('mouseout',function(){$(this).fadeTo('fast',1);});
	}
	
	//checks if the cookie for rooms' disponibility check is enabled, if it is, it shows disponible rooms
	var booking_cookie=Get_Cookie("kyhsdata");
	if (String(window.location.hash)=="#book"){
		if (booking_cookie)
			hotel_check_disponibility(partner_id,hotel_id,hotel_id);
	}
}

function  hotel_gallery_slide(movement,images_number){
	var slider_limit=(images_number*100)-500;
	var current_margin=$("#hotel_gallery_slider").css("margin-left");
	new_margin=new String((parseInt(current_margin)+movement)+"px");
	if (Math.abs(parseInt(new_margin))<slider_limit && parseInt(new_margin)<=0)	$("#hotel_gallery_slider").animate({marginLeft:new_margin}, 200, "linear");
}


function hotel_gallery_loadimage(image){
	$("#hotel_body_imagefile").attr("src", image);
}

function SearchFormSubmit(){
	if (CheckSearchForm()) {
		document.forms['leftbar_search_form'].submit();
	}
}

function CheckSearchForm(){
	var search_string=new String(document.leftbar_search_form.search_string.value);

	if (trim(search_string) == "" || trim(search_string)==SEARCH_DEFAULT_TEXT) 
		return false;
		
	return true;
}

function inizialize_search_box(){
	if (document.leftbar_search_form.search_string.value==SEARCH_DEFAULT_TEXT) {
		document.leftbar_search_form.search_string.value="";
	}
	$("#main_search_box").css('color','#525252');
}

function inizialize_homepage_searchbox(){
	if (document.homepage_search_form.search_string.value==HOMEPAGE_SEARCH_DEFAULT_TEXT) {
		document.homepage_search_form.search_string.value="";
		$("#homepage_search_text").css('color','#525252');
	}
}

function reinizialize_homepage_searchbox(){
	
	if (document.homepage_search_form.search_string.value=="") {
		document.homepage_search_form.search_string.value=HOMEPAGE_SEARCH_DEFAULT_TEXT;
		$("#homepage_search_text").css('color','#b9b9b9');
	}
}

function reinizialize_search_box(){
	setTimeout('$(\'#search_autocomplete_div\').hide()',400);
	if (document.leftbar_search_form.search_string.value=="") {
		document.leftbar_search_form.search_string.value=SEARCH_DEFAULT_TEXT;
		$("#main_search_box").css('color','#b9b9b9');
	}
}


function city_inizialize(category_id){
	map.hotel_filter_category=category_id+1;
}

function update_map_hash(){		
	window.location.hash="pag"+map.ky_mapbig_paging+"|fpr"+map.hotel_filter_price+"|fst"+map.hotel_filter_stars+"|fsv"+map.hotel_filter_services+"|fct"+map.hotel_filter_category+"|stp"+map.hotel_sort_type+"|mlt"+map.getCenter().lat()+"|mln"+map.getCenter().lng()+"|mzm"+map.getZoom();
}

function city_onload(latitude,longitude){
	if (window.location.hash && String(window.location.hash)!="#"){
		$(window).trigger( 'hashchange' );
	} else {
		setTimeout('city_markers_map('+latitude+','+longitude+',1,1)', 100);
		change_map_center(latitude,longitude,map.getZoom());
	}
}

function attraction_onload(latitude,longitude){
	if (window.location.hash){
		$(window).trigger( 'hashchange' );
	} else {
		setTimeout('city_markers_map('+latitude+','+longitude+',1,1)', 100);
		change_map_center(latitude,longitude,map.getZoom());
	}
}

function ie_label_fix(destination){
	var browserName=navigator.appName; 

	if (browserName=="Microsoft Internet Explorer") {
		document.getElementById("city_filter_stars_"+destination).checked=!document.getElementById("city_filter_stars_"+destination).checked;
		city_filter_stars(destination);
	}
}

function attraction_text_more(){
	$("#attraction_head_text_show_href").hide();
	$("#attraction_head_text_hide").show();
}

function attraction_text_hide(){
	$("#attraction_head_text_hide").hide();
	$("#attraction_head_text_show_href").show();	
}

function city_ini_mapid(city_id){
	map.ky_city_id=city_id;
}

function attraction_ini_mapid(attraction_id){
	map.ky_attraction_id=attraction_id;
}

function city_showallattractions(category_id,current_attraction_id,field_type,field_value,reverse) {
	var ajaxRequest=GetXMLHttpRequest();
	
	element_height=$("#city_rightbar_attracioncontainer_"+category_id).height();
	
	var queryString="?category_id="+ category_id +"&current_attraction_id="+current_attraction_id+"&field_type="+field_type+"&field_value="+field_value+"&reverse="+reverse;	
	ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_city_showall_attractions.php" + queryString, true);
	//alert(HTTP_URL_SITE + "inc/ajax_attraction_description.php" + queryString);
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var CompleteList = ajaxRequest.responseText;
			document.getElementById("city_rightbar_attracionul_"+category_id).innerHTML=CompleteList;
			
			$("#city_rightbar_attracioncontainer_"+category_id).css('height',element_height);
			$("#city_rightbar_attracioncontainer_"+category_id).css('overflow','auto');
			
			//$("#city_rightbar_attracionmore_"+category_id).hide();
			if (reverse==0) {
				document.getElementById("city_rightbar_attracionmore_"+category_id).innerHTML=ATTRACTION_LINK_HIDEALL;
				$("#city_rightbar_attracionmore_"+category_id).attr("href","javascript:city_showallattractions('"+category_id+"','"+current_attraction_id+"','"+field_type+"','"+field_value+"',1);");
			} else {
				document.getElementById("city_rightbar_attracionmore_"+category_id).innerHTML=ATTRACTION_LINK_SHOWALL;
				$("#city_rightbar_attracionmore_"+category_id).attr("href","javascript:city_showallattractions('"+category_id+"','"+current_attraction_id+"','"+field_type+"','"+field_value+"',0);");				
				$("#city_rightbar_attracioncontainer_"+category_id).css('overflow','hidden');
				$("#city_rightbar_attracioncontainer_"+category_id).css('height',element_height);
			}
		
		}
	};
	ajaxRequest.send(null);
}

function city_showallservices() {
	var ajaxRequest=GetXMLHttpRequest();
	
	element_height=$("#city_rightbar_servicescontainer").height();
	
	var queryString="";	
	ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_city_showall_services.php" + queryString, true);
	//alert(HTTP_URL_SITE + "inc/ajax_attraction_description.php" + queryString);
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var CompleteList = ajaxRequest.responseText;
			document.getElementById("city_rightbar_servicesul").innerHTML=CompleteList;
			
			$("#city_rightbar_servicescontainer").css('height',element_height);
			$("#city_rightbar_servicescontainer").css('overflow','auto');
			document.getElementById("city_filterbox_servicesmore").innerHTML=CITY_SERVICES_CLEAN;
			$("#city_filterbox_servicesmore").attr("href","javascript:city_cleanallservices();");
			
			//checks previous checked services
			if (map.hotel_filter_services!=0) {
				temp_services=new String(map.hotel_filter_services);
				temp_services=temp_services.split("-");
				
				for (var k=0;k<temp_services.length;k++)
					$("#city_filter_services_"+temp_services[k]).attr('checked', true);
			}
			
		}
	};
	ajaxRequest.send(null);
}

function city_cleanallservices(){
	map.hotel_filter_services=0;
	$(".cityhash_filter_services").attr('checked', false);
	update_map_hash();
}

function contactformSubmit(){
	if (Checkcontactform()) {
		document.forms['contactform'].submit();
	}
}

function Checkcontactform(){
	var name=new String(document.contactform.contact_form_name.value);
	var email=new String(document.contactform.contact_form_email.value);
	var message=new String(document.contactform.contact_form_message.value);
	var subject=new String(document.contactform.contact_form_subject.value);
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

	if (trim(name) == "") {
		alert('Specificare il nome');
		return false;
	} else if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
		alert('Inserire un indirizzo mail valido');
		return false;
	} else if (trim(subject) == "") {
		alert('Specificare l\'oggetto della richiesta');
		return false;
	} else if (trim(message) == "") {
		alert('Non hai inserito il messaggio');
		return false;
	} 
	
	return true;
}

function hotelwebSubmit(){
	if (Checkhotelweb()) {
		document.forms['hotelsubmit_form_1'].submit();
	}
}

function Checkhotelweb(){
	var website=new String(document.hotelsubmit_form_1.website.value);

	if (trim(website) == "" || trim(website.substring(0,7))!="http://" || trim(website)=="http://") {
		alert('Specificare il sito nella formula: http://www.nomesito.it');
		return false;
	}
	
	return true;
}

function update_select(table,field_value){
	var ajaxRequest=GetXMLHttpRequest();
	
	var queryString="?table="+table+"&field_value="+field_value;	
	ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_hotelsubmit_updateselect.php" + queryString, true);
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var CompleteList = ajaxRequest.responseText;
			
			while (document.getElementById(table).options.length) {
					document.getElementById(table).remove(0);
				} 
			
			elementlist=CompleteList.split("#");
			for (var i=0;i<(elementlist.length-1);i++){
				var rowdata=elementlist[i].split("|");
				addOption(document.getElementById(table), rowdata[0], rowdata[1]);
			}
				
			$("#"+table).removeAttr("disabled");
			if (table=="province") {document.getElementById("city").innerHTML="<option value=\"0\"></option>";$("#city").attr("disabled","disabled");}
		}
	};
	ajaxRequest.send(null);
}

function addOption(select, value, text) {
	//Aggiunge un elemento <option> ad una lista <select>
	var option = document.createElement("option");
	option.value = value,
	option.text = text;
	try {
		select.add(option, null);
	} catch(e) {
		//Per Internet Explorer
		select.add(option);
	}
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
	element=obj.value;
	document.getElementById("hotelform_remaining_chars").value=mlength-element.length;
	obj.scrollTop = obj.scrollHeight;
}

function hoteldataSubmit(){
	if (Checkhoteldata()) {
		document.forms['hotelsubmit_form_2'].submit();
	}
}

function Checkhoteldata(){
	var category_id=document.hotelsubmit_form_2.category_id.selectedIndex;
	var region_id=document.hotelsubmit_form_2.region_id.selectedIndex;
	var province_id=document.hotelsubmit_form_2.province_id.selectedIndex;
	var city_id=document.hotelsubmit_form_2.city_id.selectedIndex;
	var name=new String(document.hotelsubmit_form_2.name.value);
	
	var stars="";
	
	for (var i=0; i < document.hotelsubmit_form_2.stars.length; i++)
	   if (document.hotelsubmit_form_2.stars[i].checked)
		  stars = document.hotelsubmit_form_2.stars[i].value;
	
	var description=new String(document.hotelsubmit_form_2.description.value);
	var telephone=new String(document.hotelsubmit_form_2.telephone.value);
	var address=new String(document.hotelsubmit_form_2.address.value);
	var email=new String(document.hotelsubmit_form_2.email.value);
	var referent=new String(document.hotelsubmit_form_2.referent.value);

	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

	if (category_id == "" || category_id==0) {
		alert('Specificare la tipologia della struttura');
		return false;
	} else if (region_id == "" || region_id==0) {
		alert('Specificare la regione');
		return false;
	} else if (province_id == "" || province_id==0) {
		alert('Specificare la provincia');
		return false;
	} else if (city_id == "" || city_id==0) {
		alert('Specificare il comune');
		return false;
	} else if (trim(name) == "") {
		alert('Specificare il nome della struttura');
		return false;
	} else if (stars == "") {
		alert('Specificare la classificazione a stelle della struttura');
		return false;
	} else if (trim(description) == "") {
		alert('Inserire una descrizione della struttura');
		return false;
	} else if (trim(address) == "") {
		alert('Specificare l\'indirizzo della struttura');
		return false;
	} else if (trim(telephone) == "") {
		alert('Specificare il numero di telefono della struttura');
		return false;
	} else if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
		alert('Inserire un indirizzo mail valido');
		return false;
	} else if (trim(referent) == "") {
		alert('Specificare il nome e cognome del Referente');
		return false;
	} 
	
	return true;
}

function set_hash(table_id){
	$(window).bind( 'hashchange', function(){
		var first_time=0;
		set_hash_executing=1;
		if (!String(window.location.hash) || String(window.location.hash)=="#") {
			var ajaxRequest=GetXMLHttpRequest();
			if (table_id==0) var queryString="?city_id="+ map.ky_city_id+"&type="+table_id;	
			else if (table_id==1) var queryString="?city_id="+ map.ky_attraction_id+"&type="+table_id;	
			ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_cityhash_getlatlong.php" + queryString, true);
			//alert(HTTP_URL_SITE + "inc/ajax_cityhash_getlatlong.php" + queryString);
			ajaxRequest.onreadystatechange = function(){
				if(ajaxRequest.readyState == 4){
					var city_coords = ajaxRequest.responseText;
					city_coords=city_coords.split("#");
					
					var latitude=city_coords[0];
					var longitude=city_coords[1];
					var zoom=parseInt(city_coords[2]);
					var page=1;
					
					//clears checkboxes
					$(".cityhash_filter_services").attr('checked', false);
					$(".cityhash_filter_stars").attr('checked', false);
					$(".cityhash_filter_price").attr('checked', false);
					map.hotel_filter_category=0;
					map.hotel_filter_price=0;
					map.hotel_filter_stars=0;
					map.hotel_filter_services=0;
					map.hotel_sort_type="1d";
					map.ky_lockresults = 0;
					
					change_map_center(latitude,longitude,zoom);
					city_markers_map(latitude,longitude,parseInt(page),first_time);
				}
			};
			ajaxRequest.send(null);
			
		} else {
			var filters= new String(window.location.hash);
			var page=1;
			var latok=0;
			var lngok=0;
			var zoomok=0;
			
			filters=filters.substring(1,filters.length);
			
			filters=filters.split("|");
			for (var i=0;i<filters.length;i++) {
				if (filters[i].substring(0,3)=="fct") {
					map.hotel_filter_category=filters[i].substring(3,filters[i].length);
					
					if ($('#navbar_current_link').length>0) {
						var navbar_href= new String($("#navbar_current_link").attr("href"));
						var navbar_newhref = new String;
						navbar_href=navbar_href.split("/");
						for (var k=0;k<navbar_href.length;k++) { 
							if (k != 3)
								navbar_newhref=navbar_newhref+navbar_href[k]+"/";
							else {
								if (map.hotel_filter_category==0) navbar_newhref=navbar_newhref+"hotel"+"/";
								else navbar_newhref=navbar_newhref+NAVBAR_CATEGORY_PAGENAMES[(map.hotel_filter_category-1)]+"/";
							}
						}
						navbar_newhref=navbar_newhref.substring(0,navbar_newhref.length-1);
						$("#navbar_current_link").attr("href",navbar_newhref);
						document.getElementById("navbar_city_element").innerHTML=NAVBAR_CATEGORIES[map.hotel_filter_category];
					}
				}
				if (filters[i].substring(0,3)=="fpr") {
					map.hotel_filter_price=filters[i].substring(3,filters[i].length);
					var temp_price=new String (filters[i].substring(3,filters[i].length));
					temp_price=temp_price.split("-");
					var counter=1;
					while ($("#city_filter_price_"+counter).length>0){
						
						if (in_array(temp_price,counter)) $("#city_filter_price_"+counter).attr('checked', true);
						else $("#city_filter_price_"+counter).attr('checked', false);
						counter++;
					}
				}
				if (filters[i].substring(0,3)=="fst") {	
					map.hotel_filter_stars=filters[i].substring(3,filters[i].length);
					
					var temp_stars=new String (filters[i].substring(3,filters[i].length));
					temp_stars=temp_stars.split("-");
					var counter=1;
					while ($("#city_filter_stars_"+counter).length>0){
						
						if (in_array(temp_stars,counter)) $("#city_filter_stars_"+counter).attr('checked', true);
						else $("#city_filter_stars_"+counter).attr('checked', false);
						counter++;
					}
				}
				if (filters[i].substring(0,3)=="fsv") {
					map.hotel_filter_services=filters[i].substring(3,filters[i].length);
					
					var temp_services=new String (filters[i].substring(3,filters[i].length));
					temp_services=temp_services.split("-");

					//checks filters boxes
					if (temp_services[0]!=0) {
						//checks if some of the services are in the hidden list of the page
						for (var k=0;k<temp_services.length;k++) {
							if (!in_array(KYA_FILTER_SERVICES_ID,temp_services[k])){
								if (document.getElementById("city_filterbox_servicesmore").innerHTML!=CITY_SERVICES_CLEAN) {
									city_showallservices();
									k=temp_services.length;
								}
							}
						}
						//remove all checked boxes
						$(".cityhash_filter_services").attr('checked', false);
						for (var k=0;k<temp_services.length;k++)
							//alert("#city_filter_services_"+temp_services[i]);
							$("#city_filter_services_"+temp_services[k]).attr('checked', true);
					}
					
				}
				if (filters[i].substring(0,3)=="pag") page=filters[i].substring(3,filters[i].length);
				if (filters[i].substring(0,3)=="mlt") {
					var latitude=filters[i].substring(3,filters[i].length);
					latok=1;
			
				}
				if (filters[i].substring(0,3)=="mln") {
					var longitude=filters[i].substring(3,filters[i].length);
					lngok=1;
				}
				if (filters[i].substring(0,3)=="mzm") {
					var zoom=parseInt(filters[i].substring(3,filters[i].length));
					zoomok=1;
		
				}
				if (filters[i].substring(0,3)=="stp") {
					map.hotel_sort_type=filters[i].substring(3,filters[i].length);
				}
			}
			
			if (latok==0) latitude=map.getCenter().lat();
			if (lngok==0) longitude=map.getCenter().lng();
			if (zoomok==0) zoom=map.getZoom();

			map.clearMarkers();
			
			change_map_center(latitude,longitude,zoom);
			city_markers_map(latitude,longitude,parseInt(page),first_time);
		}
	
		set_hash_executing=0;
	});
}

function HotelBook(form_id){
	form_id=new String(form_id);
	form_name=new String('hotel_disponibility_'+form_id);
	document.forms[form_name].submit();
}

function hotel_check_disponibility(hotel_id,ky_hotel_id,city_id){
	var ajaxRequest=GetXMLHttpRequest();
	if ($("#ky_checkin_date").val()!="" && $("#ky_checkin_date").val()!="") {
		
		if ($('#hotel_book_result').height()>10) $('#hotel_loading_div').height($('#hotel_book_result').height()+$("#hotel_book").height());
		$('#hotel_loading_div').css('filter', 'alpha(opacity=60)'); //sets opacity for internet explorer bug
		$('#hotel_loading_div').fadeIn("fast");
		$('#hotel_loading_div_2').fadeIn("fast");
		
		var queryString="?hotel_id="+hotel_id+"&ky_hotel_id="+ky_hotel_id+"&checkin_date="+$("#ky_checkin_date").val()+"&checkout_date="+$("#ky_checkout_date").val()+"&pval="+$("#ky_pval").val()+"&rval="+$("#ky_rval").val()+"&city_id="+city_id;	
		ajaxRequest.open("GET", HTTP_URL_SITE + "inc/ajax_hotel_checkdisponibility.php" + queryString, true);
		//alert(HTTP_URL_SITE + "inc/ajax_hotel_checkdisponibility.php" + queryString);
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var CompleteList = ajaxRequest.responseText;
				
				document.getElementById("hotel_book_result").innerHTML=CompleteList;
				
				$('#hotel_loading_div').fadeOut("fast");
				if ($('#hotel_book_result').height()>10) $('#hotel_loading_div').height($('#hotel_book_result').height()+$("#hotel_book").height());
				$('#hotel_loading_div_2').fadeOut("fast");
			}
		};
		ajaxRequest.send(null);
	}
}

function hotel_check_disponibility_blank(){
	document.getElementById("hotel_book_result").innerHTML="";
	$('#hotel_loading_div').height("150px");
}

function searchbox_autocomplete(inputString) {
	$("#main_search_box").keyup(function(e){
		if (e.keyCode == 38) { 
		   //alert ($('#search_autocomplete_div_ul li').find('.search_autocomplete_li_selected').index());
		  // $(".search_autocomplete_li_selected").prev().addClass("search_autocomplete_li_selected");
		   return false;
		}
		if (e.keyCode == 40) { 
			//$(".search_autocomplete_li_selected").next().addClass("search_autocomplete_li_selected");
		   return false;
		}
	});
	if (inputString!=previous_search) {
		previous_search=inputString;
		if(inputString.length < 3) {
			// Hide the suggestion box.
			$('#search_autocomplete_div').hide();
		} else {
			$.get(HTTP_URL_SITE + "inc/ajax_search_autocomplete.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >15) {
					$('#search_autocomplete_div').show();
					$('#search_autocomplete_div').html(data);
					//$('#search_autocomplete_div li:first').addClass("search_autocomplete_li_selected");
					//$('#search_autocomplete_div li:first').focus();
				}
			});
		}
	}
/*
	$("#main_search_box").keyup(function(e){
		if (e.keyCode == 38) { 
		   $(".search_autocomplete_li_selected").prev().addClass("search_autocomplete_li_selected");
		   return false;
		}
		else if (e.keyCode == 40) { 
			$(".search_autocomplete_li_selected").next().addClass("search_autocomplete_li_selected");
		   return false;
		} else {
			
		}
	});
*/					
					
   
		
			
}
