jQuery(document).ready(function(){
	jQuery("#up").click(function(event){
		var value_s = jQuery("#qty").attr('value');
		var items = parseInt(value_s);
		var total_s = jQuery("#total").text();
		var total = parseFloat(total_s);
		//Change the Quantity
		items = items + 1;
		jQuery("#qty").attr('value', items);
		
		// Calculate the total
		total = getTotal(items);
		
		if(items < 3){
			jQuery("#shipping_price").text('Shipping : $ 1.95');
		}
		else{
			jQuery("#shipping_price").text('Free Shipping');
		}
		
		jQuery("#total").text(total);
		jQuery(".update_cart").trigger('click');
	  });
	jQuery("#down").click(function(event){
		var value_s = jQuery("#qty").attr('value').replace(/[^0-9]/g, '');
		var items = parseInt(value_s); 
		var total_s = jQuery("#total").text();
		var total = parseFloat(total_s);
		
		items = items - 1;
		if(items < 1){
			jQuery("#qty").attr('value', '1');
			// Calculate the total
			total = getTotal(1);			
			jQuery("#total").text(total);
			jQuery("#shipping_price").text('Shipping : $ 1.95');
		}
		else{
			jQuery("#qty").attr('value', items);
			// Calculate the total
			total = getTotal(items);			
			jQuery("#total").text(total);
			if(parseInt(items)<3){
				jQuery("#shipping_price").text('Shipping : $ 1.95');
			}
		}
		jQuery(".update_cart").trigger('click');
	});
	// Calculate the total
	function getTotal(items){
		var qty = parseInt(items);
		var total = 0;
		if(qty < 3){
			total = 11.95 * qty;
		}
		else if(qty == 3){
			total = 29.95;
		}
		else{
			qty = qty - 3;
			total =  29.95 + (qty * 9.95);
		}
		//total =  (parseInt(qty/3)* parseFloat(29.95)) + (parseInt(qty % 3)* parseFloat(11.95));
		return total.toFixed(2);
	}
	
	// Billing Toggle
	jQuery(".add_to_cart").click(function(event){
		//jQuery(".add_to_cart").attr('disabled',true);
		jQuery(".add_to_cart").css("display", "none");
		jQuery(".update_cart").css("display", "block");
		$.ajax({
			type: 'POST',
			url: "catalog_cart.php", 
			data: $("#productForm").serialize(), 
			success: function(data){ 
			   jQuery("#billing").css("display", "block");
			   jQuery("#spacer").css("display", "none");
		   	},
		   	error: function(request, status, error) {
		   		jQuery(".add_to_cart").attr('disabled',false);
		   		alert(status + "\n" + error);
		   	}
		});
	});
	
	// Billing Toggle
	jQuery(".update_cart").click(function(event){
		$.ajax({
			type: 'POST',
			url: "catalog_cart.php", 
			data: $("#productForm").serialize(), 
			success: function(data){ 
			   //jQuery("#billing").css("display", "block");
		   	},
		   	error: function(request, status, error) {
		   		alert(status + "\n" + error);
		   	}
		});
	});
	//Trigger on Continue
	/*jQuery(".continue").click(function(event){
		jQuery(".update_cart").trigger('click');
	});*/
	
	// Default Values
	jQuery("#total").text('11.95');
	jQuery("#qty").attr('value', '1');	
});
