$(document).ready(function() {

	$(".more").click(function(){
		more(this);
	});
	$(".cboxElement").colorbox({width:"860px", height:"590px", iframe:true});
	$(".eventi-perks").colorbox({innerWidth:"720px", innerHeight:"665px", iframe:true});
	$(".live_fitness").colorbox({innerWidth:"470px", innerHeight:"160px", iframe:true});
	$(".spa-rules").colorbox({innerWidth:"625px", innerHeight:"550px", iframe:true});
	$(".wine_hour").colorbox({innerWidth:"400px", innerHeight:"210px", iframe:true});
	
	$('#reservation_close').click(function(){
	  $("#reservation_app").fadeOut();
	});

	$("#show_options").click(function(){
		if( $(this).hasClass('visible') ) {
			$("#hidden_options").slideUp();
			$(this).removeClass('visible');
			$(this).addClass('hidden');
			$(this).html('more options &raquo;');
		}
		else if( $(this).hasClass('hidden') ) {
			$("#hidden_options").slideDown();
			$(this).removeClass('hidden');
			$(this).addClass('visible');
			$(this).html('less options &raquo;');
		}
	});
	
	initDateFields();

	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$("select[name=arrDate]").attr('selectedIndex', $("select[name=arrDate]").attr('selectedIndex')+1);
		$("select[name=arrMonth]").attr('selectedIndex', $("select[name=arrMonth]").attr('selectedIndex')+1);
	}

	$('#mini-res').submit(function() {
		//constants
		domain = "https://gc.synxis.com/rez.aspx";

		//gather reservation form data
		//id like to serialize this, in future...but for now...
		var url = domain;
		var hotelId = $('#destination').val();
		var arrMonth = $('#arrMonth').val();
		var arrDate = $('#arrDate').val();
		var arrYear = $('#arrYear').val();
		var arrivalDate = arrMonth + '/' + arrDate + '/' + arrYear;
		var nights = $('#nights').val();
		var adults = $('#adults').val();
		var children = $('#children').val();
		var promo = $('#rateCode').val();
		var group = $('#groupCode').val();
		var rateType = $('#rateType').val();

		if (group == '') {
		//'group code' input is empty
		//generate URL string
		url += "?hotel="+hotelId
				+"&arrive="+arrivalDate
				+"&nights="+nights
				+"&adult="+adults
				+"&child="+children
				+"&promo="+promo
				+"&group="+group
				+"&start=51"
				+"&filter="+rateType;
	}

	else {
		//user has entered 'group code'
		//generate URL string
		url += "?hotel="+hotelId
				+"&arrive="+arrivalDate
				+"&nights="+nights
				+"&adult="+adults
				+"&child="+children
				+"&promo="+promo
				+"&group="+group
				+"&start=51";
	}

		//send url data to synxis booking engine
		window.location = url;

		return false;
	});
	
});

function photos() {
	$.fn.colorbox({href: '/manhattan-hotel-photo.html', width:"860px", height:"625px", iframe:true});
}
function more(trigger) {
	if($("#additional_content").attr('display') == 'block') {
		$("#additional_content").slideUp();
		$("#additional_content").attr('display', 'none');
		$(trigger).html("more");
		$(trigger).removeClass("less");
		return false;
	} else {
		$("#additional_content").slideDown();
		$("#additional_content").attr('display', 'block');
		$(trigger).html("less");
		$(trigger).addClass("less");
	}
}

function reservations() {
	if( $("#reservation_app").css("display") == "none") {
		$("#reservation_app").fadeIn();
	}
	else if( $("#reservation_app").css("display") == "block") {
		$("#reservation_app").fadeOut();
	}
}

function adjustYear(month) {

	frm = document.resLauncher;
	var now = new Date();
	
	if (month < now.getMonth() + 1) {
		frm.arrYear.selectedIndex = 1;
	} else {
		frm.arrYear.selectedIndex = 0;
	}
}

// initDateFields() initializes the month, day, and year select inputs
// the form must be named "resLauncher" and the date fields must be 
// named: "arriveMonth" "arriveDate" and "arriveYear"
// the select inputs will be populated dynamically

function initDateFields() {

	var frm = document.getElementById("mini-res");

	//
	// Begine creation of Month/Year <select>
	//
	var mnths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

	var now = new Date();
	//tomorrowDt   = new Date(now.getTime() + (1 * 86400000));
	currMnth = now.getMonth();	
	currDay  = now.getDate();
	currYear = now.getFullYear();

	var mt,yr,dy,mnthYr,len;

	// Day
	for (var dayIdx = 1; dayIdx <= 31; dayIdx++) {
		lenDay = frm.arrDate.options.length;
		if (dayIdx == currDay) {
			selDay = true;
			selDayIdx = dayIdx;
		} else {
			selDay = false;
		}
		
		frm.arrDate.options[lenDay] = new Option(dayIdx, dayIdx, selDay, selDay);
	}

	// Month
	for (var mnthIdx = 0; mnthIdx < 12; mnthIdx++) {
		lenMnth = frm.arrMonth.options.length;
		if (mnthIdx == currMnth) {
			selMnth = true;
			selMnthIdx = mnthIdx;
		} else {
			selMnth = false;
		}
		
		frm.arrMonth.options[lenMnth] = new Option(mnths[mnthIdx], mnthIdx+1, selMnth, selMnth);
	}

	//  Years
	for (var yearIdx = currYear; yearIdx < currYear + 2; yearIdx++) {
		lenYear = frm.arrYear.options.length;
		selYear = (yearIdx == currYear) ? true : false;
		frm.arrYear.options[lenYear] = new Option(yearIdx, yearIdx, selYear, selYear);
	}
}

function calePopUp(width, height)
{
	// Get the month/year/dates
	// these must be converted from there full date format to single
	// states such as individual month/year/dates (all numeric values returned)

	// month/year here

	var selMnth_index = document.resLauncher.arrMonth.selectedIndex;
	var selYear_index = document.resLauncher.arrYear.selectedIndex;
	var selMnthYr = new Date(document.resLauncher.arrYear.options[selYear_index].value, document.resLauncher.arrMonth.options[selMnth_index].value);

	var selMnthYr_mnth = selMnthYr.getMonth()-1;
	var selMnthYr_year = selMnthYr.getFullYear();

	// day here

	var selDate_index = document.resLauncher.arrDate.selectedIndex;
	var selDate = new Date(document.resLauncher.arrYear.value, document.resLauncher.arrMonth.value, document.resLauncher.arrDate.value);

	var selMnthYr_dayy = selDate.getDate();

	// Create url
	
	var winURL = "/calendar.html?var_month=" + selMnthYr_mnth + "&var_year=" + selMnthYr_year + "&var_day=" + selMnthYr_dayy;

	// open popup
	
	window.open(winURL,"winCale","toolbar=no,location=no,status=no,directories=no,width=" +width+ ",height=" +height+ ",left=390,top=350,scrollbars=no,resizable=no");
}

/**
 * Sets the form dates
 *
 * @param integer month
 * @param integer year
 * @param integer day
 */
function setFormDates(month, year, day)
{
	// set month
	document.resLauncher.arrMonth.selectedIndex = month;

	// set year
	var select_arrYear_length = document.resLauncher.arrYear.options.length;
	var select_arrYear_index = 0;
	for (m = 0; m < select_arrYear_length; m++) {
		var select_arrYear = document.resLauncher.arrYear.options[m].value;
		if (year == select_arrYear) select_arrYear_index = m;
	}
	document.resLauncher.arrYear.selectedIndex = select_arrYear_index;

	// set day
	document.resLauncher.arrDate.selectedIndex = day - 1;
}
