// tabs setup
var tabsHeight  = 0;
var queryString = new Querystring();
var selectedTab = parseInt(queryString.get("tab"), 10);
if (isNaN(selectedTab)) selectedTab = 1; selectedTab--;

// show options on book now form
function showOptions (formInput, formSelect) {
	$(formSelect).show();
	formInput.blur();
	formSelect.focus();
}

// hide options on book now form
function hideOptions (formSelect, formInput) {
	formInput.value = formSelect.options[formSelect.selectedIndex].text;
	$(formSelect).hide();
}

// book now form submission
function bookNow (bookNowForm) {
	var bookYear = bookNowForm.checkInYear.options[bookNowForm.checkInYear.selectedIndex].value;
	var bookMonth = bookNowForm.checkInMonth.options[bookNowForm.checkInMonth.selectedIndex].value;
	var bookDay = bookNowForm.checkInDay.options[bookNowForm.checkInDay.selectedIndex].value;
	var departureDate = new Date(bookYear, bookMonth - 1, bookDay);

	departureDate.setDate(departureDate.getDate() + parseInt(bookNowForm.lengthOfStay.options[bookNowForm.lengthOfStay.selectedIndex].value, 10));

	bookMonth = '00' + bookMonth;
	bookDay = '00' + bookDay;
	bookMonth = bookMonth.substr(bookMonth.length - 2, 2);
	bookDay = bookDay.substr(bookDay.length - 2, 2);
	bookNowForm.arrivalDate.value = bookYear + '-' +  bookMonth + '-' + bookDay;

	if (bookNowForm.resort[0].checked) {
		bookNowForm.action = 'http://www.starwoodhotels.com/luxury/search/ratelist.html';
		bookNowForm.target = '_blank';
	} else if (bookNowForm.resort[1].checked) {
		bookNowForm.action = 'https://gc.synxis.com/rez.aspx';
		bookNowForm.target = '_self';

		bookNowForm.arrive.value = bookMonth + '/' +  bookDay + '/' + bookYear;
		bookNowForm.nights.value = parseInt(bookNowForm.lengthOfStay.options[bookNowForm.lengthOfStay.selectedIndex].value, 10);
		bookNowForm.rooms.value = parseInt(bookNowForm.numberOfRooms.options[bookNowForm.numberOfRooms.selectedIndex].value, 10);
		bookNowForm.adult.value = parseInt(bookNowForm.numberOfAdults.options[bookNowForm.numberOfAdults.selectedIndex].value, 10);
	}

	bookMonth = departureDate.getMonth() + 1;
	bookMonth = '00' + bookMonth.toString();
	bookDay = '00' + departureDate.getDate().toString();
	bookMonth = bookMonth.substr(bookMonth.length - 2, 2);
	bookDay = bookDay.substr(bookDay.length - 2, 2);
	bookNowForm.departureDate.value = departureDate.getFullYear() + '-' +  bookMonth + '-' + bookDay;
	
	bookNowForm.method = 'get';

	InpageTracker._linkByPost(bookNowForm);
	return false;
}

// jquery init
$(document).ready(function() {
	if (typeof sIFR == "function") {
		sIFR.replaceElement("#categoryHeadline", named({sFlashSrc: "assets/fonts/Helvetica_55_Roman.swf", sColor: "#7b6e48"}));
	};

	$("#tabs > div").each(function() { if ($(this).height() > tabsHeight) tabsHeight = $(this).height(); });
	if (isNaN(selectedTab) || selectedTab < 0 || selectedTab >= $("#tabs > ul > li").length) selectedTab = 0;

	$("#tabs > ul").tabs({
		selected: selectedTab,
		show: function() { if (tabsHeight > 0) $("#tabs > div").height(tabsHeight); },
    	fx: { 
//        	height: "toggle"
        	opacity: "toggle"
	    }
	});

//	$("#sidebar img").hover(
//		function(){$(this).fadeTo("normal", 0.7);},
//		function(){$(this).fadeTo("normal", 1.0);}
//	);

	$("ul.nav img").preload({
	   	find: ".gif",
	    replace: "_hover.gif"
	});

	$("ul.nav").superfish({delay:800, animation:{height:"show"}}).find(">li").hover(
		function() { $(this).find("img").attr("src", function() { return this.src.replace(".gif", "_hover.gif"); }); },
		function() { $(this).find("img").attr("src", function() { return this.src.replace("_hover", ""); }); }
	);

	$("#booknow select").blur(function() { $(this).hide(); });
});
