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() {

	$('.currency-changer').bind('change', function(){
		href = document.location.href;
		document.location.href = '/?currency=' + $(this).attr('value') + '&redirect=' + href;
	});

	$(".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;});

	$(".add-to-cart").click(function() {

		el = $(this).parent().parent().find(".product-price");

		var curPrice = el.attr('pprice') * 1;
		var curCurr = el.attr('pcurrency');
		var pId = $(this).attr('pid');

		
		//alert(pId + ' ' + curPrice + ' ' + curCurr);
		//return false;

		if (!$(this).hasClass("added")) {
			if ($(".cb-empty").is(":visible")) {
				$(".cb-empty").hide();
				$(".cart-block:not('.cb-empty')").show();
				$(".cart-products-count").text('1 товар').attr('ccount', 1);
				$(".cart-products-price").html('<strong>' + curPrice + '</strong>&nbsp;' + curCurr).attr('cprice', curPrice);
			}
			else {
				var iCol = $(".cart-products-count").attr('ccount') * 1 + 1;
				$(".cart-products-count").attr('ccount', iCol).text(iCol + ' ' + ruscomp(iCol, 'товаров,товар,товара'));

				var cartPrice = $(".cart-products-price").attr('cprice') * 1;
				var totalPrice = cartPrice + curPrice;

				a = cartPrice.toString();
				b = curPrice.toString();
				if (a.indexOf('.') > 0 || b.indexOf('.') > 0)
				{
					totalPrice = totalPrice.toFixed(2);
				}
				else
				{
					totalPrice = totalPrice.toFixed(0);
				}

				$(".cart-products-price").html('<strong>' + setBackspaces(totalPrice.toString()) + '</strong>&nbsp;' + curCurr).attr('cprice', totalPrice);
			}

			var items = "";
			if ($("#cart-items").attr("value") == "") {
				items = pId;
			}
			else {
				items = $("#cart-items").attr("value") + "," + pId;
			}
			$("#cart-items").attr("value", items);
			
			$.cookie('cart', items, { expires: 7, path: '/'});

			$(this).hide();
			$(this).parent().find('.added-label').show();

            //$.openDOMWindow({loader:0, height: 140, width: 350, windowSourceID:'#cartMessage' });
			
			getPopup("cartMessage");
			
	    	return false;
		}
	});



	//$.cookie('cart', null, {path: '/'});

	$("#cart-items").attr("value", $.cookie('cart'));

	$('a.cart-del').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;
	});
    $('.top_menu li a').bind('mouseover',function(){
                $(this).children('.img_of').hide();
                $(this).children('.img_on').show();
               
            });
    $('.top_menu li a').bind('mouseout',function(){
                $(this).children('.img_on').hide();
                $(this).children('.img_of').show();
               
    });
                








});



function ruscomp($number, $compl)
{

	$comp = $compl.split(',');
	//$comp[0]="минут"; # 0, 5
	//$comp[1]="минута"; # 1
	//$comp[2]="минуты"; # 3

    if ($number==0 || ($number%10)==0) {return $comp[0];}
    if ($number>=5 && $number<=20) {return $comp[0];}
    if ($number%10>=5 && $number%10<=9) {return $comp[0];}
    if (($number%10)==1) {return $comp[1];}
    if (($number%10)>=2 && ($number%10)<=4) {return $comp[2];}
}


function setBackspaces(p) {

	p = p.toString();

	m = '';
	a = p.split('.');
	if (a.length > 1)
	{
		p = a[0];
		m = '.' + a[1];
	}

	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 + m;
}
