/* Italian initialisation for the jQuery UI date picker plugin. */
/* Written by Apaella (apaella@gmail.com). */

jQuery(function(jQuery){
	jQuery.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};
	jQuery.datepicker.setDefaults(jQuery.datepicker.regional['it']);
});

jQuery(function() 
{
	jQuery('#data_arrivo, #data_partenza').datepicker({
	numberOfMonths: 3,
	showButtonPanel: true,
	minDate: 0			
	});

	jQuery('#dateButtonArr').click(function() {
	jQuery('#data_arrivo').datepicker('show');
	});

	jQuery('#dateButtonPar').click(function() {
	jQuery('#data_partenza').datepicker('show');
	});
		 
	jQuery.datepicker.setDefaults(jQuery.datepicker.regional['it']);
});

function cambiaGiorno()
{
	var ar = jQuery('#data_arrivo');
	
	var arrivo = ar[0].value.split("/");
	var d = new Date();
	var d1 = new Date();
	
	var a = parseInt(arrivo[2],10);
	var m = parseInt(arrivo[1],10) - 1;
	var g = parseInt(arrivo[0],10);

	d.setFullYear(a);
	d.setMonth(m);
	d.setDate(g);
	
	d1.setTime(d.getTime() + (60*60*24*1000));
	
	var aa = d1.getFullYear();
	var mm = d1.getMonth()+1;
	var gg = d1.getDate();
	
	if(mm<10) mm = '0'+mm;
	if(gg<10) gg = '0'+gg;

	jQuery('#data_partenza')[0].value =  gg + '/' + mm + '/' + aa;
	aggiornaDate();
}
function controllaArrivoTematico(baseUrl)
{
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	
	var d = new Date();
	var d1 = new Date();

	var arrivo = ar[0].value.split("/");
	var a = parseInt(arrivo[2],10);
	var m = parseInt(arrivo[1],10) - 1;
	var g = parseInt(arrivo[0],10);

	d.setFullYear(a);
	d.setMonth(m);
	d.setDate(g);
	
	var partenza = pr[0].value.split("/");
	var a1 = parseInt(partenza[2],10);
	var m1 = parseInt(partenza[1],10) - 1;
	var g1 = parseInt(partenza[0],10);
	d1.setFullYear(a1);
	d1.setMonth(m1);
	d1.setDate(g1);

	if(d >= d1)
	{
		d1.setTime(d1.getTime() - (60*60*24*1000));
		
		var aa = d1.getFullYear();
		var mm = d1.getMonth()+1;
		var gg = d1.getDate();

		if(mm<10) mm = '0'+mm;
		if(gg<10) gg = '0'+gg;
	
		jQuery('#data_arrivo')[0].value =  gg + '/' + mm + '/' + aa;
	}
	aggiornaDateTematico(baseUrl);
}
function cambiaGiornoTematico(baseUrl)
{
	var ar = jQuery('#data_arrivo');
	
	var arrivo = ar[0].value.split("/");
	var d = new Date();
	var d1 = new Date();
	
	var a = parseInt(arrivo[2],10);
	var m = parseInt(arrivo[1],10) - 1;
	var g = parseInt(arrivo[0],10);

	d.setFullYear(a);
	d.setMonth(m);
	d.setDate(g);
	
	d1.setTime(d.getTime() + (60*60*24*1000));
	
	var aa = d1.getFullYear();
	var mm = d1.getMonth()+1;
	var gg = d1.getDate();
	
	if(mm<10) mm = '0'+mm;
	if(gg<10) gg = '0'+gg;

	jQuery('#data_partenza')[0].value =  gg + '/' + mm + '/' + aa;
	aggiornaDateTematico(baseUrl);
}
function aggiornaDateTematico(baseUrl)
{
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	var partenza = pr[0].value;
	var arrivo = ar[0].value;
	
		var q = "arrivalDate="+arrivo+"&departureDate="+partenza;

		jQuery.ajax({
			type: "POST",
			url: "http://"+baseUrl+"/service/aggiornaDate.php",
			data: q,
			success:function aggiornaDateH(response){aggiornaDateHandler(response);}
			});
}


function controllaArrivo()
{
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	
	var d = new Date();
	var d1 = new Date();

	var arrivo = ar[0].value.split("/");
	var a = parseInt(arrivo[2],10);
	var m = parseInt(arrivo[1],10) - 1;
	var g = parseInt(arrivo[0],10);

	d.setFullYear(a);
	d.setMonth(m);
	d.setDate(g);
	
	var partenza = pr[0].value.split("/");
	var a1 = parseInt(partenza[2],10);
	var m1 = parseInt(partenza[1],10) - 1;
	var g1 = parseInt(partenza[0],10);
	d1.setFullYear(a1);
	d1.setMonth(m1);
	d1.setDate(g1);

	if(d >= d1)
	{
		d1.setTime(d1.getTime() - (60*60*24*1000));
		
		var aa = d1.getFullYear();
		var mm = d1.getMonth()+1;
		var gg = d1.getDate();

		if(mm<10) mm = '0'+mm;
		if(gg<10) gg = '0'+gg;
	
		jQuery('#data_arrivo')[0].value =  gg + '/' + mm + '/' + aa;
	}
	aggiornaDate();
	
}

function prenotaBooking(baseUrl)
{
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	var partenza = pr[0].value.split("/");
	var arrivo = ar[0].value.split("/");

	var parametri = "&checkin_monthday=" + arrivo[0] + "&checkin_year_month="+arrivo[2]+"-"+arrivo[1];
	parametri = parametri + "&checkout_monthday=" + partenza[0] + "&checkout_year_month="+partenza[2]+"-"+partenza[1];
	parametri = parametri + "&do_availability_check=1";
	baseUrl = baseUrl + parametri;
	window.open(baseUrl,"_self");			
}

function aggiornaDate()
{
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	var partenza = pr[0].value;
	var arrivo = ar[0].value;
	
		var q = "arrivalDate="+arrivo+"&departureDate="+partenza;

		jQuery.ajax({
			type: "POST",
			url: "http://www.prenotazionealberghi.it/amministrazione/service/aggiornaDate.php",
			data: q,
			success:function aggiornaDateH(response){aggiornaDateHandler(response);}
			});
}
function aggiornaDateHandler(response)
{
	//alert(response);
}

function getHotels(url)
{
	url = url.replace(/&/g,'|');
	var q = "url="+url;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/getHotels.php",
		data: q,
		success:function getHotelsH(response){getHotelsHandler(response);}
		});
}
function getHotelsHandler(response)
{
	//alert(response);
	var myObject = eval('(' + response + ')');
	var h = '';
	for(var i = 0; i<myObject.HotelListResponse.HotelList.HotelSummary.length;i++)
	{
		var hotel = myObject.HotelListResponse.HotelList.HotelSummary[i];
		h = h + ' ' + hotel.hotelId; 
	}

	alert(h);
}

function getAlriHotel(cacheKey,cacheLocation)
{
	jQuery("#altriRisultati").replaceWith('<div id="altriRisultati" style="width: 540px;float:left;padding-top: 10px;" align="center"><img src="http://www.prenotazionealberghi.it/imageHotel/loading.gif" /><div>');
	var q = "cacheKey="+cacheKey+'&cacheLocation='+cacheLocation;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/getAltriHotels.php",
		data: q,
		success:function getAltriHotelsH(response){getAltriHotelsHandler(response);}
		});
}
function getAltriHotelsHandler(response)
{
	jQuery("#altriRisultati").replaceWith('<div id="altriRisultati">'+response+'<div>');
	   jQuery(document).ready(function() {
		    jQuery("button").button();
		  });
		  
		  jQuery(function() {
				jQuery( "a", ".bottoneHref" ).button();
				//jQuery( "a", ".bottoneHref" ).click(function() { return false; });
			});
}

function getAlriHotelTematico(cacheKey,cacheLocation,baseUrl)
{
	jQuery("#altriRisultati").replaceWith('<div id="altriRisultati" style="width: 540px;float:left;padding-top: 10px;" align="center"><img src="http://www.prenotazionealberghi.it/imageHotel/loading.gif" /><div>');
	/*
	  if ( jQuery.browser.mozilla )
	  {
		    jQuery('#altriRisultati').replaceWith('<div id="altriRisultati" style="width: 540px;float:left;padding-top: 10px;" align="center"><img src="http://www.prenotazionealberghi.it/imageHotel/loading.gif" /><div>');
	  }
	  else
	  {
		    jQuery('#altriRisultati').html('<div id="altriRisultati" style="width: 540px;float:left;padding-top: 10px;" align="center"><img src="http://www.prenotazionealberghi.it/imageHotel/loading.gif" /><div>');
	  }
	  */
	var q = "cacheKey="+cacheKey+'&cacheLocation='+cacheLocation;
	jQuery.ajax({
		type: "POST",
		url: "http://"+baseUrl+"/service/getAltriHotelsTematico.php",
		data: q,
		success:function getAltriHotelsTematicoH(response){getAltriHotelsTematicoHandler(response);}
		});
}
function getAltriHotelsTematicoHandler(response)
{
	jQuery("#altriRisultati").replaceWith('<div id="altriRisultati">'+response+'<div>');
	/*
	if ( jQuery.browser.mozilla )
	  {
		    jQuery('#altriRisultati').replaceWith('<div id="altriRisultati">'+response+'<div>');
	  }
	  else
	  {
		    jQuery('#altriRisultati').html('<div id="altriRisultati">'+response+'<div>');
	  }
	  */
	
	   jQuery(document).ready(function() {
		    jQuery("button").button();
		  });
		  
		  jQuery(function() {
				jQuery( "a", ".bottoneHref" ).button();
				//jQuery( "a", ".bottoneHref" ).click(function() { return false; });
			});
}


function getNumberOfHotelForCity(destinationId,div)
{
	var q = "destinationId="+destinationId;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/getNumberOfHotelForCity.php",
		data: q,
		success:function getNumberOfHotelForCityH(response){getNumberOfHotelForCityHandler(response,div);}
		});
}
function getNumberOfHotelForCityHandler(response,div)
{
	//jQuery("#"+div).replaceWith('<div>'+response+'<div>');
	// alert(response);
	jQuery("#"+div)[0].text = response + " " + jQuery("#"+div)[0].text;
}
function getTripAdvisor(hotelID,div,full)
{
	var q = "hotelID="+hotelID+'&full='+full;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/getTripAdvisor.php",
		data: q,
		success:function getTripAdvisorH(response){getTripAdvisorHandler(response,div);}
		});
}
function getTripAdvisorTematico(baseUrl,hotelID,div,full)
{
	var q = "hotelID="+hotelID+'&full='+full;
	jQuery.ajax({
		type: "POST",
		url: "http://"+baseUrl+"/service/getTripAdvisor.php",
		data: q,
		success:function getTripAdvisorH(response){getTripAdvisorHandler(response,div);}
		});
}
function getTripAdvisorHandler(response,div)
{
	//jQuery("#"+div)[0].replaceWith(response);
	jQuery("#"+div).replaceWith('<div id="' + div + '">'+response+'<div>');
	
}

function getLandmark(destinationId,div,city)
{
	var q = "destinationId="+destinationId+'&city='+city;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/getLandmark.php",
		data: q,
		success:function getLandmarkH(response){getLandmarkHandler(response,div);}
		});
}

function getLandmarkTematico(baseUrl,destinationId,div,city)
{
	var q = "destinationId="+destinationId+'&city='+city;
	jQuery.ajax({
		type: "POST",
		url: "http://" + baseUrl + "/service/getLandmark.php",
		data: q,
		success:function getLandmarkH(response){getLandmarkHandler(response,div);}
		});
}
function getLandmarkHandler(response,div)
{
	jQuery("#"+div).replaceWith('<div id="' + div + '">'+response+'<div>');
	
}


function deepLink(hotelID)
{
	var q = "hotelID="+hotelID;
	jQuery.ajax({
		type: "POST",
		url: "http://www.prenotazionealberghi.it/service/deepLink.php",
		data: q,
		success:function deepLinkH(response){deepLinkHandler(response);}
		});
}

function deepLinkTematico(hotelID,siteBase)
{
	var q = "hotelID="+hotelID;
	jQuery.ajax({
		type: "POST",
		url: "http://" + siteBase + "/service/deepLink.php",
		data: q,
		success:function deepLinkH(response){deepLinkHandler(response);}
		});
}

function deepLinkHandler(response)
{
	location.replace(response);
}

function printCopyHome(siteBase)
{
	
	jQuery.ajax({
		type: "POST",
		url: "http://" + siteBase + "/tmp/salvaHome.php",
		success:function printCopyHomeH(response){printCopyHomeHandler(response);}
		});
}
function printCopyHomeHandler(response)
{
	//alert(response);
}

function cercaCitta(baseUrl,codCitta,nomeCitta)
{
	
	var ar = jQuery('#data_arrivo');
	var pr = jQuery('#data_partenza');
	var partenza = pr[0].value.replace("/","-").replace("/","-").replace("/","-");
	var arrivo = ar[0].value.replace("/","-").replace("/","-").replace("/","-");;
	
	var q = "|arrivalDate="+arrivo+"|departureDate="+partenza;
		
	var parametri = "/it_IT/" + nomeCitta + "/citta/" + codCitta + q ;
	
	baseUrl = 'http://' + baseUrl + parametri;
	window.open(baseUrl,"_self");			
}

