// Устанавливает печеньку :)
function SetCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "; expires=0, 01-01-2050 00:00:00 GMT") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	if ((name + "=" + escape(value)).length <= 4000) document.cookie = curCookie;
}

// Читает печеньку :)
function GetCookie(name, defval) 
{
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) return defval;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function FormatInt(n)
{
	n = parseInt(n);
	var text = (n % 1000).toString();
	n = parseInt(n / 1000);
	while(n > 0)
	{
		while((text.length+1) % 4 > 0) text = "0" + text;
		text = (n % 1000).toString() + " " + text;
		n = parseInt(n / 1000);
	}
	return text;
}

function AddToCmp(good_id, quantity)
{
	if(!quantity) quantity = 1;
	$.ajaxSetup({async: false});
	$.post('/inlinecompare', { 'good_id': good_id, 'action': 'add', 'quantity': quantity });
	$('#incompare').load('/inlinecompare');
	$('#'+good_id).css('display','none');
	$('#'+good_id+'_delcmp').css('display','block');
}

function DelFromCmp(good_id)
{
	$.ajaxSetup({async: false});
	$.post('/inlinecompare', { 'del_items[]': good_id, 'action': 'show', 'quantity': 1 });
	$('#incompare').load('/inlinecompare');
	$('#'+good_id).css('display','block');
	$('#'+good_id+'_delcmp').css('display','none');
}

jQuery(function($)
{
	$('[event=run-compare]').click(function() 
	{
		if ($("#goods_in_cart").text() < 2)
		{
			alert("Пожалуйста, выберите минимум два товара для сравнения.");
			return false;
		}
		else
			document.location.href = '/ajaxcompare';
	});
	
	
	eur_currency = 0;
	
	$('[event=select-eur]').click(function() 
	{
		$('[event=select-eur]').css("text-decoration", "none");
		$('[event=select-rub]').css("text-decoration", "underline");
		SetCookie("currency", "eur");
		$("span[type=currency]").text("евро");
		$("span[currency][value]").each(function()
		{
			if($(this).attr("currency").match(/eur/i))
			{
				$(this).text(FormatInt($(this).attr("value")));
			}
			else
			{
				$(this).text(FormatInt($(this).attr("value") / eur_currency));
			}
		});
	});
	
	$('[event=select-rub]').click(function() 
	{
		$('[event=select-eur]').css("text-decoration", "underline");
		$('[event=select-rub]').css("text-decoration", "none");
		SetCookie("currency", "rub");
		$("span[type=currency]").text("руб.");
		$("span[currency][value]").each(function()
		{
			if($(this).attr("currency").match(/rub/i))
			{
				$(this).text(FormatInt($(this).attr("value")));
			}
			else
			{
				$(this).text(FormatInt($(this).attr("value") * eur_currency));
			}
		});
	});
	
	$.getJSON("/cources.php", function(data)
	{
		eur_currency = data.EUR;
		if(GetCookie("currency", "rub") == "rub")
		{
			$("[event=select-rub]").click();
		}
		else
		{
			$("[event=select-eur]").click();
		}
	});
});
