      $(function() {
      	$("img.add_cart").click(function(e) {
//							alert ($(this).attr("id"));
        		$.ajax({
        			type: "GET",
        			url: "http://www.okdecor.ru/cart_action.php",
        			data: "order_code=" + $(this).attr("id") + "&quantity=1",
        			success: function() {
//							alert ('добавил');
								$("#basket").load("http://www.okdecor.ru/cart_action.php?baskettop=1");
								MessageShow ("Товар добавлен в корзину", 3000, e.pageX, e.pageY);
        			},
        			error: function() {
//							alert ('ошибка');
        			}
        		});
      });
     });


$(function() {
  $("img.delete_cart").click(function() {
		var orderCode = $(this).attr("id");
		var orderCodeId = orderCode.split("|").join("-");
		var orderCodeId = orderCodeId.split(",").join("-");
		$.ajax({
			type: "GET",
			url: "http://www.okdecor.ru/cart_action.php",
//			url: "cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
//				$("#cart img.delete_cart input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
//					$(this).remove();
//					calcPrice();
//				});
//				alert ("Удаляемс");
				$("#tr-" + orderCodeId).fadeOut(500, function() {
					$(this).remove();
					calcPrice();
				});
				$("#tr-i-" + orderCodeId).fadeOut(500, function() {
					$(this).remove();
				});
				$("#basket").load("http://www.okdecor.ru/cart_action.php?baskettop=1");
			},
			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 = parseInt($(this).val());
		if (isNaN(quantity) == true) quantity = 0; 

		$(this).val(quantity);
		$.ajax({
			type: "GET",
			url: "http://www.okdecor.ru/cart_action.php",
//			url: "cart_action.php",
			data: "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);
				$("#tr"+orderCode).animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				calcPrice();
			},
			error: function() {
				window.location("cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
});

function calcPrice() {
	var totalPrice = 0;
	var totalQuantity = 0;
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text();//.slice(1);
		var extendedPrice = Math.round(quantity*unitPrice*100)/100;
		totalPrice += extendedPrice;
		totalQuantity += Number(quantity);

//		$(".extended_price", this).html("$" + extendedPrice);
//		$("#total_price").html("$"+totalPrice);
		$(".extended_price", this).html("<b>"+extendedPrice+"</b>");
	});

	  totalPrice = Math.round(totalPrice*100)/100;
	
		$("#total_price").html("<b>"+totalPrice+"</b>");
		$("#basket_top_quantity").html("<b>"+totalQuantity+"</b>");
		$("#basket_top_price").html("<b>"+totalPrice+"</b>");

	if ( totalPrice == 0 ) {
		$("#cart").parent().replaceWith('<table width="100%" cellpadding="0" cellspacing="1"><tr><th bgcolor="cccccc" height="1" colspan="7"></th></tr><tr bgcolor="e7e7e7" align="center"><th class="basket_title" width="6%">№</th><th class="basket_title" width="18%">Изображение</th><th class="basket_title" width="25%">Наименование</th><th class="basket_title" width="14%">Цена,<br>руб.</th><th class="basket_title" width="13%">Количество</th><th class="basket_title" width="14%">Стоимость,<br>руб.</th><th class="basket_title" width="10%">Удалить</th></tr><tr><td class="basket_item" colspan="7" align="center" height="50">У Вас нет товаров в корзине</td></tr></table>');
//		$("#cart").parent().replaceWith('TEST');
	}

	var num = 1;
	$("#cart tr .num").parent().each(function() {
//		$(".extended_price", this).html("$" + extendedPrice);
//		$("#total_price").html("$"+totalPrice);
		$(".num", this).html(num);
		num++;
	});
}

