//
// Class: 
// јвтор: јндрей SaZ ¬ареник (окт 2008), (с) и вс€ хуйн€
// ”слови€ использовани€: свободное копирование/изменение, если своих мозгов не хватает написать аналогичное.
// ƒух браузера, мой бубен сильнее твоей тупости!
//


jQuery('document').ready(InitiateCartCounter);

var aProcessedTags = [];

function InitiateCartCounter()
{
	jQuery('#add-to-cart-button-bt').click( PutToCart );
	jQuery('#lay-out-of-cart').click( LayOut );

	// hidebox navigation
	jQuery('span.hidebox-swapdiv').click( SwapHidebox );
	jQuery('input[@name="addprop_size"]').click( SelectAddProp );
		
	GetCartMoneySumm ();
}

function SwapHidebox()
{
	var rBrother = $(this.parentNode.previousSibling);
	if (rBrother.css('display') == 'none')
	{
		//rBrother.css('display', 'block');
		rBrother.fadeIn(100);
		$(this).text("уберите это!");
	}
	else
	{
		//rBrother.css('display', 'none');
		rBrother.fadeOut(100);
		$(this).text("еще...");
	}
}

function SelectAddProp()
{
	jQuery('#add-to-cart-button-bt').attr('issizeselected', '');
	jQuery('#add-to-cart-button-bt').text('Положить в мешок');
	GetCartMoneySumm ();
}

function PutToCart ()
{
	var nSize = "";
	
	if (jQuery('#is_addprop_available').val() == 'true')
	{
		jQuery('input[@name="addprop_size"]').each
		(
			function () 
			{
				if(jQuery(this).attr('checked'))
				{
					nSize = jQuery(this).val();
					return;
				}
			}
		);
	}
	var nGoodID = jQuery(this).attr('goodid');
	jQuery('#add-to-cart-button-bt').attr('disabled', 'true');
	// save in cookies
	var sPrevGoods = getCookie('goodslist');
	if (sPrevGoods == null || sPrevGoods == "undefined")
	{
		sPrevGoods = "";
	}
	
	if (nSize != '')
	{
		nGoodID += "." + nSize;
	}
	
	sPrevGoods += ":" + nGoodID;
	setCookie ('goodslist', sPrevGoods, "Tue, 01-Jan-2019 00:00:00 GMT", "/");
	
	jQuery('div.cart-on-page').hide();
	
	// calculate it
	jQuery.getJSON(
	  '/cgi-bin/calc_cart.pl',
	  {
	  	GoodsList: sPrevGoods
	  },
	  onCartCalc
	);
}

function onCartCalc(data)
{
	// store data in cookie
	setCookie ('summ', data.nTotalCost, "Tue, 01-Jan-2019 00:00:00 GMT", "/");
	
	// show summ
	GetCartMoneySumm(data.nTotalCost);
}

function LayOut ()
{
	var nGoodID = jQuery('#add-to-cart-button-bt').attr('goodid');
	jQuery('div.cart-on-page').fadeOut(100);
	
	if (nGoodID > 0)
	{
		var sPrevGoods = getCookie('goodslist');
		var aNewGoods = [];
		var aGoods = sPrevGoods.split(":");
		var bIsDeleted = false;
		for (var i = 0; i < aGoods.length; i++)
		{
			var aGoodsData = aGoods[i].split(".");
			
			if (aGoodsData[0] != nGoodID || bIsDeleted)
			{
				var sGoodsKey = aGoodsData[0];
				if (aGoodsData[1] != '')
					sGoodsKey += "." + aGoodsData[1];
				aNewGoods.push(sGoodsKey);
			}
			else
			{
				bIsDeleted = true;
			}
		}
		
		setCookie ('goodslist', aNewGoods.join(":"), "Tue, 01-Jan-2019 00:00:00 GMT", "/");
	
		// calculate it
		jQuery.getJSON(
		  '/cgi-bin/calc_cart.pl',
		  {
			GoodsList: aNewGoods.join(":")
		  },
		  onCartCalc
		);
	}

}

function GetCartMoneySumm (nSumm)
{
	// this stuff is only for summ display. if u're a |-|a}{oR - change it, but remember - 
	// data is stored on the server :)
	var nMoneySumm = getCookie("summ");
	//alert(nMoneySumm);
	if (nMoneySumm == "" && nSumm > 0)
	{
		nMoneySumm = nSumm;
	}
	
	// show how much of current goods is in cart
	var nGoodID = jQuery('#add-to-cart-button-bt').attr('goodid');
	jQuery('div.info-in-cart-quantity').hide();
	if (nGoodID > 0)
	{
		var sPrevGoods = getCookie('goodslist');
		
		if (sPrevGoods == null || sPrevGoods == "undefined")
		{
			sPrevGoods = "";
		}
		
		var nInCart = 0;
		
		var aGoods = sPrevGoods.split(":");
		for (var i = 0; i < aGoods.length; i++)
		{
			var aGoodsData = aGoods[i].split(".");
			if (aGoodsData[0] == nGoodID)
			{
				nInCart++;
			}
		}
		
		if (nInCart > 0)
		{
			jQuery('div.info-in-cart-quantity span.number').text(nInCart);
			jQuery('div.info-in-cart-quantity').fadeIn(1);
		}
	}
	
	jQuery('#cart-summ').text(nMoneySumm);
	
	
	// Opera 9.6 on Mac fails on redraw inline element text
	// so, forcely redraw it with effects :)
	// IE6 on PC fucks up all content in div. Think on it!
	
	if (nMoneySumm > 0)
	{
		jQuery('div.cart-on-page').show(); //fadeIn(200);
	}
	
	if (jQuery('#add-to-cart-button-bt').attr('issizeselected') != 'false')
		jQuery('#add-to-cart-button-bt').attr('disabled', '');
}

// move it to common lib
function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}