$(document).ready(function()
{
	$(document).pngFix();
	$.preloadImages(
		'/SiteBase/wrap/theme0/images/nav.specials-pkgs.on.gif',
		'/SiteBase/wrap/theme0/images/nav.lodging.on.gif',
		'/SiteBase/wrap/theme0/images/nav.things_to_do.on.gif',
		'/SiteBase/wrap/theme0/images/nav.restaurants.on.gif',
		'/SiteBase/wrap/theme0/images/nav.area_info.on.gif',
		'/SiteBase/wrap/theme0/images/nav.trip_planning.on.gif',
		'/SiteBase/wrap/theme0/images/nav.getting_here.on.gif',
		'/SiteBase/wrap/theme0/images/nav.groups_celebrations.on.gif',
		'/SiteBase/wrap/theme0/images/0.gif',
		'/SiteBase/wrap/theme0/images/1.gif',
		'/SiteBase/wrap/theme0/images/2.gif',
		'/SiteBase/wrap/theme0/images/3.gif',
		'/SiteBase/wrap/theme0/images/4.gif',
		'/SiteBase/wrap/theme0/images/5.gif',
		'/SiteBase/wrap/theme0/images/6.gif',
		'/SiteBase/wrap/theme0/images/7.gif',
		'/SiteBase/wrap/theme0/images/8.gif',
		'/SiteBase/wrap/theme0/images/9.gif'
	); // takes array of image urls
	$.initInntopiaBookingForm();
});

/*
 * Preload multi-state images
 */
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery.initInntopiaBookingForm = function()
{
	$("#frmBookingWidget").submit(
    function() {
		var blnFormOk = true;
		var dtArrive = $("#txtDtArrive").val();
		var dtDepart = $("#txtDtDepart").val();
		var d1_ms = Date.parse(dtArrive);
		var d2_ms = Date.parse(dtDepart);
		if (isNaN(d1_ms) || isNaN(d2_ms)) {
			alert('Please select your Arrival and/or Departure Dates.');
			blnFormOk = false;
		}
		if (d1_ms >= d2_ms) {
			alert('Your Departure Date must be after your Arrival Date.');
			blnFormOk = false;
		}
		
		var iAdults = $("#ddlAdults").val();
		var iChildren = $("#ddlChildren").val();
		if (isNaN(iAdults) || isNaN(iChildren) || (Number(iAdults) + Number(iChildren) < 1)) {
			alert('Your party size must be greater than 0.');
			blnFormOk = false;
		}
		
		if (blnFormOk) {
			// Construct inline form and submit for GA tracking
			var newPage = window.open('','inntopia','height=700,width=790,scrollbars=yes,resizable=yes');
			var newForm = newPage.document.createElement("form");
			newForm.setAttribute("action", "http://www.inntopia.travel/aspnet/2.0/search_07.aspx");
			newForm.setAttribute("method", "post");
			newForm.setAttribute("name", "frmBookingWidget2");
			newForm.setAttribute("id", "frmBookingWidget2");
			newForm.setAttribute("style", "display:none;");
			newForm.setAttribute("onSubmit", "__utmLinkPost(this)");
			var newInput = newPage.document.createElement("input");
			newInput.setAttribute("type", "text");
			newInput.setAttribute("name", "arrivaldate");
			newInput.setAttribute("value", dtArrive);
			newForm.appendChild(newInput);
			var newInput2 = newPage.document.createElement("input");
			newInput2.setAttribute("type", "text");
			newInput2.setAttribute("name", "departuredate");
			newInput2.setAttribute("value", dtDepart);
			newForm.appendChild(newInput2);
			var newInput3 = newPage.document.createElement("input");
			newInput3.setAttribute("type", "text");
			newInput3.setAttribute("name", "adultcount");
			newInput3.setAttribute("value", iAdults);
			newForm.appendChild(newInput3);
			var newInput4 = newPage.document.createElement("input");
			newInput4.setAttribute("type", "text");
			newInput4.setAttribute("name", "childcount");
			newInput4.setAttribute("value", iChildren);
			newForm.appendChild(newInput4);
			var newInput5 = newPage.document.createElement("input");
			newInput5.setAttribute("type", "text");
			newInput5.setAttribute("name", "salesid");
			newInput5.setAttribute("value", "487643");
			newForm.appendChild(newInput5);
			newPage.document.body.appendChild(newForm);
			var newSubmitButton = newPage.document.createElement("input");
			newSubmitButton.setAttribute("type", "submit");
			newSubmitButton.setAttribute("name", "btnSubmit");
			newSubmitButton.setAttribute("value", "Go");
			newForm.appendChild(newSubmitButton);
			newSubmitButton.click();
		}
		
		// Always return false so that form action isn't followed unless JS is off
		return false;
    });
}