function toggleCatalogState(){
	if (!$("#home-catalog-wrapper:not(:animated)").size()) return false;
	if ($("#open-catalog").hasClass("closed")){
		$("#home-catalog-wrapper").slideDown({duration: 200, easing: "easeInCubic"});
		$("#open-catalog").removeClass("closed");
	} else {
		$("#home-catalog-wrapper").slideUp({duration: 200, easing: "easeOutCubic"});
		$("#open-catalog").addClass("closed");
	};
	return false; //suppress link
};

function toggleInnerCatalogState(){
	if (!$("#cnt-catalog-wrapper:not(:animated)").size()) return false;
	if ($("#cnt-catalog-block").hasClass("closed")){
		$("#cnt-catalog-wrapper").slideDown({duration: 200, easing: "easeInCubic"});
		$("#cnt-catalog-block").removeClass("closed");
	} else {
		$("#cnt-catalog-wrapper").slideUp({duration: 200, easing: "easeOutCubic"});
		$("#cnt-catalog-block").addClass("closed");
	};
	return false; //suppress link
};


$(document).ready(function(){
	var els = $(".js-prompt");
	els.focus(function(){
		if (!this.jsChanged){
			this.value = "";
		};
		$(this).addClass("js-changed");
	});
	els.blur(function(){
		if (this.value != ""){
			this.jsChanged = true;
			$(this).addClass("js-changed");
		} else {
			this.jsChanged = false;
			$(this).removeClass("js-changed");
			this.value = this.title;
		};
	})
})

$(document).ready(function(){
	$(".cnt-product-price-additional-1").mouseover(function(){
		$(document.body).addClass("js-balloon-state-1");
		$(document.body).removeClass("js-balloon-state-2")
		$(document.body).removeClass("js-balloon-state-3")
	});
	$(".cnt-product-price-additional-2").mouseover(function(){
		$(document.body).addClass("js-balloon-state-2");
		$(document.body).removeClass("js-balloon-state-1")
		$(document.body).removeClass("js-balloon-state-3")
	});
	$(".cnt-product-price-label").mouseover(function(){
		$(document.body).addClass("js-balloon-state-3");
		$(document.body).removeClass("js-balloon-state-1")
		$(document.body).removeClass("js-balloon-state-2")
	});
});

/* == CART ========================================================== */

var cartAnimation = false;
var cartAmount = 0;

function getAbsoluteOffset(obj){
	var offsetX = 0;
	var offsetY = 0;
	var o = obj;
	while(o != document.body.offsetParent){
		offsetX += o.offsetLeft;
		offsetY += o.offsetTop;
		o = o.offsetParent;
	}
	return [offsetX, offsetY];
};

function cartTween(){
	var image = cartAnimation.image;
	var frameDivider = 6;
	var frameEnd = 10;

	if (cartAnimation.frame >= frameEnd){
		
		addToCart(cartAnimation.productId);
		if (cartAnimation.scrollBack){
			$(document).scrollTo( {top: cartAnimation.originalScrollTop + "px", left: cartAnimation.originalScrollLeft + "px"}, 700);
		};
		cartAnimationClear();
		
		return;
	}


	if (cartAnimation.frame < frameDivider){
		var progress = (cartAnimation.frame + 1) / frameDivider;
		image.style.top = parseInt(cartAnimation.original.y - (cartAnimation.original.y - cartAnimation.transition.y)*progress) + "px";
		image.style.left = parseInt(cartAnimation.original.x - (cartAnimation.original.x - cartAnimation.transition.x)*progress) + "px";
		image.style.width = parseInt(cartAnimation.original.w - (cartAnimation.original.w - cartAnimation.transition.w)*progress) + "px";
		image.style.height = parseInt(cartAnimation.original.h - (cartAnimation.original.h - cartAnimation.transition.h)*progress) + "px";
	} else {
		var progress = (cartAnimation.frame + 1 - frameDivider) / (frameEnd - frameDivider);
		image.style.top = parseInt(cartAnimation.transition.y - (cartAnimation.original.y - cartAnimation.target.y)*progress) + "px";
		image.style.left = parseInt(cartAnimation.transition.x - (cartAnimation.original.x - cartAnimation.target.x)*progress) + "px";
		image.style.width = parseInt(cartAnimation.transition.w - (cartAnimation.original.w - cartAnimation.target.w)*progress) + "px";
		image.style.height = parseInt(cartAnimation.transition.h - (cartAnimation.original.h - cartAnimation.target.h)*progress) + "px";

	}
	cartAnimation.frame++;
	setTimeout(cartTween, 40);
}

function updateCart(){
	var cart = getCartContents();
	var cartAmount = 0;
	for (var i in cart){
		cartAmount++;
		$(".js-cart-triggerbox-" + i).addClass("js-cart-added");
	};
	if (cartAmount){
		var cartStr = cartAmount  + "&nbsp;" + (((cartAmount > 10 && cartAmount < 20) || cartAmount%10 > 4 || cartAmount%10 == 0 )?"�������":(cartAmount%10 == 1)?"�����":"������");
	} else {
		var cartStr = "��� �������";
	}
	$("#tpl-cart-amount").html(cartStr);	
}


function getCartContents(){
	var arr = new Array();
	var cookiePreg = /cart\s*=\s*([^;]*)(;|\s*$)/.exec(document.cookie);
	if (!cookiePreg||!cookiePreg[1]) return arr;
	var chunks = cookiePreg[1].split("|");
	var chunk = new Array();
	for (var i in chunks){
		if (typeof(chunks[i]) == "undefined") continue;
		chunk = chunks[i].split(":");
		if (chunk.length < 1) continue;
		if (chunk.length < 2) {
			chunk[1] = 1;
		}
		if (chunk[1] < 0) continue;
		arr[chunk[0]] = chunk[1];
	};
	return arr;
}

function putCartContents(arr){
	if(typeof(arr) != "object"){
		arr = new Array();
	};
	var chunks = new Array();
	for (var i in arr){
		if (typeof(arr[i]) == "undefined") continue;
		if (!arr[i] || arr[i] < 1) continue;
		chunks.push(i + ":" + arr[i]);
	}
	document.cookie = "cart=" + chunks.join("|") + "; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/";
}

function addToCart(productId){
	var cart = getCartContents();
	if (typeof(cart[productId]) == "undefined" || cart[productId] < 1){
		cart[productId] = 1;
	}
	putCartContents(cart);
	updateCart();
}

//----------------------NEW--------------------------

function number_format( number, decimals, dec_point, thousands_sep ) {    // Format a number with grouped thousands
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
 
    var i, j, kw, kd, km;
 
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
 
 
    return km + kw + kd;
}

function swap(num){

    var obj=document.getElementById(num);
	var id_num=/\d+/.exec(num); //alert(id_num);
	if(!(id_num<11||id_num>22)) for(var i=11;i<=22;i++) if(i!=id_num) {var obj_i=document.getElementById('morem'+i); if(obj_i) {obj_i.style.display='none'; obj_i.style.visibility='hidden';}
	var obj_i=document.getElementById('linkmorem'+i); if(obj_i) {obj_i.style.display=''; obj_i.style.visibility='visible';}}
	if(id_num<11||id_num>22) for(var i=23;i<=220;i++) if(i!=id_num) {var obj_i=document.getElementById('morem'+i); if(obj_i) {obj_i.style.display='none'; obj_i.style.visibility='hidden';}
	var obj_i=document.getElementById('linkmorem'+i); if(obj_i) {obj_i.style.display=''; obj_i.style.visibility='visible';}}	
    
	if(obj.style.visibility=='hidden'){
          obj.style.display='';
          obj.style.visibility='visible';
    }else{
          obj.style.display='none';
          obj.style.visibility='hidden';
    }
  }
  
function swapMore(oid){
	wrapping();
	swap('more'+oid);
	swap('linkmore'+oid);
	wrapping();
	return false;
}

function toggleBottomLine(){
	$("#tpl-counter").height(500);
	if($("#tpl-bottomline-wrapper").is(":visible")){
		$("#tpl-bottomline-wrapper").slideUp(300);
		$("#tpl-bottomline-label").html("����������");
		setTimeout(function(){$("#tpl-bottom-overwrap").removeClass("grow");$("#tpl-map-pixline").css("display", "none");},300);
	} else {
		setTimeout(function(){$("#tpl-bottom-overwrap").addClass("grow");$("#tpl-map-pixline").css("display", "block");},100);
		$("#tpl-bottomline-wrapper").slideDown(300);
		$("#tpl-bottomline-label").html("��������");
	};
	return false;
}


$(document).ready(function(){
	$("#tpl-carcass-top").find("div").bind('click', function(e){
	if($("#tpl-bottom-overwrap").hasClass("grow")) toggleBottomLine();});
});

function wrapping() {
	var win=$(window).height()-40;
	var div=$("#tpl-counter").height();
	if($("#tpl-counter").height()>$(window).height()) {$("#tpl-counter").height(win);
	var flag=1}
	else $("#tpl-counter").height("100%")
}

 
function DelItem(productId) 
{
	if($("#cnt-cart-chbox-"+productId).attr("checked")) 
	{
		$("#cnt-cart-chbox-"+productId).attr("checked","");
		$("#cnt-cart-chbox-"+productId).attr("lastvalue",$("#lotof-"+productId).val());
		$("#lotof-"+productId).val("0");
		$("#cnt-cart-chboximg-"+productId).removeClass("cnt-cart-chboximg-y"); 
  		$("#cnt-cart-chboximg-"+productId).addClass("cnt-cart-chboximg-n");
		CalcPrice();
		return;
	}
	if(!$("#cnt-cart-chbox-"+productId).attr("checked")) 
	{
		$("#cnt-cart-chbox-"+productId).attr("checked","1");
		$("#lotof-"+productId).val($("#cnt-cart-chbox-"+productId).attr("lastvalue"));
		$("#cnt-cart-chboximg-"+productId).addClass("cnt-cart-chboximg-y"); 
  		$("#cnt-cart-chboximg-"+productId).removeClass("cnt-cart-chboximg-n");
		CalcPrice();
		return;
	}
	
}

function plusToCart() 
{
	var cart = getCartContents();
	arr = new Array();
	var lotof;
	for (var i in cart)
	{
		if($("#cnt-cart-chbox-"+i).attr("checked"))
		{
			lotof=parseInt($("#lotof-"+i).val());
			if(!isNaN(lotof)) arr[i] = lotof;
			else arr[i]=cart[i];
		}
	}
 	putCartContents(arr);
	updateCart();
}

function CalcPrice()
{
	var cart = getCartContents();
	var all_price=0;
	var price; var dprice; var oprice;
	var lotof;

	for (var i in cart)
	{
		lotof=parseInt(("0" + $("#lotof-"+i).val()).replace(/\s+/, "").replace(/\D.*/,"").replace(/0+([^$])/, "$1"));
		$("#lotof-"+i).val(lotof);
		if(!isNaN(lotof)&&isFinite(lotof))
		{
			if(lotof!=0) $("#cnt-cart-chbox-"+i).attr("checked","1");
			if(lotof==0) $("#cnt-cart-chbox-"+i).attr("checked","");

			if($("#cnt-cart-chbox-"+i).attr("checked")) 
			{
				$("#cnt-cart-chboximg-"+i).addClass("cnt-cart-chboximg-y");
				$("#cnt-cart-chboximg-"+i).removeClass("cnt-cart-chboximg-n");
			}
			else
			{
			$("#cnt-cart-chboximg-"+i).addClass("cnt-cart-chboximg-n");
			$("#cnt-cart-chboximg-"+i).removeClass("cnt-cart-chboximg-y");
			}

			if(lotof<=5) 
			{
				if($("#cnt-price-"+i).length) price=parseInt($("#cnt-price-"+i).html().replace(/(\s+)/g,""));
				$("#cnt-price-desc-"+i).removeClass("js-hidden");
				$("#cnt-dprice-desc-"+i).addClass("js-hidden");
				$("#cnt-oprice-desc-"+i).addClass("js-hidden");
				$("#cnt-tr-item-"+i).removeClass();
				$("#cnt-tr-item-"+i).addClass("item-price");
			} 
			if(lotof>5&&lotof<=10) 
			{
				price=parseInt($("#cnt-dprice-"+i).html().replace(/(\s+)/g,""));
				$("#cnt-price-desc-"+i).addClass("js-hidden");
				$("#cnt-dprice-desc-"+i).removeClass("js-hidden");
				$("#cnt-oprice-desc-"+i).addClass("js-hidden");
				$("#cnt-tr-item-"+i).removeClass();
				$("#cnt-tr-item-"+i).addClass("item-dprice");
			}
			if(lotof>10) 
			{
				price=parseInt($("#cnt-oprice-"+i).html().replace(/(\s+)/g,""));
				$("#cnt-price-desc-"+i).addClass("js-hidden");
				$("#cnt-dprice-desc-"+i).addClass("js-hidden");
				$("#cnt-oprice-desc-"+i).removeClass("js-hidden");
				$("#cnt-tr-item-"+i).removeClass();
				$("#cnt-tr-item-"+i).addClass("item-oprice");
			}
			if($("#cnt-cart-chbox-"+i).attr("checked")) all_price=all_price+lotof*price;
		}
	}
	if(isNaN(all_price)) all_price="�������";
	if(!isNaN(lotof)&&isFinite(lotof)) chagestoimost($("#all_price"),all_price);
}


 $(document).ready(function(){
	var cart = getCartContents();
	var cartAmount = 0;
	for (var i in cart)
	{
		cartAmount++;
	};
	if(cartAmount) 
	{
		$("#cnt-product-cart-show").removeClass("js-hidden");
		$("#cnt-product-cart-alt").addClass("js-hidden");
	}
	else
	{
		$("#cnt-product-cart-alt").removeClass("js-hidden");
		$("#cnt-product-cart-show").addClass("js-hidden");
	}
}); 

 $(document).ready(function(){
	$(".cnt-product-cart-lotof").change(function(){CalcPrice();});
	
	if($("#tpl-counter").height()>$(window).height()) {$("#tpl-counter").height($(window).height);alert("work?")};
}); 

function chagestoimost(element,value)
{
	if(element.length)
	{
		var from = parseInt(element.html().replace(" ",""));
		var to = value;
		if(from==to) return;
		var offset = parseInt((to-from)/10);
		var st = from;
		var stt;
		for (var i=1;i<=9;i++)
		{
			setTimeout(function()
			{
				st+=offset;
				stt=number_format(st, 0, '', ' ')
				element.html(stt);
			},i*50);
		}
		setTimeout(function(){to=number_format(to, 0, '', ' '); element.html(to)},500);
	}
}

//---------------------------------------------------

function cartAnimationClear(){
	if (!cartAnimation) return;
	if (cartAnimation.image){
		cartAnimation.image.parentNode.removeChild(cartAnimation.image);
		delete cartAnimation.image;
	}
	cartAnimation = false;
};

function cartTriggerHandler(e){
	cartAnimationClear();
	
	var productId = (/js-cart-trigger-([^\s^$]+)/.exec(this.className)[1]);
	var productImage = $(".js-cart-image-" + productId).get(0);
	var cart = document.getElementById("tpl-cart");
	if (!cart || !productImage){
		return false;
		addToCart(productId);
	}
	var productImageOffset = getAbsoluteOffset(productImage);
	var cartFloatingImage = document.createElement("img");
	cartFloatingImage.style.position = "absolute";
	cartFloatingImage.style.left = productImageOffset[0] + "px";
	cartFloatingImage.style.top = productImageOffset[1] + "px";
	cartFloatingImage.src = productImage.src;
	document.body.appendChild(cartFloatingImage);
	
	cartAnimation = new Object();
	
	cartAnimation.image = cartFloatingImage;
	
	cartAnimation.original = {
		x: productImageOffset[0],
		y: productImageOffset[1],
		w: productImage.offsetWidth,
		h: productImage.offsetHeight
	};
	
	cartAnimation.transition = {
		x: productImageOffset[0] - parseInt(productImage.offsetWidth * .05),
		y: productImageOffset[1] - parseInt(productImage.offsetHeight * .05),
		w: parseInt(productImage.offsetWidth * 1.1),
		h: parseInt(productImage.offsetHeight * 1.1)
	};

	cartOffset = getAbsoluteOffset(cart);
	
	cartAnimation.target = {
		x: cartOffset[0] + 110,
		y: cartOffset[1] + 10,
		w: 10,
		h: parseInt(productImage.offsetHeight * 10 / productImage.offsetHeight)
	};
	
	cartAnimation.frame = 0;
	cartAnimation.productId = productId;
	

	cartAnimation.originalScrollTop = $(window).scrollTop();
	cartAnimation.originalScrollLeft = $(window).scrollLeft();

	if (cartAnimation.originalScrollTop > cartOffset[1]){
		cartAnimation.scrollBack = true;
		$(document).scrollTo( '#tpl-cart', 300, {onAfter: cartTween});
	} else {
		cartAnimation.scrollBack = false;
		cartTween();
	};
	
	if (e.stopPropagation) e.stopPropagation();
	if (e.preventDefault) e.preventDefault();
};

$(document).ready(function(){
	$(".js-cart-trigger").click(cartTriggerHandler);
	updateCart();
	setInterval(updateCart, 5000);
	CalcPrice();
});

function BrandColor(brandId,doit)
{
	if(doit=="over")
	{
		$("#cnt-brand-logo-shad-"+brandId).addClass("js-hidden");
		$("#cnt-brand-logo-norm-"+brandId).removeClass("js-hidden");
	}
	if(doit=="out")
	{
		$("#cnt-brand-logo-shad-"+brandId).removeClass("js-hidden");
		$("#cnt-brand-logo-norm-"+brandId).addClass("js-hidden");
	}

}


/* == END OF CART =================================================== */

$(document).ready(function(){
$("#alf-2").css('display','none');
$("#alf-link1").addClass('active');
	$("#alf-link1").click(function(){
$("#alf-2").hide(200);
$("#alf-1").show(200);
$("#alf-link1").addClass('active');
$("#alf-link2").removeClass('active');
});
	$("#alf-link2").click(function(){
$("#alf-1").hide(200);
$("#alf-2").show(200);
$("#alf-link2").addClass('active');
$("#alf-link1").removeClass('active');

});
	
});

/*
$(document).ready(function() {

$('.cnt-filter-table-item .active').click(function(event) {
event.preventDefault ? event.preventDefault() : (event.returnValue=false);
})

})
*/

