$(function() {
	$("#cart tr .remove input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice();
				});
			},
			error: function() {
				window.location("cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	//$("#cart tr .quantity select").change(function() {
		//var orderCode = $(this).attr("name").slice(9, -1);
		//var quantity = $(this).val();
		//$.ajax({
			//type: "GET",
			//url: "cart_action.php",

			//success: function() {
				//var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				//$("#cart tr .quantity select[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				//calcPrice();
			//},
			//error: function() {
				//window.location("cart_action.php?quantity[" + orderCode + "]=" + quantity);
			//}
		//});
	//});
});

function calcPrice() {
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity select", this).val();
		var unitPrice = $(".unit_price", this).text().slice(0, -1);
                var carriagePrice = $("#carriage").text().slice(0, -1);
		var extendedPrice = quantity*unitPrice;
                
                extendedPrice -= 0;
                carriagePrice -= 0;
                if (extendedPrice > 30000) { carriagePrice = 0 } else { carriagePrice = 630 };
		totalPrice = extendedPrice + carriagePrice;
		
		$(".extended_price", this).html(extendedPrice + "円");
                $("#carriage").html(carriagePrice + "円");
		$("#total_price").html(totalPrice + "円");
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith("<p class='center'>カートの中にはなにも入っていません</p>");
	}
}