$(document).ready(function(){
	$("input#same").click(function(){
		if ($("input#same").is(':checked'))
		{
			// Checked, copy values
			$("input#bill_address").val($("input#ship_address").val());
			$("input#bill_city").val($("input#ship_city").val());
			$("input#bill_state").val($("input#ship_state").val());
			$("input#bill_zip").val($("input#ship_zip").val());
			$("input#bill_country").val($("input#ship_country").val());
		}
		else
		{
			// Clear on uncheck
			// Checked, copy values
			$("input#bill_address").val("");
			$("input#bill_city").val("");
			$("input#bill_state").val("");
			$("input#bill_zip").val("");
			$("input#bill_country").val("");
		}
	});

	// bind the recalc function to the quantity fields
	$("input[name^=qty]").bind("keyup", recalc);
	// run the calculation function now
	recalc();

	function recalc(){
		$("[id^=total_item]").calc(
			// the equation to use for the calculation
			"qty * price",
			// define the variables used in the equation, these can be a jQuery object
			{
				qty: $("input[name^=qty]"),
				price: $("[id^=price_item_]")
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a dollar grandTotal
				return "$" + s.toFixed(2);
			},
			// define the finish callback, this runs after the calculation has been complete
			function ($this){
				// sum the total of the $("[id^=total_item]") selector
				var sum = $this.sum();
				var shipping = $("#shippingTotal").sum();

				// add shipping
				sum = sum + shipping;

				$("#grandTotal").text(
					// round the results to 2 digits
					"$" + sum.toFixed(2)
				);
			});
		}

	$("a.addToCart").click(function() {
		$("#update-form").append('<div><input type="hidden" name="qty[' + $(this).attr("product_id") + ']" value="1" /></div>');
		$("#update-form").submit();
		return false;
	});
});

