﻿/************/
/* On Ready */
/************/

$(document).ready(function() {
	planVacation_initializeDestinationChange();
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(planVacation_initializeDestinationChange);

	planVacation_initializeChildCountChange();
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(planVacation_initializeChildCountChange);
});

function planVacation_initializeDestinationChange() {
	$('.vacationPlanner_hasNonBookable').each(function() {
		var $ddlDestination = $('[ID*=ddlDestination]', this);
		var $panels = $('.vacationPlanner_bookable', this);

		$ddlDestination.change(function(event) {
			if ($('option:selected', $ddlDestination).attr("Bookable") == "True") {
				$panels.slideDown(500);
			}
			else {
				$panels.slideUp(500);
			}
		});
	});
}

function planVacation_initializeChildCountChange() {
	var $ddlChildCount = $('[ID*=ddlChildCount]');
	planVacation_toggleChildAges($ddlChildCount);

	$ddlChildCount.change(function(event) {
		planVacation_toggleChildAges($ddlChildCount);
	});
}

function planVacation_initializeButtonToggling() {
	planVacation_hidePlanVacationButton();

	$('select,[type=text]', '.vacationPlanner').change(function() {
		planVacation_showPlanVacationButton();
		$('.hotelResultsContent').slideUp('slow');
	});

	$('[ID*=txtSearch]', '.vacationPlanner').keypress(function() {
		planVacation_showPlanVacationButton();
	});
}

/********************/
/* Vacation Planner */
/********************/

function planVacation_toggleChildAges($this) {
	var count = $this.val();
	var $pnlChildren = $('.vacationPlanner_childAges_container');
	var $childAges = $('.vacationPlanner_childAges_items_child', $pnlChildren);

	if (count == 0) {
		$pnlChildren.slideUp(500);
		$childAges.hide();
	}
	else {
		$childAges.slice(0, count).show();
		$childAges.slice(count).hide();
		$pnlChildren.slideDown(500);
	}
}

/********************/
/* Standard Planner */
/********************/

function planVacation_updateCheckInDate($clrCheckIn, value) {
	var $clrCheckOut = $('.slimCalendar[ID*=clrCheckOut]');
	var nightCount = planVacation_getNightCount();
	var nextDate = new Date(value);

	// Set minimum date
	nextDate.setDate(nextDate.getDate() + 2);
	slimCalendar_setMinimumValue($clrCheckOut, nextDate.format('M/dd/yyyy'));

	// Set selected check out date
	nextDate.setDate(nextDate.getDate() + nightCount - 2);
	slimCalendar_setValue($clrCheckOut, nextDate.format('M/dd/yyyy'));
	$('[ID*=txtCheckOut]').val(nextDate.format('M/dd/yyyy')).change();

	// Set maximum date
	nextDate.setDate(nextDate.getDate() + 15 - nightCount);
	slimCalendar_setMaximumValue($clrCheckOut, nextDate.format('M/dd/yyyy'));

	delete nextDate;
}

function planVacation_updateCheckOutDate($clrCheckOut, value) {
	var checkInDate = new Date(slimCalendar_getValue($('.slimCalendar[ID*=clrCheckIn]')));
	var checkOutDate = new Date(value);
	
	$('[ID*=txtNightCount]').val(Math.ceil((checkOutDate.getTime() - checkInDate.getTime()) / (1000 * 60 * 60 * 24))).change();
	
	delete checkInDate;
	delete checkOutDate;
}

/****************/
/* Step Planner */
/****************/

function planVacation_updateTravelDate($calendar, value) {
	$('[ID*=hdnHasDateChanged]').val('1');
}

/*****************/
/* Plan Vacation */
/*****************/

function planVacation_showPlanVacationButton() {
	$('[ID*=btnPlanVacation]').fadeIn(500);
}

function planVacation_hidePlanVacationButton() {
	$('[ID*=btnPlanVacation]').hide();
}

/**********/
/* Dialog */
/**********/

function planVacation_dialog_display() {
	var $dialog = $('#vacationPlannerDialog');

	if ($dialog.dialog('isOpen') == false) {
		$dialog.dialog('open');
	}
	else {
		$dialog.dialog({
			resizable: false,
			modal: true,
			buttons: {
				'Ok': function() {
					$(this).dialog('close');
				}
			}
		});
	}

	$dialog.dialog('option', 'title', $('[ID*=lnkDialog]').html());
}

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);
}

/************************/
/* 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 $('[ID*=ddlNightCount]').val();
	return parseInt($('[ID*=txtNightCount]').val());
}

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();
}