﻿/********************************/
/* hotelResults.js				*/
/*	- Requires ticketDialog.js	*/
/*		or TicketDialog.ascx	*/
/********************************/

/**********************/
/* Abstract Functions */
/**********************/

// function loadHotelListing(subsetIndex, reloadTickets);
// function refreshTicketListing(voucherPricing);

/************/
/* On Ready */
/************/

$(document).ready(function() {
	hotelResults_initialize();
	//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(hotelResults_initialize);
});

function hotelResults_initialize() {
	hotelResults_getContainer().each(function() {
		var $hotelGroups = hotelResults_getHotelGroups(this);

		// If we have enough records to make multiple pages, initialize the nagivation and hide the records following the first 10
		if ($hotelGroups.length > 10) {
			hotelResults_initializeNavigation(hotelResults_getContent(this), $hotelGroups, hotelResults_getNavigation(this), 1, Math.ceil($hotelGroups.length / 10));
			$hotelGroups.slice(10).slideUp(600);
		}

		hotelResults_getLoading(this).slideUp(600);
	});
}

function hotelResults_refresh($hotelResults, subsetIndex, reloadTickets) {
	// Get containers
	var $content = hotelResults_getContent($hotelResults);
	var $loading = hotelResults_getLoading($hotelResults);
	var $navigation = hotelResults_getNavigation($hotelResults);

	// Load attributes
	var type = parseInt($hotelResults.attr('result-type'));
	var serviceCall = $hotelResults.attr('service-call');
	var parameters = $hotelResults.attr('service-parameters');

	// Hide navigation and content
	$navigation.slideUp(600);
	$content.slideUp(600, function() {
		// If we're loading by location (type == 1), include subset index as parameter
		if (type == 1) {
			if (subsetIndex != null) {
				parameters = subsetIndex + ', ' + parameters;
			}
			else {
				parameters = '1, ' + parameters;
			}
		}

		// If we have an explicit value for reloading tickets, use it, else default to true
		if (reloadTickets) {
			parameters = parameters + ', ' + reloadTickets;
		}
		else {
			parameters = parameters + ', true';
		}

		// Call function for loading results from subset
		$loading.slideDown(600, function() {
			hotelResults_displaySubset($content, $loading, $navigation, serviceCall, parameters);
		});
	});
}

/**************/
/* Call Backs */
/**************/

// Call WCF Service that returns subset to call back function for populating container
function hotelResults_displaySubset($content, $loading, $navigation, serviceCall, parameters) {
	// Evaulate service call and its parameters dynamically, then pass results to anonymous call back function
	eval(serviceCall + '(' + parameters + ', ' + function(subset) {
		planVacation_initializeButtonToggling();
		$content.html('');

		if (subset.Results.length == 0) {
			$('#noResultsMessage').show();
		}
		else {
			$('#noResultsMessage').hide();

			// Populate results
			$content.setTemplateURL('/Snippets/HotelResults/hotelResults.htm');
			$content.processTemplate(subset);

			var $hotelGroups = hotelResults_getHotelGroups($content);
			$hotelGroups.hide().slice((subset.CurrentSubset - 1) * 10, subset.CurrentSubset * 10).show();
			getMapsLink().slideDown(300);

			// Calculate and refresh selectable tickets if available
			if (subset.VoucherPricing.SelectableVouchers.length > 0) {
				refreshTicketListing(subset.VoucherPricing);
			}
		}

		$loading.slideUp(600, function() {
			planVacation_getActionButton().siblings('img').remove();
			$content.slideDown(600);

			// If applicable, initialize navigation
			if (subset.TotalSubsets > 1) {
				hotelResults_initializeNavigation($content, $hotelGroups, $navigation, subset.CurrentSubset, subset.TotalSubsets);
			}
		});
	} + ', ' + hotelResults_displayError + ', ' + hotelResults_displayError + ');');
}

// Callback function for displaying WCF Service error
function hotelResults_displayError(error, context, methodName) {
	alert(error.get_message());
}

/**************/
/* Navigation */
/**************/

function hotelResults_initializeNavigation($content, $hotelGroups, $navigation, subsetIndex, pageCount) {
	// Remove existing navigation and replace it according to passed parameters
	$navigation.children().remove();
	$navigation.html('').append(hotelResults_generateNavigation(subsetIndex, pageCount)).slideDown(600);

	$('a', $navigation).click(function(event) {
		// Prevent default and get the subset index of the navigation item clicked
		event.preventDefault();
		subsetIndex = parseInt($(this).attr('tag'));

		$navigation.slideUp(600);
		$content.slideUp(600, function() {
			// If the selected index is valid, load a slice of records accordingly, else load all records
			if (subsetIndex > 0) {
				$hotelGroups.hide().slice((subsetIndex - 1) * 10, subsetIndex * 10).show();
			}
			else {
				$hotelGroups.show();
			}

			// Initialize the new navigation according to the new selection
			hotelResults_initializeNavigation($content, $hotelGroups, $navigation, subsetIndex, pageCount);
			$content.slideDown(600);
		});
	});
}

function hotelResults_generateNavigation(subsetIndex, pageCount) {
	var contents;

	if (subsetIndex > 0) {
		contents = hotelResults_generateNavigationItem(subsetIndex - 1, 'Previous', subsetIndex > 1)
			+ ' | ';

		for (var index = 1; index <= pageCount; index++)
			contents += hotelResults_generateNavigationItem(index, index, index != subsetIndex);

		contents += ' | '
			+ hotelResults_generateNavigationItem(subsetIndex + 1, 'Next', subsetIndex < pageCount)
			+ '&nbsp;&nbsp;&nbsp;'
			+ hotelResults_generateNavigationItem(0, '(Show All)', true);
	}
	else
		contents = hotelResults_generateNavigationItem(1, 'Show Pages', true);

	return contents;
}

function hotelResults_generateNavigationItem(index, label, isActiveLink) {
	var contents = '<span>';

	if (isActiveLink)
		contents += '&nbsp;<a href="javascript:{}" tag="' + index + '">' + label + '</a>&nbsp;';
	else
		contents += '&nbsp;' + label + '&nbsp;';

	return contents + '</span>';
}

/*************/
/* Accessors */
/*************/

function hotelResults_getContainer() {
	return $('.hotelResults');
}

function hotelResults_getContent(parent) {
	return $('.hotelResults_content', parent);
}

function hotelResults_getHotelGroups(parent) {
	return $('.hotelGroup', parent);
}

function hotelResults_getLoading(parent) {
	return $('.hotelResults_loading', parent);
}

function hotelResults_getNavigation(parent) {
	return $('.hotelResults_navigation', parent);
}