// This is a JQuery document that updates the basket and also displays it.
$(document).ready(function() {
	// This stops any forms with the class .product-form from being submitted
	$(".product-form").submit(function() { return false; });
	$("form").live('submit', function() { return false; });
	
	// Hides the basket until its been updated
	$("#basket").hide();
	
	$('.jq-basket-progress-icon').hide();
	
	// Updates the current basket on every page
	updateBasket();

	// This reloads the basket with the correct data but does not update quantities
	function updateBasket() {

		// Set to http://82.69.115.147 if theres a problem!
		var websiteurl = $("#websiteurl").val();
		$.post(websiteurl+"classes/displaybasket.inc.php", null,
			function(returned_data){
				var mySplitResult = returned_data.split("[[DIVIDER]]");
				$('#basket-header').html(mySplitResult[0]);
				$('#basket-messages').html(mySplitResult[1]);
				$('#basket-items').html(mySplitResult[2]);
				$("#basket").fadeIn();
			}
			, "html");

	}
	
	function flashProduct() {
		
		if ($('.flash').length) { // implies *not* zero E1F0D2
			$(".flash").animate(  { 'backgroundColor':'#ffffff'}, 800 )
						.animate( { 'backgroundColor':'#bcdcff'}, 800 )
						.animate( { 'backgroundColor':'#ffffff'}, 800 )
						.animate( { 'backgroundColor':'#bcdcff'}, 800 )
						.animate( { 'backgroundColor':'#ffffff'}, 800 );
		}
	}
	
	// Do ALL this when a buy button is clicked!
	$(".buy-button").click( function(event) {
		
		// Show the spinnder as its loading
		$('.jq-basket-icon').hide();
		$('.jq-basket-progress-icon').show();
		
		// Finds the form the button click is sitting in
		$form = $(this).closest("form").attr("action");
	
		//$button = $(this);
		var sku = $("#product-sku").val();
		var qty = $("#product-quantity").val();
		
		// Disable all the buy buttons
		//$(".buy-button").attr('disabled', 'disabled');
		
		var params = {};
		params[sku] = qty;

		// This posts the value to the form
		$.post($form, params, function(returned_data) {
																 
			// Splits the returned data and populates the correct divs
			var mySplitResult = returned_data.split("[[DIVIDER]]");
			$('#basket-header').html(mySplitResult[0]);
			$('#basket-messages').html(mySplitResult[1]);
			$('#basket-items').html(mySplitResult[2]);
						
			$('#basket-messages').show();
			
			//flashProduct();
			$('#basket-contents').slideDown(1000, flashProduct()).delay(4000).slideUp(1000, function(){ 

					//$(".buy-button").removeAttr('disabled');
					// Hide the spinnder as its no longer loading
					$('.jq-basket-progress-icon').hide();
					$('.jq-basket-icon').show();
				
				});
			
		} , "html");
		
	});
	
	// Do ALL this when a buy button on category page clicked!
	$(".category-buy").click( function(event) {
		
		// Show the spinnder as its loading
		$('.jq-basket-icon').hide();
		$('.jq-basket-progress-icon').show();
		
		// Finds the form the button click is sitting in
		$form = $(this).closest("form").attr("action");
		$sku = $(this).attr("key");
		
		// Only ever add one!
		var qty = 1;
		
		var params = {};
		params[$sku] = qty;

		// This posts the value to the form
		$.post($form, params, function(returned_data) {
																 
			// Splits the returned data and populates the correct divs
			var mySplitResult = returned_data.split("[[DIVIDER]]");
			$('#basket-header').html(mySplitResult[0]);
			$('#basket-messages').html(mySplitResult[1]);
			$('#basket-items').html(mySplitResult[2]);
						
			$('#basket-messages').show();
			
			//flashProduct();
			$('#basket-contents').slideDown(1000, flashProduct()).delay(4000).slideUp(1000, function(){ 

					//$(".buy-button").removeAttr('disabled');
					// Hide the spinnder as its no longer loading
					$('.jq-basket-progress-icon').hide();
					$('.jq-basket-icon').show();
				
				});
			
		} , "html");
		
	});

	// Make sure everything is hidden when we start
	$('#basket-contents').hide().animate({opacity: 0.95}, 1);
	$('#slideleft').hide();
	// Make sure the button is enabled
	$('#buybutton')
	.attr({ 
		disabled: ""
	});
	
	// On hover basket start...
	$("#basket").hover(
		function () {
			$('#basket-messages').hide();
			$('#basket-contents')
			.stop(true, true)
			.slideDown('fast');
		},
		function () {
			$('#basket-contents')
			.stop(true, true)
			.delay(500)
			.slideUp('fast');
			$('.jq-basket-progress-icon').hide();
			$('.jq-basket-icon').show();
		}
	);
	// On hover basket end.
	
	// These handle the buttons
	$("input[value='delete']").live('click', function() {
		
		var confirmDelete=confirm("Are you sure you wish to remove this product from your basket?");
		if (confirmDelete==true) {
		
			// Find the closest form action
			$form = $(this).closest("form").attr("action");
			// Find the closest key which is actually the sku
			sku = $(this).closest("form").attr("key");
			
			var params = {};
			// Create some parameters to pass to the form
			params[sku] = 0;
			
			// Post the information
			$.post($form + "removefrombasket.inc.php", params, function(returned_data) {
			
				// Return the data
				updateBasket();
			
			});
			
		}
		
	});
	
	$("input[value='update']").live('click', function() {
		
			// Find the closest form action
			$form = $(this).closest("form").attr("action");
			// Find the closest key which is actually the sku
			sku = $(this).closest("form").attr("key");
			
			qty = $("#qty-" + sku).val();
			
			var params = {};
			// Create some parameters to pass to the form
			params['theSku'] = sku;
			params['theQty'] = qty;
			params['theAction'] = "update";
			
			// Post the information
			$.post($form + "addtobasket.inc.php", params, function(returned_data) {
			
				// Return the data
				updateBasket();
			
			});
		
	});
	
});

