﻿/************/
/* On Ready */
/************/

$(document).ready(function () {
	planVacation_initializeChildCountChange();
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(planVacation_initializeChildCountChange);
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

	planVacation_loadForCheckinDate();
});

function endRequestHandler() {
	planVacation_destinationChange("afterPostBack");
}

function planVacation_initializeChildCountChange() {
	var $ddlChildCount = $('[ID*=ddlChildCount]');
	planVacation_toggleChildAges($ddlChildCount.val());
}

function planVacation_initializeButtonToggling() {
	planVacation_hidePlanVacationButton();

	$('select,[type=text]', '.vacationPlanner').not('[ID*=txtSearch]').change(function() {
		$('select,[type=text]', '.vacationPlanner').unbind();
		planVacation_showPlanVacationButton();
		$('.hotelResultsContent').slideUp('slow');
	});

	$('[ID*=txtSearch]', '.vacationPlanner').keypress(function() {
		$('select,[type=text]', '.vacationPlanner').unbind();
		planVacation_showPlanVacationButton();
	});
}

function planVacation_loadForCheckinDate() {
	var stepPlanner = $('[ID*=stepPlanner]');
	var stdPlanner = $('[ID*=standardPlanner]');
	var $calendar = $('.slimCalendar[ID*=clrCheckIn]');
	var value = new Date(slimCalendar_getValue($calendar)).format('M/dd/yyyy');

	if (stepPlanner.length > 0)
		planVacation_stpLoadForCheckinDate($calendar, value);

	if (stdPlanner.length > 0) 
		planVacation_stdLoadForCheckinDate($calendar);
}

function planVacation_hideShowDateLabel() {
	if ($('.slimCalendar').is(':hidden'))
		$('[ID*=lblSelectedDate]').text('');
	else {
		$('[ID*=lblSelectedDate]').text($('[ID*=hdnSelectedDate]').val());
	}
}

/********************/
/* Vacation Planner */
/********************/

function planVacation_toggleChildAges(count) {
	var $pnlChildren = $('.vacationPlanner_childAges_container');
	var $childAges = $('.vacationPlanner_childAges_items_child', $pnlChildren);
	var prevChildCount = parseInt($('[ID*=hdnChildCount]').val());

	if (count == 0) {
		$pnlChildren.slideUp(500);
		$childAges.hide();
	}
	else {
		$childAges.slice(0, count).show();
		$childAges.slice(count).hide();

		if (prevChildCount == NaN || prevChildCount == 0)
			$pnlChildren.slideDown(500);
		else
			$pnlChildren.show();
	}

	$('[ID*=hdnChildCount]').val(count);
	planVacation_showPlanVacationButton();
}

/********************/
/* Standard Planner */
/********************/

function planVacation_updateCheckInDate($clrCheckIn, value) {
	var $clrCheckOut = $('.slimCalendar[ID*=clrCheckOut]');

	if ($clrCheckOut.length > 0) {
		var nightCount = planVacation_getNightCount();
		var checkInDate = new Date(slimCalendar_getValue($clrCheckIn));
		var checkOutDate = new Date(slimCalendar_getValue($clrCheckOut));
		var nextDate = new Date(value);

		//If the checkin date is less than default checkin date, hide the middle and bottom panels, and show a panel with a message telling the user to call.
		planVacation_stdLoadForCheckinDate($clrCheckIn);
		
		// If the minimum date is greater than the check out date, move it out according to the night count, else just change the night count
		if (nextDate >= checkOutDate) {
			// Set selected check out date
			nextDate.setDate(nextDate.getDate() + nightCount);
			slimCalendar_setValue($clrCheckOut, nextDate.format('M/dd/yyyy'));
			$('[ID*=txtCheckOut]').val(nextDate.format('M/dd/yyyy')).change();
		}
		else {
			nightCount = planVacation_calculateNightCount(checkInDate, checkOutDate);
			// if our new nightcount is 1, it needs to be *at least* 2, so update and reset
			if (nightCount == 1) {
				nextDate.setDate(nextDate.getDate() + 2);
				slimCalendar_setValue($clrCheckOut, nextDate.format('M/dd/yyyy'));
				$('[ID*=txtCheckOut]').val(nextDate.format('M/dd/yyyy')).change();
				checkOutDate = nextDate;
			}

			planVacation_resetNightCount(checkInDate, checkOutDate);
		}
		
		delete checkInDate;
		delete checkOutDate;
		delete nextDate;
	}
}

function planVacation_updateCheckOutDate($clrCheckOut, value) {
	var checkInDate = new Date(slimCalendar_getValue($('.slimCalendar[ID*=clrCheckIn]')));
	var checkOutDate = new Date(value);

	planVacation_resetNightCount(checkInDate, checkOutDate);
	
	delete checkInDate;
	delete checkOutDate;
}

function planVacation_resetNightCount(checkInDate, checkOutDate) {
	planVacation_setNightCount(planVacation_calculateNightCount(checkInDate, checkOutDate));
//	planVacation_setNightCount(Math.round((checkOutDate.getTime() - checkInDate.getTime()) / (1000 * 60 * 60 * 24)));
}

function planVacation_calculateNightCount(checkInDate, checkOutDate) {
	return Math.round((checkOutDate.getTime() - checkInDate.getTime()) / (1000 * 60 * 60 * 24));
}

function planVacation_stdLoadForCheckinDate($calendar) {
	var selectDate = new Date(slimCalendar_getValue($calendar));
	var selectDay = selectDate.getDate();
	var selectMonth = selectDate.getMonth();
	var selectYear = selectDate.getFullYear();

	var defaultDate = new Date(slimCalendar_getMinimumValue($calendar));
	var defaultDay = defaultDate.getDate() + 3;
	var defaultMonth = defaultDate.getMonth();
	var defaultYear = defaultDate.getFullYear();

	var checkOutDate = $('.slimCalendar[ID*=clrCheckOut]');
	var minCheckOutDate = new Date(selectDate.valueOf() + (1000 * 60 * 60 * 48)); // 2 days
	slimCalendar_setMinimumValue(checkOutDate, minCheckOutDate);

	delete defaultDate;

	if (selectYear == defaultYear && selectMonth == defaultMonth && selectDay < defaultDay) {
		$('[ID*=pnlMiddle]').css('display', 'none');
		$('[ID*=pnlBottom]').css('display', 'none');
		$('[ID*=pnlContactUs]').css('display', 'inline');
		$('#sortOptionsBox').hide();
		$('.bblHotelListings').css('display', 'none');
	}
	else {
		$('[ID*=pnlMiddle]').css('display', 'inline');
		$('[ID*=pnlBottom]').css('display', 'inline');
		$('[ID*=pnlContactUs]').css('display', 'none');
		$('#sortOptionsBox').show();
		$('.bblHotelListings').css('display', 'inline');
	}
}

/************************************************************/
/* Standard Planner: Destination Drop-down OnChange Methods */
/************************************************************/
function planVacation_destinationChange(state) {
	$('.vacationPlanner_hasNonBookable').each(function () {
		var $ddlDestination = $('[ID*=ddlDestination]', this);
		var $panels = $('.vacationPlanner_bookable', this);
		var $hidePanels = $('[ID*=hdnBookingPanels]').val();
		var $bookable = $('option:selected', $ddlDestination).attr("Bookable");
		
		if (document.URL.search("GroupInformationRequest") == -1) {
			if (state == "beforePostBack")
				planVacation_destChangeBeforePostBack($bookable, $panels, $hidePanels);
			else
				planVacation_destChangeAfterPostBack($bookable, $panels);
		}
	});
}

function planVacation_destChangeBeforePostBack($bookable, $panels, $hidePanels) {
	if ($bookable == "True") {
		if ($hidePanels != "1") {
			$panels.slideUp(0).slideDown(500);
			$('[ID*=hdnBookingPanels]').val('1');
		}
	}
	else
		if ($hidePanels != "0") {
			$panels.slideDown(0).slideUp(500);
			$('[ID*=hdnBookingPanels]').val('0');
		}
		else
			$panels.slideUp(0);
}

function planVacation_destChangeAfterPostBack($bookable, $panels) {
	if ($bookable == "True") {
		$panels.slideDown(0);
	}
	else
		$panels.slideUp(0);
}

/****************/
/* Step Planner */
/****************/

function planVacation_updateTravelDate($calendar, value) {
	//update the date label
	$('[ID*=hdnSelectedDate]').val(value);
	$('[ID$=lblSelectedDate]').text(value);
	
	planVacation_stpLoadForCheckinDate($calendar);
}

function planVacation_stpLoadForCheckinDate($calendar) {
	var selectDate = new Date(slimCalendar_getValue($calendar));
	var defaultDate = new Date(slimCalendar_getMinimumValue($calendar));
	var defaultDay = defaultDate.getDate() + 3;
	var defaultMonth = defaultDate.getMonth();
	var defaultYear = defaultDate.getFullYear();
	var minValidDate = new Date(defaultYear, defaultMonth, defaultDay);

	//update the stepPlanner sections
	if (selectDate.getTime() < minValidDate.getTime()) {
		var value = minValidDate.format('M/dd/yyyy');
		slimCalendar_setValue($calendar, value);
		planVacation_minDateDialog($calendar, value);
	}
	else {
//		__doPostBack($calendar.attr('id'), '');
	}
}

/*****************/
/* Plan Vacation */
/*****************/

function planVacation_showPlanVacationButton() {
	$('[ID*=btnPlanVacation]:hidden').fadeIn(500);
}

function planVacation_hidePlanVacationButton() {
	$('[ID*=btnPlanVacation]:visible').hide();
}

/**********/
/* Dialog */
/**********/

function planVacation_dialog_display(title, message) {
	var $dialog = $('#vacationPlannerDialog');

	if ($dialog.dialog('isOpen') == false) {
		$dialog.dialog('open');
	}
	else {
		$dialog.dialog({
			resizable: false,
			modal: true,
			buttons: {
				'Ok': function () {
					$(this).dialog('close');
				}
			}
		});
	}

	$('#dialogBody').html(message);
	$dialog.dialog('option', 'title', title);
}

function planVacation_error_display(title, error) {
	$('#vacationPlannerError').append(error).dialog({
		resizable: false,
		modal: true,
		buttons: {
			'Ok': function() {
				$(this).html('').dialog('close');
			}
		}
	}).dialog('option', 'title', title);
}

function planVacation_minDateDialog($calendar, value) {
	var $dialog = $('#minDateDialog');
	var title = $('[ID*=lblDialogTitle]').text();
	
	$dialog.dialog({
		title: title,
		resizable: false,
		modal: true,
		closeOnEscape: false,
		buttons: {
			'Ok': function () {
				$(this).dialog('close');
			}
		},
		beforeclose: function () {
			$('[ID*=hdnSelectedDate]').val(value);
			$('[ID*=lblSelectedDate]').text(value);
		}
	}).dialog('open');
}

/************************/
/* Accessors & Mutators */
/************************/

function planVacation_getActionButton() {
	return $('[ID*=btnPlanVacation]');
}

function planVacation_getDestinationID() {
	return $('[ID*=ddlDestination]').val();
}

function planVacation_getCheckInDate() {
	return slimCalendar_getValue($('.slimCalendar[ID*=clrCheckIn]'));
}

function planVacation_getNightCount() {
	return parseInt($('[ID*=txtNightCount]').val());
}

function planVacation_setNightCount(value) {
	if (value > 21) {		
		planVacation_dialog_display($('[ID*=hdnNightCountMaxTitle]').val(), $('[ID*=nightCountMaxMessage]').html().replace('####', value));

		$('[ID*=pnlMiddle]').css('display', 'none');
		$('[ID*=pnlBottom]').css('display', 'none');
		$('[ID*=pnlContactUs]').css('display', 'inline');
		$('#sortOptionsBox').hide();
		hotelResults_collapse();
	}
	else {
		$('[ID*=pnlMiddle]').css('display', 'inline');
		$('[ID*=pnlBottom]').css('display', 'inline');
		$('[ID*=pnlContactUs]').css('display', 'none');
		$('#sortOptionsBox').show();
	}

	$('[ID*=txtNightCount]').val(value).change();
}

function planVacation_getAdultCount() {
	return $('[ID*=ddlAdultCount]').val();
}

function planVacation_getChildAges() {
	var $ddlChildAge = $('[ID*=ddlChildAge]:visible');
	var childAges = new Array($ddlChildAge.length);

	if (childAges.length > 0) {
		var pos = 0;

		$ddlChildAge.each(function() {
			if (pos >= $ddlChildAge.length) {
				return false;
			}

			childAges[pos++] = $(this).val();
		});
	}

	return childAges;
}

function planVacation_getSearchText() {
	return $('[ID*=txtSearch]').val();
}
