String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getViewportHeight() {
 return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

function getDocumentHeight() {
 return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

$(document).ready(function() {
	$(".items-list .item").hover(
		function() {
			$(this).find(".orange-lbl").css("display", "inline-block");
		},
		function() {
			$(this).find(".orange-lbl:not('.added')").css("display", "none");
		}
	);

    $('.cartMessageClose').click(function(){$.closeDOMWindow(); return false;});

	$(".items-list .item .orange-lbl:not('.disabled'), .images-block .orange-lbl:not('.disabled')").click(function() {
		var curPrice = $(this).parent().find(".price:not('.orange-lbl') strong").text();
		var i = curPrice.indexOf("у");

		if (!$(this).hasClass("added")) {
			if ($(".cb-empty").is(":visible")) {
				$(".cb-empty").hide();
				$(".cart-block:not('.cb-empty')").show();
				$("#prod").text("1");
				curPrice = curPrice.substr(0, i);
				$("#prod-price").text(curPrice);
			}
			else {
				var iCol = $("#prod").text() * 1;
				$("#prod").text(iCol + 1);
				var cartPrice = $("#prod-price").text().replaceAll(' ', '') * 1;
				curPrice = curPrice.substr(0, i).replaceAll(" ", "") * 1;
				var totalPrice = (cartPrice + curPrice).toString();

				totalPrice = setBackspaces(totalPrice);

				$("#prod-price").text(totalPrice);

			}
			$(this).addClass("added").find("strong").text("В корзине");
			var items = "";
			if ($("#cart-items").attr("value") == "") {
				items = $(this).parent().parent().attr("rel");
			}
			else {
				items = $("#cart-items").attr("value") + "," + $(this).parent().parent().attr("rel");
			}
			$("#cart-items").attr("value", items);
			$.cookie('cart', items, { expires: 7, path: '/'});


            $.openDOMWindow({
		        loader:0,
		        height: 200,
		        width: 350,
		        windowSourceID:'#cartMessage'
	    	});


		}
	});



	//$.cookie('cart', null, {path: '/'});

	$("#cart-items").attr("value", $.cookie('cart'));

	$('.td-button a').click(function(){		items	 = $("#cart-items").attr("value").split(',');
		tmp = new Array();
		n=0;
		id = $(this).attr("rel");
		for (i = 0; i < items.length; i++)
		{			if (items[i] != id)
			{				tmp[n] = items[i];				n++;			}		}
		$.cookie('cart', tmp.join(','), { expires: 7, path: '/'});
		document.location.href = document.location.href;
		return false;	});

	$(".images-block .thumbs a").click(function() {
	  var mainImg = $(".images-block .main-image a img:not('.lupa')");
	  mainImg.attr("src", $(this).attr("rel"));
	  $(".images-block .main-image a").attr("href", $(this).attr("ref"))
	  $(".images-block .thumbs a").removeClass("act");
	  $(this).addClass("act");
	 });

	$(".main-image a").hover(
		function() {
			$(".lupa", this).show();
		},
		function() {
			$(".lupa", this).hide();
		}
	);




	$(".cart-table input[type=text]").keyup(function(e) {
	  var val = parseInt(this.value, 10);
	  if (val != 0)
	  {
	   var price = $(this).parent().prev().find("strong").text();
	   var i = price.indexOf("у");
	   var res = $(this).parent().next().find("strong");
	   price = price.substr(0, i).replaceAll(" ", "");
	   price = price * val;

	   price = setBackspaces(price.toString());

	   res.text(price);
	   setTotalPrice();
	  }

	  this.value = val;

	  function setTotalPrice() {
	   var prices = $(".cart-table td.td-total strong");
	   var totalPrice = $("#total-price");
	   var tp = 0;

	   for (var i = 0; i < prices.length; i++) {
	    var p = prices.eq(i).text().replaceAll(" ", "")*1;
	    tp += p;
	   }
	   totalPrice.text(setBackspaces(tp+""));
	  }
	 });


      $(".main-image a.lightbox img").click(function() {
	   setTimeout('$("#jquery-overlay").height(getDocumentHeight())', 100);
	  });


});




function setBackspaces(p) {
	var n = (p.length - (p.length % 3)) / 3;
	if (p.length % 3 == 0)
		n--;

	var k = 1;
	for (var x = n; x > 0; x--) {
		var j = p.length - k - 3 * k;
		var s1 = p.substr(0, j + 1);
		var s2 = p.substr(j + 1, p.length - 1);
		p = s1 + " " + s2;
		k++;
	}
	return p;
}
