
$(function() {
	
	//$('form.product select').livequery('change', function() {
	//	$(this).closest('form').ajaxSubmit();
	//});
	
	$('a.post').click(function(e) {
		e.preventDefault();
		var form = $('<form method="post" />').attr('action', $(this).attr('href'));
		form.appendTo('body').hide().submit();
	});
	
	$('form.ajax').ajaxForm();
	
	
});


// push down order summary

// on change of previous address, fill address fields
	

/* 
 * CHECKOUT
 * 
 */
var checkEmailTimeout;
var checkEmail;

$(function() {
	/* 
	 * 
	 * 
	 */
	checkEmailTimeout = setTimeout("", 500);
	checkEmail = function() {
		$.get('/checkuser/', { email: $('#CustomerEmail').val() }, function(responseJson) {
			if (responseJson) {
				$('.login').show();
				$('#RegisterPasswordContainer').hide();
			} else {
				$('.login').hide();
				$('#RegisterPasswordContainer').show();
			}
		}, 'json');
		
	};
	$('#CustomerEmail').keyup(function() {
		clearTimeout(checkEmailTimeout);
		checkEmailTimeout = setTimeout("checkEmail()", 500);
	}).change(function() { $(this).keyup(); }).keyup();
	
	/* 
	 * 
	 * 
	 */
	var updateDelivery = function() {
		var params = { 'sagepay[DeliveryCountryID]': $('#DeliveryCountryID').val() };
		$('.DeliveryTypeID').each(function() {
			params[$(this).attr('name')] = $(this).val();
		});
		params['sagepay[Deliveries]'] = $('.Deliveries:checked').val();
		$.get('?', params);
	};
	$('#DeliveryCountryID, .DeliveryTypeID').live('change', updateDelivery);
	$('.Deliveries').click(updateDelivery).filter(':checked').click();
	//$('.Deliveries:checked').click();
	
	$('#DeliveriesSingle').click(function() {
		$('#DeliveriesSingleSelects').show();
		$('#DeliveriesSplitSelects').hide();
	});
	$('#DeliveriesSplit').click(function() {
		$('#DeliveriesSplitSelects').show();
		$('#DeliveriesSingleSelects').hide();
	});
	
	$('#DeliveryCountryID').change(function() {
		if ($('#DeliveryCountryID option:selected').attr('title') == 'US') $('.DeliveryStateHolder').show();
		else $('.DeliveryStateHolder').hide();
	});
	$('#BillingCountryID').change(function() {
		if ($('#BillingCountryID option:selected').attr('title') == 'US') $('.BillingStateHolder').show();
		else $('.BillingStateHolder').hide();
	}).change();
	
	$('#DeliveryCountryID').change();
	
	/*
	 *
	 *
	 */
	$('#CardType').change(function() {
		if ($(this).val() == 'MAESTRO' || $(this).val() == 'SOLO') {
			$('.maestro').show();
		} else {
			$('.maestro').hide();
		}
		if ($(this).val() == 'AMEX') {
			$('#cv2_hint_amex').show();
			$('#cv2_hint').hide();
		} else {
			$('#cv2_hint_amex').hide();
			$('#cv2_hint').show();
		}
	}).change();
	
	/*
	 *
	 *
	 */
	$('.PreviouslyUsedAddresses').change(function() {
		if($(this).val()) {
			var json;
			eval('json=' + $(this).val());
			var type = $(this).attr('id').replace('Addresses', '');
			
			$('#'+type+'Firstnames').val(json.first_name);
			$('#'+type+'Surname')   .val(json.last_name);
			$('#'+type+'Address1')  .val(json.address_1);
			$('#'+type+'Address2')  .val(json.address_2);
			$('#'+type+'City')      .val(json.city);
			$('#'+type+'State')     .val(json.state);
			$('#'+type+'PostCode')  .val(json.postcode);
			$('#'+type+'CountryID') .val(json.country_id).change();
			$('#'+type+'Phone')     .val(json.phone);
		}
		
	});
	
	/*
	 *
	 *
	 */
	$('#BillingAddressSame, #BillingAddressDifferent').click(function() {
		if ($(this).filter(':checked').val() == 'different') {
			$('#BillingAddress').fadeIn();
		} else {
			$('#BillingAddress').hide();
		}
	}).filter(':checked').click();
	
	/*
	 *
	 *
	 */
	var orderSummaryOffset = 170;
	if (/msie/.test(navigator.userAgent.toLowerCase())) {
		orderSummaryOffset = 140;
	}
	$(window).scroll(function() {
		if ($(window).scrollTop() > orderSummaryOffset) {
			$('#order-summary').css('margin-top', $(window).scrollTop() - orderSummaryOffset);
		}
	});
	
})

