$(document).ready(function() {
	//initialise date picker(s)
	$('.datepicker').datepicker({
		duration: "",
		minDate: '0',
		maxDate: '1Y'
	});
	
	$('#arrive').bind('change', function() {
		//grab date string and parse
		var departDate = $.datepicker.parseDate('mm/dd/yy', $('#arrive').val());
		
		//split date for creation of new Date()
		var month = departDate.getMonth();
		var date = departDate.getDate();
		var year = departDate.getFullYear();
		
		//assure check-out date is > check-in date
		$('#depart').datepicker('option', 'minDate', new Date(year, month, date + 1));
	});
	
	//submit reservation form
	$('#vhg-rese-widget').submit(function() {
		//constants
		domain = "https://gc.synxis.com";
		chainId = "1003";
		
		//gather reservation form data
		var url = domain;
		var hotelId = $('#destination :selected').val();
		var arrivalDate = $('#arrive').val();
		var departDate = $('#depart').val();
		var adults = $('#adults :selected').val();
		var kids = $('#kids :selected').val();
		var codeType = $('#code-type :selected').val();
		var code = $('#code').val();
		
		//generate URL string
		url += "?chain="+chainId
			  +"&hotel="+hotelId
			  +"&arrive="+arrivalDate
			  +"&depart="+departDate
			  +"&adult="+adults
			  +"&child="+kids
			  +"&"+codeType+"="+code
		  	  +"&start=1"
			  +"&lang=1";
		
		//is the destination 'Viceroy Snowmass'?
		//we only want to pass synxis &shell var	
		if (hotelId == '24803') {
			url +=
			"&shell=viceroy"
		}
		
		//we want to append synxis &template var
		else {
			url +=
			"&shell=viceroy"
			+"&template=viceroy"
		}
		
		//send url data to synxis booking engine
		window.open(url);
		
		return false;
	});
	//modify the dropdown lists
	adultsKids();

	// when the location changes
	$('#destination').bind('change', function() {
        setTimeout("adultsKids()", 250);
	});

	// when the adults ddl changes
	$('#adults').bind('change', function() {
        setTimeout("adultsKids()", 250);
	});

	// when the kids ddl changes
	$('#kids').bind('change', function() {
        setTimeout("adultsKids()", 250);
	});
});

function adultsKids () {

	// get the number of adults and then set this value if still exists.
	var adultsVal = $('#adults').val();
	
	// get the number of kids and then set this value if still exists.
	var kidsVal = $('#kids').val();

	// set the max value for the ddl add 1 to max number
	var kidsMaxVal = 13 - adultsVal ;
	var adultsMaxVal = 13 - kidsVal;
	
	
	// if Anguilla
	if ( $('#destination').val()=='19595' ) {

 		$("#adults").html( genDD( '1', adultsMaxVal, adultsVal ) );
 		$("#kids").html( genDD( '0', kidsMaxVal, kidsVal ) );
		
	} else {
		$("#adults").html( genDD( '0', 5, adultsVal ) );
		$("#kids").html( genDD( '0', 5, kidsVal ) );
	}
}

function genDD( startWith, numTo, selectedNum ) {
	var ddval="";
	for ( i=startWith; i< numTo; i++) {
		ddval += "<option value=\"" + i + "\"";
		if ( i == selectedNum ) {
			ddval += " selected ";
		}
		ddval += ">" + i + "</option>";
	}
	return( ddval );
}