jQuery(document).ready(function() {

	jQuery("#box_liste_chambre/ul/li").mouseover(
		function() {
			jQuery(this).css("background-color", "#F5EFEB");
	});

	jQuery("#box_liste_chambre/ul/li").mouseout(
		function() {
			jQuery(this).css("background-color", "#FFFFFF");
	});

	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 pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();
			var nnt = jQuery("#nnt").val();
			var ids = jQuery("#ids").val();

			if(pda.length > 0 && pdb.length > 0)
			{
			   jQuery("#stat_pda").text(pda);
			   jQuery("#stat_pdb").text(pdb);
			   jQuery("#stat_nuit").text(nnt);

				jQuery("#box_spinner_rechercher").fadeIn("normal");

				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);

				var qwery = "id=" + ids + "&da=" + pda + "&db=" + pdb;
				jQuery.ajax({
					type: "POST",
					url: "/hotels/search/rooms",
					data: qwery,
					dataType: "html",
					cache: false,
					success: function(retour) {
						jQuery("#box_liste_chambre").hide();
						jQuery("#box_liste_chambre").html(retour);
						jQuery("#box_liste_chambre").fadeIn("normal");
						set_change_select();
						set_ajouter_unique();
						set_verifier_disponible();
						annuler_selection();
		   			jQuery("#box_spinner_rechercher").hide();
					}
			   });
			}
	});

	jQuery("#reserver").click(
		function() {
			if(jQuery("#cdl").val().length == 0)
			{
				jQuery.jGrowl(msgInfo.getItem("before-booking"), { life: 3000 });
				return false;
			}

			jQuery("#box_spinner_rechercher").fadeIn("normal");

			var URL = "/booking/process/request";
			var pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();

			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);
			URL = URL + "/" + jQuery("#ids").val() + "/" + pda + "-"+ pdb;

			jQuery.ajax({
				type: "POST",
				url: URL,
				data: "cdl=" + jQuery("#cdl").val(),
				dataType: "text",
				cache: false,
				success: function(retour) {
					if(retour.length == 8)
					{
						var URL = msgInfo.getItem("secure") + "/booking/process/form/" + retour + "/";
						URL = URL + msgInfo.getItem("domaine");
						document.location = URL;
					}
					else
					{
						alert("Error occured!");
					}
					jQuery("#box_spinner_rechercher").hide();
				}
		   });
	});

	jQuery("#exreserver").click(
		function() {
			var ids = jQuery("#ids").val();
			var nnt = jQuery("#nnt").val();
			var pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();
						
			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);

			if(nnt.length == 0)
			{
				jQuery.jGrowl(msgInfo.getItem("before-booking"), { life: 3000 });
				return false;
			}

			jQuery.ajax({
				type: "POST",
				url: "/hotels/tracking",
				data: "id=" + ids + "&da=" + pda + "&db=" + pdb + "&st=D",
				dataType: "text",
				cache: false,
				success: function(retour) {
					var success = (retour.substring(0, 21) == "<!-- success = OK -->" ? true : false);

					if(success == true)
					{
						var URL = jQuery("#adr").val();
						URL = URL.replace("{date}", pda);
						URL = URL.replace("{nuit}", nnt);
						document.location = URL;
					}
					else
					{
						alert("Error occured!");
					}
				}
		   });
	});

	set_change_select();

	jQuery("#annuler").mouseover(
		function() {
			$(this).attr("src", "/ressource/image/l_close_hover.gif");
	});

	jQuery("#annuler").mouseout(
		function() {
			$(this).attr("src", "/ressource/image/l_close.gif");
	});

	jQuery("#annuler").click(
		function() {
			if(jQuery("#cdl").val().length > 0)
			{
				jQuery.jGrowl(msgInfo.getItem("selection-canceled"), { life: 1500 });
			}
			annuler_selection();
	});
	
	set_ajouter_unique();
	set_verifier_disponible();

	jQuery(".cancel_room").click(
		function() {
			var idchambre = jQuery(this).attr("id").substring(7, 15);
			var idselect = "#tf" + idchambre;
			var idoption = idselect + " option";

			jQuery(idselect).val("");
			calculer_panier();
	});

   jQuery("a.photo_vignette").lightBox({
		overlayBgColor: '#111',
		overlayOpacity: 0.6,
		imageLoading: "/ressource/image/lightbox/spinner.gif",
		imageBtnClose: "/ressource/image/lightbox/close.gif",
		imageBtnPrev: "/ressource/image/lightbox/prev.gif",
		imageBtnNext: "/ressource/image/lightbox/next.gif",
		containerResizeSpeed: 350,
		txtImage: "",
		txtOf: "/"
   });

   //$("#detail_onglet > ul").tabs({ fx: { opacity: "show" } });
   $("#chambre_onglet > ul").tabs({ fx: { opacity: "show" } });
});

function set_change_select()
{
	jQuery("#box_liste_chambre select").change(
		function() {
			calculer_panier();
			jQuery.jGrowl(msgInfo.getItem("selection-updated"), { life: 1500 });
	});
}

function set_ajouter_unique()
{
	jQuery(".ajouter_unique").click(
		function() {
			var idchambre = jQuery(this).attr("id").substring(2, 10);
			var idselect = "#tf" + idchambre;
			var idoption = idselect + " option";

			jQuery(idoption).each(function() {
				if(jQuery(this).text() == "+1")
				{
					if(jQuery(idselect).val() == "")
					{
						//jQuery.jGrowl.defaults.position = "center";
						jQuery.jGrowl(msgInfo.getItem("room-added"), { life: 1500 });
					}
					jQuery(idselect).val(jQuery(this).val());
					calculer_panier();
				}
			});
	});
}

function calculer_panier()
{
	var totalSejour = 0;
	var valueSejour = "";
	var nombreChambre = 0;
	var nombrePersonne = 0;

	jQuery("#box_liste_chambre select").each(function() {
		var t = 0;
		var n = 0;
		var p = 0;

		if(jQuery(this).val().length > 0)
		{
			t = parseInt(jQuery(this).val().split(":")[1]);
			n = parseInt(jQuery(this).val().split(",")[0]);
			p = parseInt(jQuery(this).val().split(",")[3]);
		}

		if(t > 0)
		{
			totalSejour += t; //totalSejour += (t * n);
			if(valueSejour.length > 0) valueSejour = valueSejour + ";";
			valueSejour = valueSejour + jQuery(this).val();
			nombreChambre += n;
			nombrePersonne += p;
		}
	});

	var montant = msgInfo.getItem("symbole").replace("{valeur}", totalSejour);
	jQuery("#montant_total").html(montant);
	jQuery("#mrs").val(totalSejour);
	jQuery("#cdl").val(valueSejour);
	jQuery("#stat_chambre").text((nombreChambre == 0 ? "-" : nombreChambre));
	jQuery("#stat_personne").text((nombrePersonne == 0 ? "-" : nombrePersonne));
}

function annuler_selection()
{
	jQuery("#box_liste_chambre select").each(function() {
		jQuery(this).val("");
	});
	
	calculer_panier();
}

function set_verifier_disponible()
{
	jQuery(".verifier_disponible").click(
		function() {
			var ids = jQuery("#ids").val();
			var nnt = jQuery("#nnt").val();
			var pda = jQuery("#pda").val();
			var pdb = jQuery("#pdb").val();
						
			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);

			jQuery.ajax({
				type: "POST",
				url: "/hotels/tracking",
				data: "id=" + ids + "&da=" + pda + "&db=" + pdb + "&st=C",
				dataType: "text",
				cache: false,
				success: function(retour) {
					var success = (retour.substring(0, 21) == "<!-- success = OK -->" ? true : false);

					if(success == true)
					{
						var URL = jQuery("#adv").val();
						document.location = URL;
					}
					else
					{
						alert("Error occured!");
					}
				}
		   });
	});
}

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();
});
