$(function(){

	/* Set the date of the check-in box to today+1 and check-out to today+2 */
	var today = new Date();	
	var tomorrow = AddDays(today,1);
	var dayAfterTomorrow = AddDays(today,2);
	
	SetCheckInDate(tomorrow);
	SetCheckOutDate(dayAfterTomorrow);

	$('#check-in-day, #check-in-month, #check-in-year').change(function(){
		SetCheckOutDate(AddDays(GetCheckInDate(),2));
	});

	$('.reservation a').click(function(){
		$('.reservation a').removeClass('selected');
		$(this).addClass('selected');
		
		if ($(this).hasClass('fixed'))
		{
			$('.reservation .check-out').show();
			$('.reservation .flexible-stay').hide();
			$('.reservation form').unbind('submit');
		}
		else if ($(this).hasClass('flexible'))
		{
			$('.reservation .check-out').hide();
			$('.reservation .flexible-stay').show();
			
			$('.reservation form').bind('submit',function(){
				
				/*
					Hi-jack the form, disable the regular submit functionality.  Instead, calculate the departure date from the arrival date and length of stay
					and direct the browser to the resulting URL
				 */
				
				var bookingUrl = 'https://secure.hilton.com/en/hi/res/choose_dates.jhtml?hotel=KEFHFHI&arrivalDay=' + $('#check-in-day').val() + '&arrivalMonth=' + $('#check-in-month').val() + '&arrivalYear=' + $('#check-in-year').val();
				
				var length = $('#length-of-stay').val();
				
				var d = new Date();
				d.setFullYear($('#check-in-year').val(), parseInt($('#check-in-month').val()) - 1, $('#check-in-day').val());
				
				d = AddDays(d,parseInt(length));
				
				var checkoutDay = d.getDate();
				var checkoutMonth = d.getMonth()+1;
				var checkoutYear = d.getFullYear();
				
				bookingUrl += '&departureDay=' + checkoutDay + '&departureMonth=' + checkoutMonth + '&departureYear=' + checkoutYear;
				
				document.location.href = bookingUrl;
				
				return false;
			});
		}
		
		return false;
	});


	initBanners();
});

function SetCheckInDate(date)
{
	$('#check-in-year').val(date.getFullYear());
	$('#check-in-month').val(AddLZeros(date.getMonth()+1));
	$('#check-in-day').val(AddLZeros(date.getDate()));
}

function SetCheckOutDate(date)
{
	$('#check-out-year').val(date.getFullYear());
	$('#check-out-month').val(AddLZeros(date.getMonth()+1));
	$('#check-out-day').val(AddLZeros(date.getDate()));
}

function GetCheckInDate()
{
	var result = new Date();
	result.setFullYear($('#check-in-year').val(), parseInt($('#check-in-month').val()) - 1, $('#check-in-day').val());
	return result;
}

function GetCheckOutDate()
{
	var result = new Date();
	result.setFullYear($('#check-out-year').val(), parseInt($('#check-out-month').val()) - 1, $('#check-out-day').val());
	return result;
}

function AddDays(date, days)
{
	var result = new Date();
	result.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
	result.setDate(result.getDate()+days);
	return result;
}

function AddLZeros(input)
{
	if (parseInt(input) < 10)
	{
		return '0' + input;
	}
	
	return input;
}

var offerIdx = 1;
var offerCount = 0;
var shakeHandle = null;
var loopHandle = null;

function initBanners()
{
	offerCount = $('#offers .offer-container').find('.offer').length;

	$('#offers .offer-container').css('width',280*offerCount);	
	
	$('#offers ul li.prev a').live('click',function() {
		if (offerIdx > 1)
		{
			$('#offers .offer-container').stop();
			offerIdx--;
			showBanner(null,offerIdx,offerCount);
		}
		
		return false;
	});

	$('#offers ul li.next a').live('click',function() {
		if (offerIdx < offerCount)
		{
			$('#offers .offer-container').stop();
			offerIdx++;
			showBanner(null,offerIdx,offerCount);
		}
				
		return false;
	});
	
	$('#offers ul li.navitem a').live('click',function() {
		$('#offers .offer-container').stop();
		var oldIdx = offerIdx;
		offerIdx = $(this).text();
		showBanner(oldIdx, $(this).text(),offerCount);
		return false;
	});
	
	if (offerCount > 1)
	{
		var navigation = '<ul>'+
			'<li class="prev"><a href="">&laquo;</a></li>';
		
		var offset = (278 / 2) - ((offerCount * 10) / 2);
			
		for (var i = 0; i < offerCount; i++)
		{
			var s = (i == 0) ? 'selected' : '';
			navigation += '<li class="navitem ' + s + '"><a href="" style="left:' + (offset + (i * 10)) + 'px;">'+(i+1)+'</a></li>';
		}

		navigation += '<li class="next"><a href="">&raquo;</a></li>' +
			'</ul>';
				
		$('#offers').append(navigation);
		
		nextBanner();
	}
}

function showBanner(oldIdx, newIdx)
{
	$('#offers ul li').removeClass('selected');
	$('#offers ul li:eq('+newIdx+')').addClass('selected');

	var speed = 1500;
	
	if (oldIdx != null)
	{
		var diff = Math.abs((oldIdx-newIdx));
		if (diff == 0) { diff = 1; }
		
		speed = 1500 / diff;
	}

	var marginLeft = 0;
	
	if (newIdx > 1)
	{
		marginLeft = ((newIdx-1) * -280);
	}
	
	var easing = ((newIdx == 1) || (newIdx == offerCount)) ? '' : 'easeOutBack';
	
	$('#offers .offer-container').animate(
		{marginLeft: marginLeft}
		 ,speed,easing,function(){
			nextBanner();
		 });
}

function nextBanner()
{
	$('#offers .offer-container').animate(
	{marginLeft: ((offerIdx-1) * -280)}
	 ,2000,'',function(){
		if (offerIdx < offerCount)
		{
			offerIdx++;
			showBanner(offerIdx-1,offerIdx);
		}
		else if (offerIdx > 1)
		{
			offerIdx = 1;
			showBanner(offerIdx-1,offerIdx);
		}
	 });
}
