/*

   Name  : Sepet Js
   Coder : KoD8
   Url   : http://www.kod8.com

*/

$(function() {
	$(".remove_data input").click(function() {
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "do=delete&" + $('#get_cart').serialize(),
			success: function(sonuc) {
			for(i=0;i<document.getElementsByName('remove[]').length;i++) {
			if(document.getElementsByName('remove[]')[i].checked == true){
			var valuess = document.getElementsByName('remove[]')[i].value;
			
				$("#cart tr .remove input[value=" + valuess + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					calcPrice('asd','1');
				});
			}
			}

			},
			error: function() {
				window.location("cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	$(".siparis_").click(function() {
		$("#sepet_s1").fadeOut("slow");
		$("#uye_bilgi").fadeIn("slow");
	});
	
	$("input[name=sepet_don]").click(function() {
		$("#uye_bilgi").fadeOut("slow");
		$("#sepet_s1").fadeIn("slow");
	});
	
	$("input[name=siparis_tamam]").click(function() {
		$.ajax({
			type: "GET",
			url: "cart_action.php",
			data: "do=siparis&" + $('#get_cart').serialize(),
			success: function(sonuc) {
			$("#siparis_sonuc").html(sonuc);

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

function calcPrice(orderCode,quanty) {
	var unprice = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find(".unit_price").text();
	var extend = quanty*unprice;
	var totalPrice = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price input", this).val();
		var extendedPrice = quantity*unitPrice;
		totalPrice += extendedPrice;
		$.get("cart_action.php?do=totalPrice",function(Sonuc){
		
		$.each(Sonuc.kod8_sepet, function(i, kod8_al){
		$("#total_price").html(kod8_al.toplam);
		$("#genel_toplam").html(kod8_al.kdvli);		
		$("#kdv_t").html(kod8_al.kdv_tutar);
		});
		},"json");
		$.ajax({
		 type: "GET",
		 url: "cart_action.php",
		 data: "do=ajax_update&trans_price="+extend,
		 success: function(data){
		 $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find(".fiyati").html(data);
		 $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find(".fiyati_s input").attr("value",data);
		},
		error: function() {
				alert('HATA');
		}
		});
	});
	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith('<div class="center"><img src="images/uyari.png" alt="Uyarı ! Sepetiniz Boş"/><br /><br />Sepetinizde Ürün Bulunmuyor..</div>');
	}

}
