/**
 * Workstation Shopping Basket
 * 
 * @author paul lashbrook
 * @copyright Original Webware Ltd
 */

function update_dom(json) {
	$.each(json.items, function( key,value) {
			$("span#" + key ).html(
					parseFloat(value).toFixed(2));
		
	});

	var count = 0;
	$('#icon[rev = deleteOrderItem]').each(function() {
		count++
	});
	if (count == 0) {
		$("#checkout-message").slideUp();
		$(".checkout_div")
				.html(
						'<div style="padding-left: 40px;"><h1 style="font-size: 16px;">System Message</h1><p style="font-size: 16px;">Your shopping cart is empty.<br /></p></div>');
	}
}


function deleteBasketItem(){
	// $('#'+$('#shipping_toggle').attr('rev')).hide();
	var site_url = $('div#site_url').html();


	// Delete Order Item
	$('#icon[rev = deleteOrderItem]')
			.live('click', function() {
				var $id = $(this).attr('rel');
				// find grandparents row
					var row = $(this).parent().parent();
					// change icon to the loading image
					$(this).removeClass().addClass('loading');
					// send request
					var $site_url = $('div#full_site_url').html();
					$.getJSON($site_url,
									{
										'action' : 'EcommerceDeleteItem',
										'id' : $id,
										'json' : 1
										

									},
									function(json) {
										// if error do somthing
									if (json.error == 1) {
										alert('We Could Not delete the item, please try again');
									} else {
										update_dom(json);
										row.fadeOut('slow').remove();
										
									}
								});
					return false;
				});
}
	
function basket_edit_ammounts(){
	
	$('a.minus').show();
	$('a.plus').show();
	$('input.amount_submit').hide();
	$('input.ammount_input').css( {
		'width' : '20%'
	});
	
	$('input.ammount_input').blur(function(){
		
		var $element = $(this);
		var $site_url = $('div#full_site_url').html();
		var ammount = $element.attr('value');
		var $item_id = $element.parent().children('input.ammount_id').attr('value');
		
		$.post($site_url,
						{
							'action' : 'EcommerceEditItemAmount',
							'new_value' : ammount,
							'id' : $item_id,
							'json' : 1
							
						},
						function(json) {
							// if error do somthing
						if (json.error == 1) {
							alert('We Could Not delete the item, please try again');
							
						} else {
							update_dom(json);
							if(ammount < 1){
								 $element.parent().parent().parent().fadeOut('slow').remove();
							}
						}
					}, 'json');
	});
	
	$('form.edit_amount a')
	.click(function() { 
		var $element = $(this);
		// holds the icon class, which is also the click type
			var $click_type = $element.attr('rel');
			// get input value
			var $ammount = $element.parent()
					.find('input.ammount_input').val();
			var $item_id = $element.parent().find('input.ammount_id')
					.val();
			// change icon to the loading image
			$element.removeClass().addClass('icon').addClass('loading');
			// start a new ammount to check the value changed correctly
			var $new_ammount = parseInt($ammount);

			if ($click_type == 'minus') {
				$new_ammount--;
			}

			if ($click_type == 'plus') {
				$new_ammount++;
			}

			if ($new_ammount == 0) {
				$element.removeClass().addClass('icon').addClass(
						$click_type);
				return false;
			}
			/**
			 * set store url
			 */
			var $site_url = $('div#full_site_url').html();
			$.post($site_url,
							{
								'action' : 'EcommerceEditItemAmount',
								'new_value' : $new_ammount,
								'id' : $item_id,
								'json' : 1
								
							},
							function(json) {
								// if error do somthing
							if (json.error == 1) {
								alert('We Could Not delete the item, please try again');
								$element.removeClass().addClass('icon')
										.addClass('error');
							} else {
								update_dom(json);
								$element.parent().find(
										'input.ammount_input').val(
										$new_ammount);
								$("#checkout-message:visable").remove();
								$element.removeClass().addClass('icon')
										.addClass($click_type);
								if(ammount < 1){
									 $element.parent().parent().parent().fadeOut('slow').remove();
								}

							}
						}, 'json');
			
			return false;

		});

	
}


$(function() {

	/**
	 * adds the edit amount functionality on shopping basket
	 */
	basket_edit_ammounts();
	/**
	 * adds the abilty to remove items from your basket using ajax requests
	 */
	deleteBasketItem();
	
});
