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

// 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(){
	$("ul.nav img").preload({
	   	find: ".gif",
	    replace: "_hover.gif"
	});

	$("ul.nav").superfish({delay:500, 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", ""); }); }
	);

	$("#storiesContent").cycle({fx:"fade", random:0, timeout:8000, pause:1, pager:"#storiesHeader", pagerAnchorBuilder:function(idx, slide) { return '<a href="#"><img src="assets/images/shim.gif" width="17" height="16" alt="" /></a>'; }});
	
	$("#specialsContent").cycle({fx:"fade", random:0, timeout:8000, pause:1, pager:"#specialsHeader", pagerAnchorBuilder:function(idx, slide) { return '<a href="#"><img src="assets/images/shim.gif" width="17" height="16" alt="" /></a>'; }});


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

	// get date range for calendar
	var startDate = new Date();
	var endDate = new Date();
	
	endDate.addYears(3);
	
	// initialise the "Select date" link
	$('#date-pick')
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:startDate.asString(),
				endDate:endDate.asString()
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				updateSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				updateSelects(selected[0]);
			}
		);
		
	var updateSelects = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);

		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();

		($('#checkInDay')[0]).selectedIndex = d - 1;
		($('#checkInMonth')[0]).selectedIndex = m;
		($('#checkInYear')[0]).selectedIndex = y - startDate.getFullYear();

		hideOptions(document.reservations.checkInDay, document.reservations.checkInDayInput);
		hideOptions(document.reservations.checkInMonth, document.reservations.checkInMonthInput);
		hideOptions(document.reservations.checkInYear, document.reservations.checkInYearInput);
	}
	// listen for when the selects are changed and update the picker
	$('#checkInDay, #checkInMonth, #checkInYear')
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#checkInYear').val(),
							$('#checkInMonth').val()-1,
							$('#checkInDay').val()
						);
				$('#date-pick').dpSetSelected(d.asString());
			}
		);
	
	// and update the datePicker to reflect it...
	$('#checkInDay').trigger('change');
});

