$(function() {
	$(".boxsepet .sepet-fiyat-sec input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "ax/cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$(".boxsepet .sepet-fiyat-sec input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice();
				});
			},
			error: function() {
				window.location("ax/cart_action.php?remove[]="+orderCode);
			}
		});
	});

	$("#sepet .quantity input").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		$.ajax({
			type: "GET",
			url: "ax/cart_action.php",
			data: "quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#sepet .quantity  input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				//$("#sepet .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location("ax/cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
});

function calcPrice() {
	var totalPrice = new Number(0);
	$("#sepet .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text();
		var extendedPrice = new Number(quantity*unitPrice);
                totalPrice += extendedPrice;

                extendedPrice = extendedPrice.toFixed(2);
		//totalPrice = totalPrice.toFixed(2);

                //alert("deneme: " + extendedPrice);

                $(".extended_price", this).html(extendedPrice);
		$("#toplamfiyat").html("" + totalPrice + " ");
	});
	if ( totalPrice == 0 ) {
		$("#sepet").parent().replaceWith("<p class='kitap_yok'>Sepetinizde hiç kitap bulunmuyor.</p>");
	}
}
