var slider = new Array();


var _gaq = _gaq || [];


$(function() {
		$('.smiles a').bind('click', function(){			ta = $('textarea', $(this).parent().parent());

			ta.attr('value', ta.attr('value') + ' ' + $(this).attr('title'));		});


		function setTotalPrice()
	   	{
	    	var prices = $("#cart-table strong.red strong");
	    	var totalPrice = $(".total-price strong");
	    	var totalPriceEconom = $(".total-price-econom strong");
	    	var discount = $('#cupon-discount strong strong').html()*1;

	    	//alert(discount);

	    	var tp = 0;
	    	var ec = 0;

	    	for (var i = 0; i < prices.length; i++) {
	     		var p = prices.eq(i).attr('price')*1;
	     		var e = prices.eq(i).attr('oldprice')*1;

	     		if (!e && (discount || discount==0))
	     		{
	     			p -= Math.round(p*discount/100);
	     			prices.eq(i).html(p);
	     		}

	     		//alert(p);

	     		tp += p;

	     		if (e > p)
	     		{
		     		ec += e;
		     	}
		     	else
		     	{
		     		ec += p;
		     	}
	    	}



	    	totalPrice.text(setBackspaces(tp+""));
	    	if (tp != ec)
	    	totalPriceEconom.text(setBackspaces((ec-tp)+""));
	   	}



	$('#cart-items').val($.cookie('cart'));

	$('a.button').bind('click', function() {
  		if ($(this).attr('rel')) {
   			$('#' + $(this).attr('rel')).submit();
  		}
 	});



 	$('#cupon-trigger').bind('change', function(){ 		t = $(this);
 		if (t.attr('checked'))
 		{
 			$('#recalculate .cupons').show();
 			$('#cupon-discount').show();
 			$('#recalculate .cupons input').trigger('change');
 		}
 		else
 		{ 			$('#cupon-discount strong strong').html('0');
 			$('#recalculate .cupons').hide();
 			$('#cupon-discount').hide();
 		}
		setTotalPrice(); 	}).trigger('change');

 	$('#recalculate .cupons input').bind('change', function(){ 		t = $(this);
 		if (t.attr('checked'))
 		{ 			$('#cupon-discount strong strong').html(t.attr('discount'));
 			$('#cupon-code').attr('value', t.attr('value'));
 			setTotalPrice(); 		} 	}).trigger('change');


 	$('.check-cupon').bind('click', function(){ 		$('#cupon-discount strong strong').html('0');
 		code = $('#cupon-code').attr('value');        $.post('/api/cupon/?code=' + code + '&' + Math.random(),
			function(data) {

		    	if (data.status == 'error') {		    		$('#cupon-result').html(data.message).show();
		    	}
		    	else
		    	{		    		$('#cupon-discount strong strong').html(data.discount);
		    		$('#cupon-discount').show();
		    		$('#cupon-result').html(data.message);

		    	}

		    	setTotalPrice();

		   	},
		   	'json'
		);

 		return false; 	});

 	$('.ajax-form').bind('submit', function(){
  		var t = $(this);
        var r = $('.ajax-result', t);

  		if (t.attr('locked') == 1) return false;
  		t.attr('locked', 1);

  		var form = t.serialize();
		$("input", t).attr("disabled", "disabled");
  		r.hide();

  		$.post('/api/' + t.attr('action') + '?' + Math.random(), form,
			function(data) {
		    	t.attr('locked', 0);

		    	$("input", t).removeAttr("disabled");
		    	r.addClass(data.status).html(data.message).show();

		    	if (data.status != 'error') {
		     		$(".pop-form div", t).hide();

		     		if (t.attr('action') == 'quick_pay')
		     		{		     			img = new Image();
		     			img.src = 'http://mixmarket.biz/tr.plx?e=3779408&r='+escape(document.referrer)+'&t='+(new Date()).getTime();


		     			// GA

						  _gaq.push(['_setAccount', 'UA-12326466-1']);
						  _gaq.push(['_trackPageview']);
						  _gaq.push(['_addTrans',
						    data.order_id,           // order ID - required
						    'goldbags',  			// affiliation or store name
						    data.total_price,          // total - required
						    '0',           // tax
						    '0',              // shipping
						    'Moscow',       // city
						    'MO',     // state or province
						    'Russia'             // country
						  ]);

							_gaq.push(['_addItem',
							    data.order_id,           // order ID - required
							    data.product_articul,           // SKU/code - required
							    data.product_name,        // product name
							    data.category_name,   // category or variation
							    data.product_price,          // unit price - required
							    1               // quantity - required
							  ]);

						  _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers


						  (function() {
						    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
						    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
						    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
						  })();
						  //

		     		}
		    	}

		    	if (data.refresh)
		    		document.location.href = document.location.href;
		   	},
		   	'json'
		);

		return false;
	});
	$("#cart-table .count-changer").keyup(function(e) {
		var val = parseInt(this.value, 10);
	   	if (val) {
	  		var price = $(this).attr("rel");
	  		var oldPrice = $(this).attr("oldprice");

	    	var res = $(this).parent().parent().next().find("strong.red strong");
	    	price = price * val;

	    	res.attr('price', price);

	    	price = setBackspaces(price + "");
	    	res.text(price);
	    	res.attr('oldprice', oldPrice * val);


	    	setTotalPrice();
			this.value = val;
	   	}
		else {
			this.value = "";
		}

	 });

	$("#left-menu > li i").click(function() {
		if (this.parentNode.className == "opened") {
			$("ul", this.parentNode).slideUp();
			this.parentNode.className = "";
		}
		else {
			$("ul", this.parentNode).slideDown();
			this.parentNode.className = "opened";
		}
	});

    $('.p-list').jcarousel({
		wrap: "circular",
		visible: 4,
		scroll: 1,
		initCallback: initSlider
	});

	/*$(".p-list li .item, #c-list li .item:not('.unavailable')").hover(
		function() {
			if (!$(this).hasClass("added"))
				$(".pay", this).stop().animate({width: "55px", marginRight: "10px"}, 200)
		},
		function() {
			if (!$(this).hasClass("added"))
				$(".pay", this).stop().animate({width: "0px", marginRight: "0px"}, 200)
		}
	);*/

	$('#mi-thumbs').jcarousel({
		wrap: "circular",
		visible: 4,
		scroll: 1
	});

	if (document.getElementById("min-val") != null) {
		numSliderInit();
	}

	$("#right-form select, #right-form input[type=checkbox]").live('change', function() {
		submit(this);
	});

	$("#right-form input[type=text]").live('blur', function() {
		submit(this);
	});

	$("#zoom").click(function() {
		var imgs = $("#main-img a").attr("rel").split(";");
		var n = $("#main-img a").attr("class");
		getImagePopup(imgs, n);
	});

	$("#overlay, #imagePopup .image .close").click(function() {
		closeImagesPopup();
	});

	$("#mi-thumbs li a").click(function() {
		if (!$(this.parentNode).hasClass("act")) {
			var newImg = this.rel;
			$("#main-img a").attr("class", this.className);
			$("#main-img a img").attr("src", newImg);
			$("#mi-thumbs li").removeClass("act");
			$(this.parentNode).addClass("act");
		}
	});

	$(".p-list .item .pay a, #c-list .item:not('.unavailable') .pay a").click(function() {
		el = $(this).parent().parent().find("strong");
		var curPrice = el.attr('pprice') * 1;
		var cartPrice = $("#price").attr("cprice") * 1;
		var priceCount = $("#priceCount").attr("ccount")*1;
		var pId = $(this).attr('rel');

		 $("#def-cart-mess").hide();
         $("#tp-info").show();

		if (!$(this).parents(".item").hasClass("added")) {
			priceCount += 1;
			$("#priceCount").attr("ccount", priceCount).text(priceCount + " " + ruscomp(priceCount, "товаров,товар,товара"));

			if (!$("#cart-items").val()) {
		    	$("#cart-items").val(pId);
		   	}
		   	else {
		    	$("#cart-items").val($("#cart-items").val() + "," + pId);
		   	}
		   	$.cookie('cart', $("#cart-items").val(), { expires: 7, path: '/'});

		   	img = new Image();
		   	img.src = 'http://mixmarket.biz/tr.plx?e=3779415&r='+escape(document.referrer)+'&t='+(new Date()).getTime();


			cartPrice += curPrice;
			$("#price").attr("cprice", cartPrice).text(setBackspaces(cartPrice));
			$(".p-list .item .pay a[rel=" + pId + "], #c-list .item .pay a[rel=" + pId + "]").each(function() {
				$(this).text("в корзине").addClass("payed").parent().css({
					width: "55px",
					marginRight: "10px"
				});
				$(this).parents(".item").addClass("added");
			});

		}
	});

	$("#pay-buts .button:not('.notpayed')").click(function() {
		var curPrice = $("#p-rice").attr('pprice') * 1;
		var cartPrice = $("#price").attr("cprice") * 1;
		var priceCount = $("#priceCount").attr("ccount")*1;
		var pId = $("#main-image").attr('ref');

		$("#def-cart-mess").hide();
        $("#tp-info").show();

		if (!$(this).hasClass("added")) {
			priceCount += 1;
			$("#priceCount").attr("ccount", priceCount).text(priceCount + " " + ruscomp(priceCount, "товаров,товар,товара"));
			if (!$("#cart-items").val()) {
				$("#cart-items").val(pId);
			}
			else {
				$("#cart-items").val($("#cart-items").val() + "," + pId);
			}
			$.cookie('cart', $("#cart-items").val(), { expires: 7, path: '/'});
			img = new Image();
		   	img.src = 'http://mixmarket.biz/tr.plx?e=3779415&r='+escape(document.referrer)+'&t='+(new Date()).getTime();


			cartPrice += curPrice;
			$("#price").attr("cprice", cartPrice).text(setBackspaces(cartPrice));

			$(this).addClass("added").removeClass("orange").find("span").text("В корзине");
			$(".lnk-q-pay").hide();
		}

	});

	$("#user-cabinet .lnk-change").click(function() {
		var lnk = $(this);
		if (lnk.hasClass("opened")) {
			$("#edit-form").hide();
			$("#skidka").show();
			$("#user-info").show();
			lnk.removeClass("opened").html("<i></i>изменить");
		}
		else {
			$("#skidka").hide();
			$("#user-info").hide();
			$("#edit-form").show();
			lnk.addClass("opened").text("скрыть");
		}
	});

	$(".c-table .lnk-item").click(function() {
		var td = $(this).parents("tr").next().find(".subtd");
		if ($(this).hasClass("opened")) {
			td.find(".z-form").slideToggle(400, function() {
				td.hide();
			});
			$(this).removeClass("opened");
		}
		else {
			td.show();
			td.find(".z-form").slideToggle(400);
			$(this).addClass("opened")
		}
	});

	$(".button, .jcarousel-prev, .jcarousel-next").mousedown(function() {
		$(this).addClass("pushed");
	});
	$(".button, .jcarousel-prev, .jcarousel-next").mouseup(function() {
		$(this).removeClass("pushed");
	});
	$(".button, .jcarousel-prev, .jcarousel-next").mouseout(function() {
		$(this).removeClass("pushed");
	});

});

function ruscomp($number, $compl) {
	$comp = $compl.split(',');

    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 numSliderInit() {
	var minVal = document.getElementById("min-val").value*1;
	var maxVal = document.getElementById("max-val").value*1;
	var mStep = document.getElementById("m-step").value*1;
	var curMinVal = document.getElementById("min-inp").value*1;
	var curMaxVal = document.getElementById("max-inp").value*1;

	var minSlider = $("#min-count").slider({
		value: curMinVal,
		min: minVal,
		max: maxVal,
		step: mStep,
		slide: function(event, ui) {
			$("#min-inp").val(ui.value);
		},
		change: function() {
			checkSliderVals()
		}
	});

	$("#min-inp")
		.val(minSlider.slider("value"))
		.blur(function() {
			var val = this.value;
			if (!(+val)) {
				val = minVal;
				this.value = minVal;
			}
			minSlider.slider("value", val);
			checkSliderVals();
		});


	var maxSlider = $("#max-count").slider({
		value: curMaxVal,
		min: minVal,
		max: maxVal,
		step: mStep,
		slide: function(event, ui) {
			$("#max-inp").val(ui.value);
		},
		change: function() {
			checkSliderVals();
		}
	});

	$("#max-inp")
		.val(maxSlider.slider("value"))
		.blur(function() {
			var val = this.value;
			if (!(+val)) {
				val = maxVal;
				this.value = maxVal;
			}
			maxSlider.slider("value", val);
			checkSliderVals();
		});

	function checkSliderVals() {
		var min = minSlider.slider("value");
		var max = maxSlider.slider("value");

		if (max < min) {
			document.getElementById("min-inp").value = max;
			document.getElementById("max-inp").value = min;
			minSlider.slider("value", max);
			maxSlider.slider("value", min);
		}

		submit(document.getElementById("max-inp"));
	}
}

var smbtTimerId;
function submit(elem) {
	var pos;
	$(elem).parents(".item").each(function() {
		pos = getElementPosition(this);
	});

	clearTimeout(htTimerId);
	clearTimeout(smbtTimerId);
	hideTooltip();
	$("#rp-tooltip").stop().fadeIn("fast").css({right: "215px", top: pos.top + (pos.height - 62)/2});
	smbtTimerId = setTimeout("ajaxEmulation()", 2000);
	return false;
}

var htTimerId;
function ajaxEmulation() {
	var t = document.getElementById("rp-tooltip");
	$("#rpt-col").html('0');


		$('#ff-oc').attr('value', Math.random());
		var form = $('#filter-form').serialize();
		$.get('/catalog/', form,
			function(data) {
		    	$("#rpt-col").html(data);
		    	$("img", t).hide();
		    	$("#rpt-col").show();
		   	},
		   	'html'
		);
}

function hideTooltip() {
	$("#rpt-col").hide();
	$("#rp-tooltip img").show();
	$("#rp-tooltip").stop().hide();
}

window.onresize = window.onload = function() {
	changeSliderWidth();
}

function changeSliderWidth() {
	var bh;
	if ($("#center-col").length > 0) {
		bh = $("#center-col").width();
	}
	else {
		bh = $("#right-wide-col").width();
	}
	var cItems = $("#c-list li");

	if (slider.length > 0) {
		for (var i = 0; i < slider.length; i++) {
			if (bh > 1029) {
				slider[i].reload();
				slider[i].options.visible = 4;
				slider[i].reload();
			}
			else if (bh > 882) {
				slider[i].reload();
				slider[i].options.visible = 4;
				slider[i].reload();
			}
			else if (bh > 735) {
				slider[i].reload();
				slider[i].options.visible = 4;
				slider[i].reload();
			}
			else if (bh > 588) {
				slider[i].reload();
				slider[i].options.visible = 4;
				slider[i].reload();
			}
			else {
				slider[i].reload();
				slider[i].options.visible = 3;
				slider[i].reload();
			}
		}
	}

	if (cItems.length > 0) {

		if (bh > 1029) {
			cItems.width("14%");
		}
		else if (bh > 882) {
			cItems.width("16%");
		}
		else if (bh > 735) {
			cItems.width("20%");
		}
		else if (bh > 588) {
			cItems.width("25%");
		}
		else {
			cItems.width("32%");
		}
	}
}

function initSlider(carousel) {
	slider.push(carousel);
}

function changeColor(sel) {
	if (sel.options[sel.selectedIndex].value == '') {
		$("#color").hide();
	}
	else {
		$("#color").show().css("background-color", sel.options[sel.selectedIndex].value);
	}
}

var pop;
var popLink;
function getPopup(popId, lnk, isQuickPay) {
	if (popId != null) {
		popLink = lnk;
		pop = document.getElementById(popId);
		pop.style.display = "block";
		addEvent(document, 'click', popClickFunc);

		if (isQuickPay) {
			var pos = getElementPosition(lnk);
			pop.style.top = pos.top - 220 + "px";
			pop.style.left = pos.left - 120 + "px";
			if ($(lnk).parents(".item").length > 0) {
				$("#quick-pay-product-id").val($(lnk).parents(".item").find(".pay a").attr("rel"));
			}
			else {
				$("#quick-pay-product-id").val($("#main-image").attr("ref"));
			}
		}

		if (popId == "login-popup") {
			var pos = getElementPosition(lnk);
			if ($(lnk.parentNode).attr("id") == "lnk-cab") {
				pop.style.position = "fixed";
				pop.style.top = pos.top + pos.height + 2 + "px";
				pop.style.left = pos.left - 254 + $(lnk).width() / 2 + "px";
			}
			else {
				pop.style.top = pos.top + pos.height + 2 + "px";
				pop.style.left = pos.left - 254 + $(lnk).width() / 2 + "px";
			}
		}

	}
}

function popClickFunc(event) {
	var event = event || window.event;
	var t = event.target || event.srcElement;

	if (t != pop && !isChildNode(pop, t) && t != popLink && !isChildNode(popLink, t)) {
		closePopup();
		removeEvent(document, 'click', popClickFunc);
	}
}

function closePopup() {
	$('.popup .pop-form div').show();
 	$('.popup .pop-form p.ajax-result').html('').hide();
	$(".popup").css("display", "none");

	$("#pop-remember").hide();
	$("#login-pu-form").show();

}

function getImagePopup(imgs, num) {
	if (imgs.length > 0) {
		var pop = $("#imagePopup");
		var imgBlock = pop.find(".image");
		var img = $("#ip-image");

		$("#overlay").height(getDocumentHeight()).show();
		pop.css("top", getBodyScrollTop() + 20).show();

		img.attr("src", imgs[num]).attr("rel", 0);
		img.attr("height", $(window).height() - 50);
		img.load(function() {
			imgBlock.height($(window).height() - 50).css("visibility", "visible");
		});
		$("#ip-prev").unbind("click").bind("click", function() {
			changeImage(imgs, "prev");
		});
		$("#ip-next").unbind("click").bind("click",function() {
			changeImage(imgs, "next");
		});
	}
}

function closeImagesPopup() {
	$("#imagePopup, #overlay").hide();
}

$(document).unbind('keydown.fb').bind('keydown.fb', function(e) {
	if (e.keyCode == 27) {
		$("#imagePopup, #overlay").hide();
	}
});

function changeImage(imgs, param) {
	var img = $("#ip-image");
	var pop = $("#imagePopup");
	var imgBlock = pop.find(".image");
	var curIndx = parseInt(img.attr("rel"));
	var newIndx;

	if (param == "next") {
		if ((imgs.length - 1) > curIndx) {
			newIndx = curIndx + 1;
		}
		else {
			newIndx = 0;
		}
	}
	else {
		if (curIndx > 0) {
			newIndx = curIndx - 1;
		}
		else {
			newIndx = imgs.length - 1;
		}
	}
	imgBlock.css("visibility", "hidden");
	img.attr("src", imgs[newIndx]).attr("rel", newIndx);
	img.load(function() {
		imgBlock.height("auto").css("visibility", "visible");
	});
}

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;
}

function getElementPosition(elem) {
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;

    var l = 0;
    var t = 0;

    while (elem) {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return { "left": l, "top": t, "width": w, "height": h };
}

function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent( "on"+type, fn );
}

function removeEvent(obj, type, fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(type, fn, false);
	else if (obj.detachEvent)
		obj.detachEvent( "on"+type, fn );
}

function isChildNode(elem, sell) {
	for (var childItem in elem.childNodes) {
		if (elem.childNodes[childItem].nodeType == 1) {
			if (elem.childNodes[childItem] == sell)
				return true;
			else if (isChildNode(elem.childNodes[childItem], sell))
				return true;
		}
	}
	return false;
}

function getClientWidth() {
	return document.compatMode == 'CSS1Compat' || window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

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());
}

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
