$(document).ready(function()
	{
		$.ifixpng("/lib/img/1x1.gif");
		$("img[src$=.png]").ifixpng();
		$("a[rel^='zoom']").fancybox({zoomSpeedIn: 500, zoomSpeedOut: 500, showCloseButton: false, padding: 10});
		$(".cycle").cycle();
	});

function add_to_card(id, prc)
	{
		var cookie = getCookie('cart');
		var cart = new Array();
		var i;
		var item = new Array();
		if (cookie)
			{
				cart = cookie.split('^');
				for (i = 0; i < cart.length; i++)
					{
						item = cart[i].split(',');
						if (item[0] == id)
							{
								alert('Этот товар уже есть в корзине.');
								return;
							}
					}
			}
		var expires = new Date();
		expires.setDate(expires.getDate() + 60);
		var type = "";
		if (document.getElementById('ptype'))
			{
				type = document.getElementById('ptype').value;			
			}

		
		setCookie('cart', ((getCookie('cart')) ? getCookie('cart') : "") + "^" + id + ",1," + type, expires);
		alert('Товар добавлен в корзину.');
				$('#cart-empty').css({display: 'none'});
				$('#cart-content').css({display: 'block'});

				$("#submenu-cart-count").text(parseFloat($("#submenu-cart-count").text()) + 1);
				var total = $("#submenu-cart-total").text();
				price = prc.replace(/\,/g, '.');
				price = parseFloat(price);
        total = total.replace(/\,/g, '.');
        total = parseFloat(total) + price;
        total = sprintf("%.2f", total);
        total = total.replace(/\./g, ',');
        $("#submenu-cart-total").text(total);
	}

function inc_product(id)
	{
		var cookie = getCookie('cart');
		var r = new RegExp('\\^' + id + ',(\\d+)', '');
 		var i = cookie.match(r);
 		var count = parseInt(i[1]) + 1;
		cookie = cookie.replace(r, '^' + id + ',' + count);
		var expires = new Date();
		expires.setDate(expires.getDate() + 60);
		setCookie('cart', cookie, expires);

		$("#cart_count_" + id).text(count);
		var price = $("#cart_price_" + id).text();
		price = price.replace(/\,/g, '.');
		price = parseFloat(price);
		var sum = count * price;
		sum = sprintf("%.2f", sum);
		sum = sum.replace(/\./g, ',');
		$("#cart_sum_" + id).text(sum);

		var total = $("#cart_total").text();
		total = total.replace(/\,/g, '.');
		total = parseFloat(total);
		total = total + price;
		total = sprintf("%.2f", total);
		total = total.replace(/\./g, ',');
		$("#cart_total").text(total);
	}

function dec_product(id)
	{
		var cookie = getCookie('cart');
		var r = new RegExp('\\^' + id + ',(\\d+)', '');
 		var i = cookie.match(r);
 		var count = parseInt(i[1]) - 1;
 		if (count == 0) {del_product(id); return;}
		cookie = cookie.replace(r, '^' + id + ',' + count);
		var expires = new Date();
		expires.setDate(expires.getDate() + 60);
		setCookie('cart', cookie, expires);

		$("#cart_count_" + id).text(count);
		var price = $("#cart_price_" + id).text();
		price = price.replace(/\,/g, '.');
		price = parseFloat(price);
		var sum = count * price;
		sum = sprintf("%.2f", sum);
		sum = sum.replace(/\./g, ',');
		$("#cart_sum_" + id).text(sum);

		var total = $("#cart_total").text();
		total = total.replace(/\,/g, '.');
		total = parseFloat(total);
		total = total - price;
		total = sprintf("%.2f", total);
		total = total.replace(/\./g, ',');
		$("#cart_total").text(total);
	}

function del_product(id)
	{
		var cookie = getCookie('cart');
		var r = new RegExp('\\^' + id + ',(\\d+)', '');
		cookie = cookie.replace(r, '');

		var price = $("#cart_price_" + id).text();
		price = price.replace(/\,/g, '.');
		price = parseFloat(price);
		
		var count = $("#cart_count_" + id).text();
		count = count.replace(/\,/g, '.');
		count = parseFloat(count);
		var sum = count * price;

		var total = $("#cart_total").text();
		total = total.replace(/\,/g, '.');
		total = parseFloat(total);
		total = total - sum;
		total = sprintf("%.2f", total);
		total = total.replace(/\./g, ',');
		$("#cart_total").text(total);
		$("#cart_product_" + id).remove();

		if (!cookie) {deleteCookie('cart'); window.location.reload(); return;}
		var expires = new Date();
		expires.setDate(expires.getDate() + 60);
		setCookie('cart', cookie, expires);
	}

function setCookie(name, value, expires, path, domain, secure)
	{
		document.cookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; 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);
	}

function deleteCookie(name, path, domain)
	{
		if (getCookie(name))
			{
				document.cookie = name + "=" + 
					((path) ? "; path=" + path : "; path=/") +
					((domain) ? "; domain=" + domain : "") +
					"; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
	}

/*
 * sprintf() for JavaScript v.0.4
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
*/

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}
