$(document).ready(function() {
// table row striping (TEMP)
$("tr:even").addClass("alt");

// add item functionality
$('#pass-selector button').click(function()
{

	var pass_type = $('#pass-selector select :selected').text();
	var pass_amount = $('#pass-selector select').val();
	var pass_quantity = $('#pass-selector input').val();
	var current_ticket_total = (pass_amount * pass_quantity);
	
	if (pass_type != '' && pass_amount != '' && _is_numeric(pass_quantity)) 
	{
		
		// insert order item
		if ($("ol#order-items").length > 0) 
		{ 
		
			// flag for NEW or EXISTING item(s)
			var current_item = false;

			$("ol#order-items li").each(function()
			{
				var current_pass_type = $(this).find('strong:first').html();
				var current_pass_quantity = parseInt($(this).find('span:first').html());
				var current_pass_amount = parseInt($(this).find('span:eq(1)').html());	
				// alert('Pass Type: ' + current_pass_type + ' Pass Amount: $' + current_pass_amount + ' Pass Quantity: ' + current_pass_quantity);
				
				// update quantity of EXISTING order list item
				if (current_pass_type == pass_type)
				{
					$(this).find('span:first').html((parseInt(current_pass_quantity) + parseInt(pass_quantity)));
					current_item = true;
					return false;
				}
			});

			if (!current_item) {
				// add NEW item to current orders list
				$('<li><strong>' + pass_type + '</strong> <span>' + pass_quantity + '</span> @ $<span>' + pass_amount + '</span> <a href="#delete-pass" title="remove pass"><img src="http://74.53.234.34/~rjbteam/skitheloup.com/themes/skitheloup/images/icon-delete.png" alt="remove pass" width="16" height="16"/></a></li>').insertAfter($('ol#order-items li:last'));
			}

		} else { 		
			// create new orders list with first item
			$('<ol id="order-items"><li><strong>' + pass_type + '</strong> <span>' + pass_quantity + '</span> @ $<span>' + pass_amount + '</span> <a href="#delete-pass" title="remove pass"><img src="http://74.53.234.34/~rjbteam/skitheloup.com/themes/skitheloup/images/icon-delete.png" alt="remove pass" width="16" height="16"/></a></li></ol>').insertBefore($('p#order-total'));
		}
		
		// refresh hidden cart items list
		_refresh_ssl_cart_items();
		
		// calculate order total
		order_total = parseInt($('p#order-total span').html()) + current_ticket_total;
		
		// update order total(s)
		$('input#ssl_amount').val(order_total);
		$('p#order-total span').html(order_total);
		
		// show the order total
		$('p#order-total').fadeIn('fast');
		
		// reset form values		
		$('#pass-selector input').val('1');
		$('#pass-selector select').val('');
		
	} else {
		_verify_selection();
	}
	
});

$("ol#order-items li a").live("click", function()
{
	var pass_quantity = parseInt($(this).parent().find('span:first').html());
	var pass_amount = parseInt($(this).parent().find('span:eq(1)').html());	
	// alert('Pass Amount: $' + pass_amount + ' Pass Quantity: ' + pass_quantity);
	
	if (pass_quantity == 1) 
	{
		// remove entire row
		$(this).parent().remove();
	} else {
		// increment total -1
		pass_quantity = (pass_quantity - 1);
		$(this).parent().find('span:first').html(pass_quantity);
	}
	
	// re-calculate order total
	order_total = (parseInt($('p#order-total span').html()) - pass_amount);
	
	// update order total(s)
	$('p#order-total span').fadeOut('fast').html(order_total).fadeIn('fast');	
	$('input#ssl_amount').val(order_total);
	
	// clear/remove DOM element values if no order items in list
	if (order_total == 0) {
		$("ol#order-items").remove();
		$('input#ssl_cart_items').val('');
		$('p#order-total').hide();
	} else {
		// refresh hidden cart items list
		_refresh_ssl_cart_items();	
	}
	
});

$("form#purchase-form").submit(function()
{
	if ($("#ssl_amount").val() == 0) 
	{
		_verify_selection();
		return false;
	} else {
		return true;
	}
});

// helper functions
function _is_numeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) 
    { 
        return false; 
    } else {
        return true; 
	}
}

function _verify_selection() 
{
	$('<p class="error">Please verify your selection and try again!</p>').insertAfter($('#pass-selector button')).animate({opacity: 1.0}, 2000).fadeOut('fast');
}

function _refresh_ssl_cart_items() 
{
	var current_cart_items = '';
	
	if ($("ol#order-items").length > 0) 
	{
	
		// refresh hidden order items list
		$("ol#order-items li").each(function()
		{
			var item_pass_type = $(this).find('strong:first').html();
			var item_pass_quantity = parseInt($(this).find('span:first').html());
			var item_pass_amount = parseInt($(this).find('span:eq(1)').html());			
			
			current_cart_items += "<p>Pass Type: " + item_pass_type + "<br /> Pass Quantity: " + item_pass_quantity + "<br /> Pass Amount: $" + item_pass_amount + "</p>";
			
		});
	
	}
	
	$('input#ssl_cart_items').val(current_cart_items);
	//alert(current_cart_items);
	
}
});