function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

// Requires: jquery, datepicker (jquery-ui)

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

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}


function padString(str, len, padc) {
	if ("string" !== typeof str) str = str.toString();
	if ("undefined" === typeof padc) padc = "0";
	if (str.length < len) {
		for (var i = str.length; i < len; ++i) {
			str = padc + str;
		}
	}
	return str;
}

Date.prototype.getMonthPad = function() {
	return padString(this.getMonth()+1, 2);
}

Date.prototype.getDatePad = function() {
	return padString(this.getDate(), 2);
}

Date.prototype.getCurrentDate = function() {
	return this.getFullYear()+"-"+this.getMonthPad()+"-"+this.getDatePad();
}

Date.prototype.getCurrentDateFriendly = function() {
	return phpJSVars.days[this.getUTCDay()]+" "+this.getUTCDate()+" "+phpJSVars.months[this.getUTCMonth()]+" "+this.getUTCFullYear();
}

Date.prototype.fromYMD = function(ymd) {
	var parts = ymd.split(/-/);
	this.setUTCFullYear(parts[0]);
	this.setUTCMonth(parts[1]-1);
	this.setUTCDate(parts[2]);
	return this;
}

Date.prototype.addDays = function(days) {
	this.setTime(this.getTime() + (86400000 * parseInt(days)));
	return this;
}

if ("function" !== typeof enableControls) {
	var enableControls = function() {
		$("[name=city]:input, [name=checkInDate]:input, [name=nbNight]:input, [type=submit]:input, [type=image]:input").removeAttr("disabled");
	}
}

$(function(){
	if ($("#searchbox-mini").length) {
		$("#searchbox-full").hide();
	}
	$("#searchbox-mini p.more-options a").click(function () {
		$("#searchbox-mini").fadeOut(200, function() {
			$("#searchbox-full").fadeIn(200);
		});
		return false;
	});

	var maxDate = new Date();

	var today = (new Date).getCurrentDate();

	$("div[id^=searchbox] form").submit(function() {
		$("[name=checkInDate]:input").each(function() {
			///if ($(this).val() === today) $(this).attr("disabled", "disabled");
		});
		$("[name=nbNight]:input").each(function() {
			////if ("1" === $(this).val()) $(this).attr("disabled", "disabled");
		});
		window.setTimeout(enableControls, 10);
		return true;
	});

	var dep_date = $("#departure-date");
	if (!dep_date.length) return;

	$("#checkInDate, #[name=nbNight]:input, #sb-sdate, #sb-nights").change(function(){
		var d = new Date(), fromDate, days;
		if ($("#sb-sdate").length) {
			fromDate = $("#sb-sdate").val();
			days = $("#sb-nights").val();
		} else {
			fromDate = $("#checkInDate").val();
			days = $("[name=nbNight]:input").val();
		}
		if (!fromDate) return;
		dep_date.text(d.fromYMD(fromDate).addDays(days).getCurrentDateFriendly());
	}).change();
});

//Search box button rollover

$(document).ready(function(){
	$("#search-submit").hover(
		function(){
			var iconName = $(this).attr("src");
			var origen = iconName.split("searchbutton")[0];
			$(this).attr({src: "" + origen + "searchbutton_active.png"});
		},
		function(){
			var iconName = $(this).attr("src");
			var origen = iconName.split("searchbutton_active.")[0];
			$(this).attr({src: "" + origen + "searchbutton.png"});
		});
});

