// JavaScript Document

/* NH Hoteles
 * Javascript de comportamiento de interfaz.  Funciones comunes.  Hacen uso de jQuery 1.4
 *
 * Version - 0.1
 * Fecha - 16/06/2010
 * Autor - Miguel Alvarez
 *
 */
var $$ = jQuery.noConflict();
var tipActivo = 0;

$$(document).ready(function() {

	var LANG = $$("html").attr("lang");	
	
	// Valores por defecto de las promociones
	if(!PROMOTION.minDate) PROMOTION.minDate = '2000-01-01';
	if(!PROMOTION.maxDate) PROMOTION.maxDate = '2099-12-31';
	if(!PROMOTION.minNights) PROMOTION.minNights = 0;
	if(!PROMOTION.maxNights) PROMOTION.maxNights = 60;
	if(!PROMOTION.startDateDisabledDays) PROMOTION.startDateDisabledDays = [];
	if(!PROMOTION.endDateDisabledDays) PROMOTION.endDateDisabledDays = [];

	// Calendario
	$$.datepicker.setDefaults(datePickerL10N[LANG]);
	$$("#from").datepicker({
		changeMonth: false,
		numberOfMonths: 2,
		minDate: new Date().latestBetween(PROMOTION.startDate.toDate()),
		maxDate: PROMOTION.endDate.toDate().shiftDays(-PROMOTION.minNights),
		showOn: "both",
		beforeShowDay: startDateIsDisabled,
		buttonImage: '_style/_css/_gfx/ico-calendar.gif',
		buttonImageOnly: true,
		onSelect: function() {
			//$$("#searchButton").attr("disabled", "disabled").parent().addClass("disabled");
			$$("#to").datepicker('setDate', '');
			$$("#to").datepicker('option', 'minDate', $$("#from").datepicker('getDate').shiftDays(PROMOTION.minNights));
			$$("#to").datepicker('option', 'maxDate', $$("#from").datepicker('getDate').shiftDays(PROMOTION.maxNights));
		}	
	});
	$$("#to").datepicker({
		changeMonth: false,
		numberOfMonths: 2,
		minDate: PROMOTION.startDate.toDate().shiftDays(PROMOTION.minNights),
		maxDate: PROMOTION.endDate.toDate(),
		showOn: "both",
		beforeShowDay: endDateIsDisabled,
		buttonImage: '_style/_css/_gfx/ico-calendar.gif',
		buttonImageOnly: true,
		onSelect: function() {
			//$$("#searchButton").removeAttr("disabled").parent().removeClass("disabled");
		}
	});

	/* TIPS */
	//On Click Event
	if ($$("a.tip_lnk").length) {
		$$("a.tip_lnk").click(function(e) {
		  e.preventDefault();
		  $$(".tip_layer").hide(); //Esconde todo el contenido de la tab
		  $$(this).next(".tip_layer").show(); //Muestra todo el contenido de cada tip
		  tipActivo = 1; 
		});
		$$("a.lnk_cerrar").click(function(e) {
		  e.preventDefault();
		  $$(this).parent().parent().hide();
		});
		$$("body").click(function(e) {
		  if (tipActivo==2) {
		   tipActivo = 0; 
		   $$(".tip_layer").hide(); //Esconde todo el contenido
		  } else if(tipActivo==1) {
		   tipActivo = 2;
		  }
		});
	}
	
	// Poblar selectores de pais, ciudad y hotel
	var countries = [], cities = [];
	$$.each(nhHotels, function(i, hotelArray) {
		if(PROMOTION.hotels.indexOf(hotelArray[0]) != -1 && cities.indexOf(hotelArray[1]) == -1) {
			cities.push(hotelArray[1]);
		}
	});
	$$.each(nhCities, function(i, cityArray) {
		if(cities.indexOf(parseInt(cityArray[1])) != -1 && countries.indexOf(cityArray[0]) == -1) {
			countries.push(cityArray[0]);
		}
	});
	$$("#pais").change(function() {
		populateCities("#ciudad", LANG, $$("#pais").val(), cities);
		$$("#ciudad").change();
	});
	$$("#ciudad").change(function() {
		populateHotels("#hotel", LANG, parseInt($$("#ciudad").val()), PROMOTION.hotels);
	});
	populateCountries("#pais", LANG, countries);
	populateRooms("#habitaciones", LANG, 4);
	$$("#pais").change();
	
	// Form submit
	$$("form").submit(function(event) {
		if(!$$("#from").val() || !$$("#to").val()) {
			event.preventDefault();
			alert(txtDateError[LANG]);
		}
		if(parseInt($$("#hotel").val())) {
			// Reserva
			$$("form").get(0).setAttribute("action", reservationURL[LANG]);
		} else {
			// Busqueda
			$$("form").get(0).setAttribute("action", searchURL[LANG]);
			$$("#hotel").removeAttr("name");
			$$("<input type='hidden' name='action' value='search' />").appendTo($$("form"));
			$$("<input type='hidden' name='searchEngine' value='1' />").appendTo($$("form"));
			$$("<input type='hidden' name='stype' value='1' />").appendTo($$("form"));
			$$("<input type='hidden' name='event' value='1' />").appendTo($$("form"));
			$$("<input type='hidden' name='country' />").val($$("#pais").val()).appendTo($$("form"));
			$$("<input type='hidden' name='city' />").val($$("#ciudad").val()).appendTo($$("form"));
		}
		$$("input[name=fini]").val($$("#from").datepicker("getDate").toSpanishDate());
		$$("input[name=fout]").val($$("#to").datepicker("getDate").toSpanishDate());
		$$("input[name=language]").val(LANG);
		$$("input[name=idpromo]").val(PROMOTION.promoId);
		for(i=1; i<=$$("#habitaciones").val(); i++) {
			$$("<input type='hidden' />").attr("name", "nadults"+i).val(2).appendTo($$("form"));
			$$("<input type='hidden' />").attr("name", "nchilds"+i).val(0).appendTo($$("form"));
		}
	});
		
});

String.prototype.toDate = function() {
	var y = this.substring(0, 4);
	var m = this.substring(5, 7) - 1;
	var d = this.substring(8, 10);
	
	return new Date(y, m, d);
};

Date.prototype.shiftDays = function(days) {
	return new Date(this.getTime() + days * 24 * 60 * 60 * 1000);
}

Date.prototype.latestBetween = function(date) {
	return date.getTime() > this.getTime() ? date : this;
}

Date.prototype.earliestBetween = function(date) {
	return date.getTime() < this.getTime() ? date : this;
}

Number.prototype.datePad = function() {
	return this < 10 ? "0"+this : this;
}

Date.prototype.toSpanishDate = function() {
	return this.getDate().datePad() + "/" + (this.getMonth() + 1).datePad() + "/" + this.getFullYear();
}

function startDateIsDisabled(date) {
	return [PROMOTION.startDateDisabledDays.indexOf(date.getDay()) == -1, ""];
}

function endDateIsDisabled(date) {
	return [PROMOTION.endDateDisabledDays.indexOf(date.getDay()) == -1, ""];
}

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
