var updateTotals = function(e) {
	var totalAmount = 0, orderListHtml = '<ul class="items">';

	$('.orderAmount').each(function() {
		var quantity = parseInt($(this).val());
		if (quantity>0) {
			totalAmount += (quantity * parseFloat($(this).parent().parent().find('.productPrice').val()));
			orderListHtml += '<li>'+quantity+' x <a href="#'+$(this).parent().attr('id')+'">'+$(this).parent().parent().find('.productDescription b').html()+'</a></li>';
		}
	});

	if (totalAmount==0) orderListHtml = '<i>U heeft nog geen producten op de bestellijst</i>';
	$('#orderList').html(orderListHtml+'</ul><div id="orderListTotal">Totaal &euro;&nbsp;'+number_format(totalAmount,2,',','.')+'</div>');
	$('#totalAmount').html('&euro;&nbsp;'+number_format(totalAmount,2,',','.'));
	if (totalAmount>0) $.cursorMessage('Totaalbedrag: <b>&euro;&nbsp;'+number_format(totalAmount, 2, ',', '.')+'</b>', {hideTimeout: 4000});
}

var handleKeyEvent = function(e) {
	if (e) if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))	return false;
	updateTotals();
}

function toggleBilling() {
	if ($('#differentBillingAddress').attr('checked')) {
		$('#billingInfo input').val('');
		$('#billingInfo').show();
	}
	else $('#billingInfo').hide();
}

function initForm() {
	$('input.orderAmount').keypress(handleKeyEvent);
	$('input.orderAmount').keyup(handleKeyEvent);

	$('input').click(updateTotals);
	updateTotals();

	$('#startDate').datepicker({showOn: 'both', buttonImageOnly: true, buttonImage: '/img/icons/famfamfam/calendar.png', dateFormat: 'dd-mm-yy', minDate: new Date()});

	$('#differentBillingAddress').click(toggleBilling);
	if ($('#differentBillingAddress').attr('checked')) $('#billingInfo').show();
	else $('#billingInfo').hide();

	if (!$('#differentBillingAddress').attr('checked')) {
		$('#eventAddress input[type="text"]').blur(function() {
			$('#billing'+$(this).attr('id').substring(0, 1).toUpperCase()+$(this).attr('id').substring(1)).val($(this).val()); // change the first letter of the id, make it uppercase and add #billing to the front.
		});
	}

	$('#orderForm').submit(function() {
		if (!$('#differentBillingAddress').attr('checked')) {
			$('#billingInfo input').each(function() {
				if ($(this).val() == '') {
					$(this).val($('#'+$(this).attr('id').substring(7, 8).toLowerCase()+$(this).attr('id').substring(8)).val());
				}
			});
		}
	});

	$('.collectionHeader').click(function() {
		var id = $(this).attr('id').split('_')[1];
		$('.collectionRow'+id).toggle();
	});

	$('.collectionHeader').mouseover(function() {
		$(this).addClass('hoverHeader');
	});
	$('.collectionHeader').mouseout(function() {
		$(this).removeClass('hoverHeader');
	});

	$('.list a').click(function() {
		$('.collectionRow').hide();
		$('.collectionRow'+$(this).attr('id').split('_')[1]).show();
		return true;
	});
	// update if returned to this page
	updateTotals();
};

$(document).ready(function() {
	$.ajaxSetup({cache: false});
	$('.orderNow').click(function() {
		var value = parseInt($(this).parents('.collectionRow').find('.orderAmount').val());
		if (isNaN(value) || ( value < 1)) $(this).parents('.collectionRow').find('.orderAmount').val(1);
		return true;
	});

	$('.orderAmount').focus(function() {
		if ($(this).val() == '') $(this).attr('value', 1);
	});

	$('#orderList').load('/cart&localAction=cartBox');

	$('#leftColumn.wide .boxContent a').cycle({fx: 'fade', sync: 1, timeout: 10000, speed: 5000, pause: 1});
	$('.lightBox').lightBox();
	$('#eventDetailsForm').validate();
	$('#processCustomerForm').validate();
	$('#differentBillingAddress').click(function() {
		$('#billingInfo input').toggleClass('required');
		$('#billingInfo').slideToggle();
	});
	$('#startDate').datepicker({dateFormat: 'dd-mm-yy'});
	$('.lightBox').lightBox();
});