$(document).ready(function() {
	var sfEls = document.getElementById("pulldown-menu");
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (sfEls && (sfEls = document.getElementById('pulldown-menu').getElementsByTagName('li'))) {
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() { this.className+=" jshover"; }
			sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" jshover\\b"), ""); }
		}
	}
	if (IE6) {
		for (items = $('.nav > ul > li'), i = items.length; i--;) {
			items[i].onmouseover=function() { this.className+=" hover"; }
			items[i].onmouseout=function()  { this.className=this.className.replace(new RegExp(" hover\\b"), ""); }
		}
	}
});

jQuery.fn.cols = function(c) {
	var config = jQuery.extend({
		base : 300,
		threshold : 200,
		evenColumns : false,
		maxColumns : 10
	}, c);
	return jQuery(this).each(function() {
		var $this = jQuery(this);
		return {
			initialize : function() {
					jQuery(window).resize(this.resize);
					this.resize();
				},
			resize : function() {
					var a = null, b = config.base, t = config.threshold, c = 0, d = null;
					a = $this.width();
					while (b < 2400) {
						c++;
						if (a <= b + t && a >= b || c >= parseInt(config.maxColumns)) {
							if (config.evenColumns && (c % 2 !== 0)) { continue; }
							d = 'c' + c;
							break;
						}
						b += t;
					}
					if (d) { $this.removeClass("c1 c2 c3 c4 c5 c6 c7 c8 9 c10").addClass(d); }
				}
		}.initialize();
	});
};

function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html(text).toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
	else if (mode == "list")
		$(element).html(text).removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart")
			.unbind('click').css("cursor", "default");
}

function addToCart(element, imageToFlyId, mode, text)
{
	if (!element || !element.href)
		return;

	var button = $(element);
	if (mode == "detail")
		button.unbind('click').css("cursor", "default");
	else if (mode == "list")
		button.removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart").unbind('click').css("cursor", "default");

	$.get(
		element.href + "&ajax_buy=1",
		$.proxy(
			function(data) {

				if (this.mode == "detail")
					this.button.removeAttr("href").html(text).toggleClass("disabled");
				else if (this.mode == "list")
					this.button.removeAttr("href").html(text);

				var imageElement = document.getElementById(this.imageToFlyId);
				if (!imageElement)
				{
					$("#headbasket").html(data);
					return;
				}

				var hoverClassName = "";
				var wrapper = null;
				if (this.mode == "detail")
				{
					hoverClassName = "catalog-detail-hover";
					wrapper = this.button.parents("div.catalog-detail");
				}
				else if (this.mode == "list")
				{
					hoverClassName = "catalog-item-hover";
					wrapper = this.button.parents("li.product");
				}

				wrapper.unbind("mouseover").unbind("mouseout").removeClass(hoverClassName);

				var imageToFly = $(imageElement);
				var position = imageToFly.position();
				var desc_position = $("#headbasket").position();
				var flyImage = imageToFly.clone().insertBefore(imageToFly);

				flyImage.css({ "position": "absolute", "left": position.left, "top": position.top });
				flyImage.animate({ width: 0, height: 0, left: desc_position.left + 120, top: desc_position.top }, 600, 'linear');
				flyImage.data("hoverClassName", hoverClassName);
				flyImage.queue($.proxy(function() {

					this.flyImage.remove();
					$("#headbasket").html(data);

					if (this.wrapper.data("adminMode") === true)
					{
						var hoverClassName = "";
						if (this.mode == "detail")
							hoverClassName = "catalog-detail-hover";
						else if (this.mode == "list")
							hoverClassName = "catalog-item-hover";

						this.wrapper.addClass(hoverClassName).bind({
							mouseover: function() { $(this).removeClass(hoverClassName).addClass(hoverClassName); },
							mouseout: function() { $(this).removeClass(hoverClassName); }
						});
					}

				}, {"wrapper" : wrapper, "flyImage" : flyImage, "mode": this.mode}));

			}, { "button": button, "mode": mode, "imageToFlyId" : imageToFlyId }
		)
	);

	return false;
}

function disableAddToCompare(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;

	$(element)
		.removeClass("catalog-item-compare").addClass("catalog-item-compared")
		.text(text)
		.unbind('click').removeAttr("href")
		.css("cursor", "default");

	return false;
}

function addToCompare(element, text)
{
	if (!element || !element.href) 
		return;

	var href = element.href;
	var button = $(element);

	button.removeClass("catalog-item-compare").addClass("catalog-item-compared").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				var compare = $("#headcompare");
				compare.html(data);
				
				this.text(text);

				if (compare.css("display") == "none") {
					compare.css({ "display": "block", "height": "0" });
					compare.animate({ "height": "22px" }, 300);
				}
			}, button
		)
	);
	
	return false;
}

var mls = [];
function mlsprep() {
	if (!$.isArray(mls)) return;
	var a = $.merge([['ml-st', 'post'], ['ml-kp', '2661290', 'mail.ru']], mls);
	for (var i = 0; i < a.length; i++) {
		if (!a[i][0] || !a[i][1] || !(el = $('.' + a[i][0]))) continue;
		g = (a[i][2] ? a[i][2] : 'stans.ru');
		el.html('<a href="mailto:' + a[i][1] + '%40' + g + '">' + a[i][1] + '@' + g + '</a>');
	}
}

