$(document).ready(function() {
	$('#ClearBasket').click(function() {
		$.ajax({
			url: 'http://www.peternashphotography.com/index.php/ClearBasket/',
			type: 'post',
			dataType: 'html',
			success: function (data) {
				$('#MyBasket').fadeOut('fast',function() {
					$('#MyBasket').replaceWith(data);
					$('.BasketActions').remove();
					$('#BasketLink').text('Your Basket');
				});
			}
		})
	});
	$('#UpdateBasket').click(function() {
		$('#MyBasket').animate({opacity: 0.5}, 500);
		$.ajax({
			url: 'http://www.peternashphotography.com/index.php/UpdateBasket/',
			type: 'post',
			data: $('#BasketForm').serialize(),
			dataType: 'html',
			success: function (data) {
				$('#MyBasket').animate({opacity: 1}, 250);
				$('#MyBasket').replaceWith(data);
			}
		})
	});
	$('.AddItemForm').submit(function() {
		$('[name=jquerySubmit]').val('yes');
		var submitButton = $('input[type=submit]', this);
	        btnText = $(submitButton).attr("value");
        	$(submitButton).attr("value", "Please Wait...");
	        $(submitButton).attr("disabled", "disabled");
		jQuery.ajax({
			url: this.action,
			data: $('form').serialize(),
			type: 'post',
			dataType: 'json',
			success: function(data) {
				if (data.BasketTotal !=="0") {
					$('#BasketLink').text('Your Basket ('+data.BasketTotal+')'); // 
				}

				var $alert = $('#alert');
				$alert.html(data.ItemQTY + 'x '+data.ItemName + ' added to your basket.');
				if($alert.length) {
					var alerttimer = window.setTimeout(function () {$alert.trigger('click');}, 3000);
					$alert.animate({height: $alert.css('line-height') || '50px'}, 200)
						.click(function () {
							window.clearTimeout(alerttimer);
							$alert.animate({height: '0'}, 200);
					});
				}
				$(submitButton).attr("value", 'Add To Basket');
				$(submitButton).removeAttr("disabled");
			},
			failure: function() {alert('A problem has been encountered.');}
		});
		return false;
	});
	$('.slideshow').cycle({
		fx: 'fade'
	});
});
