// nexus
function resize_footer()
{
	if($('.content').height() + 240 > $('.catalog').height() + 340)
	{
		var bestheight = '.content';
		var bestheight_m = 300;
	} else {
		var bestheight = '.catalog';
		var bestheight_m = 400;
	}
	
	if($(bestheight).height() + bestheight_m < $(window).height())
		$('.copy, .copy_info').css({ top: $(window).height() - $('.copy').height() });
	else
		$('.copy, .copy_info').css({ top: $(bestheight).height() + bestheight_m });
}

$(document).ready(function()
{
	// Футер
	if($('.content').height() + 240 > $('.catalog').height() + 340)
	{
		var bestheight = '.content';
		var bestheight_m = 300;
	} else {
		var bestheight = '.catalog';
		var bestheight_m = 400;
	}
	
	if($(bestheight).height() + bestheight_m < $(window).height())
		$('.copy, .copy_info').css({ top: $(window).height() - $('.copy').height() });
	else
		$('.copy, .copy_info').css({ top: $(bestheight).height() + bestheight_m });

	$(window).resize(function()
	{
		resize_footer();
	});
	
	setInterval(function()
	{
		resize_footer();
	}, 500)
	
	// В корзину
	$('.content .item_list .item').hover(function()
	{
		$('.to_basket_text').remove();
		$('body').append('<div class="to_basket_text"></div>');
		$('.to_basket_text').css(
		{
			top: $('.to_basket a', this).offset().top + 13,
			left: $('.to_basket a', this).offset().left - 30,
			opacity: .0,
			display: 'block'
		}).animate(
		{
			opacity: 1,
			top: '+=7px'
		}, 'fast');
	}, function()
	{
		$('.to_basket_text').remove();
	});
	
	// Разделы на главной
	$('.main_sections .item_main_section img').each(function()
	{
		$(this).mouseenter(function()
		{
			if($('img:animated', $(this).parent()).length) return false;
			
			$(this).css({ position: 'absolute', left: $(this).position().left, top: $(this).position().top });

			var pL = $(this).position().left;
			var pT = $(this).position().top;
			
			$(this)
			.animate({ width: 140, height: 140, top: (pT + 5), left: (pL + 5) }, 100)
			.animate({ width: 170, height: 170, top: (pT - 15), left: (pL - 15) }, 200)
			.animate({ width: 150, height: 150, top: pT, left: pL }, 100, function()
			{
				$(this).css({ position: '' });
			});
		});
	});
});


// bitrix

function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html("<span>" + text + "</span>").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.toggleClass("disabled").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("<span>" + text + "</span>");
				else if (this.mode == "list")
					this.button.removeAttr("href").html(text);*/

				$('.basket_info').html(data);
				var imageElement = document.getElementById(this.imageToFlyId);
				/*if (!imageElement)
				{
					$("#cart_line").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("div.catalog-item");
				}

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

				var imageToFly = $(imageElement);
				var position = imageToFly.position();
				var offset = imageToFly.offset();
				//var flyImage = imageToFly.clone().insertBefore(imageToFly);
				var flyImage = imageToFly.clone().appendTo('body')
				
				
				var personal_offset = $('.personal').offset();
				//flyImage.css({ zIndex: 999, "left": offset.left - 250, "top": offset.top - 200, "position": "absolute" });
				flyImage.css({ zIndex: 999, "left": offset.left, "top": offset.top, "position": "absolute" });
				//flyImage.animate({ width: 0, height: 0, left: personal_offset.left - 250, top: personal_offset.top - 200 }, 1000, 'linear');
				flyImage.animate({ width: 0, height: 0, left: personal_offset.left, top: personal_offset.top }, 1000, 'linear');
				flyImage.data("hoverClassName", hoverClassName);
				flyImage.queue($.proxy(function() {

					this.flyImage.remove();
					$("#cart_line").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 = $("#compare");
				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;
}
