$(document).ready(function(){
	if ($.cookie("cartEmpty") == null) {
		$.cookie("cartEmpty", "1", {path: "/", domain: "klenmarket.ru", expires: 365});
	};
	if ($.cookie("cartOpened") == null) {
		$.cookie("cartOpened", "1", {path: "/", domain: "klenmarket.ru", expires: 365});
	};
	if ($.cookie("cartOpened") == "0" && $.cookie("cartEmpty") == "1") {
		$.cookie("cartOpened", "1", {path: "/", domain: "klenmarket.ru", expires: 365});
	};


	if ($.cookie("cartEmpty") == "1") {
		$("div.korz_tbl").hide();
	};
	if ($.cookie("cartEmpty") == "0") {
		$("div.korz_tbl").show();
		if ($.cookie("cartOpened") == "1") {
			$("div#little_cart").hide();
			$(".korz_tbl").animate({width: '+=115px'}, {complete: function(){$("a.cart-arrow").next().fadeIn(20);}});
			
		} else {
			$("div#little_cart").hide();
			$("a.cart-arrow > img").attr("src", "/images/arrow-left.png");
			$("a.cart-arrow").css("background", "transparent url(/images/cart-picture.png) top center no-repeat");
		};
	};


	$("a.cart-arrow").click(function(){
		if ($.cookie("cartOpened") == "0") {
			$.cookie("cartOpened", "1", {path: "/", domain: "klenmarket.ru", expires: 365});
			$(this).next().attr("class", "activeCart");
			$("div#little_cart").hide();
			$(".korz_tbl").animate({width: '+=115px'}, {complete: function(){$("a.cart-arrow").next().fadeIn(20);}});
		} else {
			$.cookie("cartOpened", "0", {path: "/", domain: "klenmarket.ru", expires: 365});
			$(this).next().attr("class", "not-activeCart");
			$("div#little_cart").hide();
			$(this).next().fadeOut(20, function(){$(".korz_tbl").animate({width: '-=115px'});});
		};


		if ($("> img", this).attr("src") == '/images/arrow-left.png') {
			$("> img", this).attr("src", "/images/arrow-right.png");
			$(this).css("background", "none");
		} else {
			$("> img", this).attr("src", "/images/arrow-left.png");
			$(this).css("background", "transparent url(/images/cart-picture.png) top center no-repeat");
		};
	});


	$("a.buy-button").click(function(){
		if ($.cookie("cartOpened") == "1" && $.cookie("cartEmpty") == "1") {
			$("div.korz_tbl").show();
			$("div#little_cart").hide();
			$(".korz_tbl").animate({width: '+=115px'}, {complete: function(){$("a.cart-arrow").next().fadeIn(20);}});
		}
		if ($.cookie("cartOpened") == "0") {
			$("a.cart-arrow").trigger('click');
		}
		$.cookie("cartEmpty", "0", {path: "/", domain: "klenmarket.ru", expires: 365});
	});
});
