jQuery(document).ready(function() {
	jQuery("#dtn").attr("autocomplete", "off");
	jQuery("#pda").attr("autocomplete", "off");
	jQuery("#pdb").attr("autocomplete", "off");
	jQuery("#nnt").attr("autocomplete", "off");

	jQuery("#nnt").change(
		function() {
			if(jQuery(this).val() == 0) jQuery(this).val(1);

			var pda = jQuery("#pda").dpGetSelected()[0];

			if(pda == null) return;

			var ONE_DAY = 1000 * 60 * 60 * 24;
			var dd = new Date(pda.getTime()+(ONE_DAY * jQuery(this).val()));
			jQuery("#pdb").dpSetSelected(dd.asString());
			jQuery("#pdb").val(dd.asString());
	});
	
	jQuery("#rechercher").click(
		function() {
			var dtn = jQuery("#dtn").val();
			var pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();
			var recherche = false;
			
			recherche = (dtn == "" ? false : true);

			dtn = (dtn == "" ? "n" : dtn);
			dtn = dtn.replace(/[,]/g, "").toLowerCase();
			dtn = noaccent(dtn);

			if(pda != "" && pdb != "")
			{
				pda = pda.substring(6, 10)+"-"+pda.substring(3, 5)+"-"+pda.substring(0, 2);
				pdb = pdb.substring(6, 10)+"-"+pdb.substring(3, 5)+"-"+pdb.substring(0, 2);
				recherche = true;
			}
			
			if((pda != "" && pdb == "") || (pda == "" && pdb != ""))
			{
				recherche = false;
			}
			
			if(recherche == true)
			{
				var qwery = "tn=" + dtn + "&da=" + pda + "&db=" + pdb;
				jQuery.ajax({
					type: "POST",
					url: "/hotels/search/product",
					data: qwery,
					dataType: "text",
					cache: false,
					success: function(retour) {
						var URL = retour;
						if(retour.substring(0, 4) == "url:")
						{
							URL = URL.replace(/^[u][r][l]:/i, "");
							document.location = URL;
						}
						else
						{
							alert(retour);
						}
					}
			   });
			}
	});
});

function days_between(date1, date2)
{
  // Le nombre de milliseconds en un jour
  var ONE_DAY = 1000 * 60 * 60 * 24

  // Convertir le jour et la date en  milliseconds
  var date1_ms = date1.getTime()
  var date2_ms = date2.getTime()

  // Calcul la difference en milliseconds
  var difference_ms = Math.abs(date1_ms - date2_ms)
  
  // Convertir encore en jours et retourner le resultat
  return Math.round(difference_ms/ONE_DAY)
}

function update_nombre_nuits()
{
	var pda = jQuery("#pda").dpGetSelected()[0];
	var pdb = jQuery("#pdb").dpGetSelected()[0];
	
	if(pda == null || pdb == null) return;
	
	jQuery("#nnt").val(days_between(pda, pdb));
}

jQuery(function()
{
	Date.firstDayOfWeek = 1;
	Date.format = "dd/mm/yyyy";
	jQuery.dpText = {
		TEXT_PREV_YEAR : "Previous year",
		TEXT_PREV_MONTH : "Previous month",
		TEXT_NEXT_YEAR : "Next year",
		TEXT_NEXT_MONTH : "next month",
		TEXT_CLOSE : "Close",
		TEXT_CHOOSE_DATE : "Show calendar"
	}
	jQuery(".date-pick").datePicker({
		clickInput: true,
		showYearNavigation: false,
		createButton: false,
		verticalOffset: 20,
		horizontalOffset: -10
	});
	
	jQuery("#pda").bind(
		"dpClosed",
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				jQuery("#pdb").dpSetStartDate(d.addDays(1).asString());
				update_nombre_nuits();
			}
		}
	);
	jQuery("#pdb").bind(
		"dpClosed",
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				jQuery("#pda").dpSetEndDate(d.addDays(-1).asString());
				update_nombre_nuits();
			}
		}
	);
	
	if(msgInfo.getItem("pda") != "")
	{
		da = new Date(msgInfo.getItem("pda").substring(0, 4), msgInfo.getItem("pda").substring(5, 7)-1, msgInfo.getItem("pda").substring(8, 10));
		jQuery("#pda").dpSetSelected(da.asString());
		jQuery("#pda").val(da.asString());
	}

	if(msgInfo.getItem("pdb") != "")
	{
		db = new Date(msgInfo.getItem("pdb").substring(0, 4), msgInfo.getItem("pdb").substring(5, 7)-1, msgInfo.getItem("pdb").substring(8, 10));
		jQuery("#pdb").dpSetSelected(db.asString());
		jQuery("#pdb").val(db.asString());
	}

	update_nombre_nuits();

	jQuery("#dtn").autocomplete(
		wheretogo,
		{
			delay: 10,
			minChars: 3,
			matchSubset: 1,
			matchContains: 1,
			pathQuery: true,
			maxItemsToShow: 10,
			cacheLength: 10,
			autoFill: false
		}
	);
	
	jQuery("#rechercher").click(
		function() {
			var dtn = jQuery("#dtn").val();
			var pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();
			var recherche = false;

			recherche = (dtn == "" ? false : true);

			dtn = (dtn == "" ? "n" : dtn);
			dtn = dtn.replace(/[,]/g, "").toLowerCase();
			dtn = noaccent(dtn);

			if(pda != "" && pdb != "")
			{
				pda = pda.substring(6, 10)+"-"+pda.substring(3, 5)+"-"+pda.substring(0, 2);
				pdb = pdb.substring(6, 10)+"-"+pdb.substring(3, 5)+"-"+pdb.substring(0, 2);
				recherche = true;
			}
			
			if((pda != "" && pdb == "") || (pda == "" && pdb != ""))
			{
				recherche = false;
			}

			if(recherche == true)
			{
				var qwery = "tn=" + dtn + "&da=" + pda + "&db=" + pdb;
				jQuery.ajax({
					type: "POST",
					url: "/hotels/search/",
					data: qwery,
					dataType: "text",
					cache: false,
					success: function(retour) {
						var URL = retour;
						if(retour.substring(0, 4) == "url:")
						{
							URL = URL.replace(/^[u][r][l]:/i, "");
							document.location = URL;
						}
						else
						{
							alert(retour);
						}
					}
			   });
			}
	});
});
