﻿/********************************/
/* ticketDialog.js				*/
/*	- Designed for Control		*/
/*		TicketDialog.ascx		*/
/********************************/

/************/
/* On-Ready */
/************/

$(document).ready(function() {
	$('#ticketDialog').dialog({
		autoOpen: false,
		width: 750,
		resizable: false,
		modal: true
	});
});

/**********/
/* Dialog */
/**********/

function openTicketDialog(ticketID) {
	$('#ticketDialog').dialog('option', 'title', getTicketDialogTitle());
	$('#ticketDialog').dialog('open');

	var ticketRows = $('.ticketRow');

	if (ticketID)
		var ticketPrice = $('#ticketPrice_' + ticketID).val();

	$('.selectableVoucher').each(function() {
		var voucher = $(this);
		if (ticketID == voucher.val())
			voucher.siblings().html('Included');
		else {
			var price = $('#ticketPrice_' + voucher.val()).val();

			if (ticketPrice)
				price -= ticketPrice;

			voucher.siblings().html('$' + (price / 100.00).toFixed(2));
		}
	});

	// Center all column content according to height of tallest column
	for (var index = 0; index < ticketRows.length; index++) {
		var maxHeight = Math.max(ticketRows[index].childNodes[0].offsetHeight, ticketRows[index].childNodes[1].offsetHeight,
			ticketRows[index].childNodes[2].offsetHeight);

		// Iterate through divs and set css top margin against height of tallest for centering
		for (var childIndex = 0; childIndex < ticketRows[index].childNodes.length; childIndex++) {
			var marginHeight = (maxHeight - ticketRows[index].childNodes[childIndex].offsetHeight) / 2;
			$(ticketRows[index].childNodes[childIndex]).css('margin-top', marginHeight);
		}
	}
}

function closeTicketDialog() {
	$('#ticketDialog').dialog('close');
}

/********/
/* Main */
/********/

function refreshTicketListing(tickets) {
	var ticketListing = $('#ticketListing');
	ticketListing.html('');

	for (var index = 0; index < tickets.length; index++) {
		var ticketVoucher;

		if (index % 2 == 0)
			ticketVoucher = $('<div class="ticketItem"></div>');
		else
			ticketVoucher = $('<div class="ticketItem_alternate"></div>');

		var template = $("#ticketVoucherTemplate").html();
		ticketVoucher.setTemplate(template);
		ticketVoucher.processTemplate(tickets[index]);

		ticketListing.append(ticketVoucher);
	}
}

/**************************/
/* Accessors and Mutators */
/**************************/

function getTicketDialogTitle() {
	return $('[ID*=hdnTicketDialogTitle]').val();
}

function setTicketDialogTitle(value) {
	$('[ID*=hdnTicketDialogTitle]').val(value);
}

function getTicketID() {
	var ticketID = $('[ID*=hdnTicketID]').val();

	if (ticketID)
		return ticketID;
	else
		return '0';
}

function setTicketID(value) {
	$('[ID*=hdnTicketID]').val(value);
}
