function refreshBasketSmall()
{
	$.ajax({
		async: true,
		type: "GET",
		url: "/includes/ajax/basket.small.php",
		data: "",
		beforeSend: function() {
		},
		success: function(msg) {
			$("div.block-basket div.in").html(msg);
		}
	});
}

function refreshBasketPopup()
{
	$.ajax({
		async: true,
		type: "GET",
		url: "/includes/ajax/basket.popup.php",
		data: "",
		beforeSend: function() {
		},
		success: function(msg) {
			$("#basket div.in").html(msg);
		}
	});
}

$(document).ready(function() {
	
	if (!$.browser.webkit) {
		$('INPUT[placeholder], TEXTAREA[placeholder]').blur(function(){ 
			if ($(this).val()=='') {
				$(this).val($(this).attr('placeholder'));
				$(this).addClass('m-placeholder');
			}
		}).focus(function(){
			$(this).removeClass('m-placeholder');
			if ($(this).val()==$(this).attr('placeholder'))
				$(this).val('');
		}).each(function(){
			if ( ($(this).val()=='') || ($(this).val()==$(this).attr('placeholder')) ) {
				$(this).val( $(this).attr('placeholder') );
				$(this).addClass('m-placeholder');
			}
			var form = $(this).closest('FORM');
			if (form.length)
				form.submit(function(){
					if ($(this).val()==$(this).attr('placeholder'))
						$(this).val('');
				});
		});	
	}
	
	$("#bannerSlide").easySlider({
		auto: true, 
		continuous: true,
		numeric: true,
		pause: 5000
	});


	$('#navigator table td').hover(
		function(){
			if ($.browser.msie)	{
				width =  $(this).find('.menu-down').width();
				$(this).find('div.top').css( 'width', width );
				$(this).find('div.bottom').css( 'width', width );
			}
			$(this).addClass('hover');
			$(this).find('.menu-down').slideDown(0);
		},
		function(){
			$(this).removeClass('hover');
			$(this).find('.menu-down').slideUp(0);
		}
	);
	
	$("a[href=#show_basket]").live('click', function() {
		$('#enter').hide();
		$('#enter2').hide();
		$('#backcall').hide();
		$('#reg').hide();
		$('#basket').show();
		return false;
	});
	
	$("a[href=#callback]").click(function() {
		$('#enter').hide();
		$('#enter2').hide();
		$('#basket').hide();
		$('#reg').hide();
		$('#backcall').show()
		return false;
	});
	
	$("a[href=#auth]").click(function() {
		$('#enter2').hide();
		$('#basket').hide();
		$('#reg').hide();
		$('#backcall').hide()
		$('#enter').show()
		return false;
	});
		
	$("a[href=#reg]").click(function() {
		$('#enter').hide();
		$('#enter2').hide();
		$('#basket').hide();
		$('#backcall').hide()
		$('#reg').show()
		return false;
	});
	
	$("#backcall div.close").click(function() {
		$('#backcall').fadeOut();
	})
	
	$("#enter div.close, #enter2 div.close").click(function() {
		$('#enter').fadeOut();
		$('#enter2').fadeOut();
	})
	
	$("#reg div.close").click(function() {
		$('#reg').fadeOut();
	})

	$("#basket div.close").live('click', function() {
		$('#basket').fadeOut();
	})
	
	$("a[href*=ADD2BASKET]").click(function() {
		var link = $(this);
		var count = $(this).parent().find("input").val();
		var href = $(this).attr("href").substr($(this).attr("href").indexOf("?")+1);
		$.ajax({
			async: true,
			type: "GET",
			url: "/includes/ajax/basket.add.php?"+href+"&count="+count,
			data: "",
			beforeSend: function() {
			},
			success: function(msg) {
				if ($(link).find("img").attr("src") == "/bitrix/templates/pampers/img/add2basket_table.png")
				{
					$(link).find("img").attr("src", "/bitrix/templates/pampers/img/add2basket_table_active.png");
				}
				else
				{
					$(link).find("img").attr("src", "/bitrix/templates/pampers/img/add2basket_list_active.png");
				}
				$(link).find("img").attr("title", "Товар в корзине");
				$(link).find("img").attr("alt", "Товар в корзине");
				
				$(link).parent().find("div.in_basket").slideDown("slow", function () {
					setTimeout(function() {	$(link).parent().find("div.in_basket").stop().slideUp() }, 2000)
				});	
				refreshBasketSmall();	
				refreshBasketPopup();		
			}
		});
		return false;
	})	
	
	$("a[href*=delete_item]").live('click', function() {
		var href = $(this).attr("href").substr($(this).attr("href").indexOf("?")+1);
		var product_id = $(this).attr("href").substr($(this).attr("href").indexOf("&product_id=")+12);
		$.ajax({
			async: true,
			type: "GET",
			url: "/includes/ajax/basket.delete.php?"+href,
			data: "",
			beforeSend: function() {
			},
			success: function(msg) {
				$("#basket div.in").html(msg);
				
				$("a.basket").each(function(i) {
					var tmp_id = $(this).attr("href").substr($(this).attr("href").indexOf("&id=")+4);
					if (tmp_id == product_id)
					{
						var image = $(this).find("img");
						if ($(image).attr("src") == "/bitrix/templates/pampers/img/add2basket_table_active.png")
						{
							$(image).attr("src", "/bitrix/templates/pampers/img/add2basket_table.png");
						}
						else
						{
							$(image).attr("src", "/bitrix/templates/pampers/img/add2basket_list.gif");
						}
						
						$(image).attr("title", "Добавить в корзину");
						$(image).attr("alt", "Добавить в корзину");
					}
				});
				refreshBasketSmall();
			}
		});
		return false;
	})	
	
	$("#catalog-more-photo a").click(function() {
		$("#catalog-image div").hide();
		
		var divId = $(this).find("img").attr("class");
		$("#"+divId).show();
		return false;
	})
	
	$("#catalog-filter div.border-dotted:last").addClass("border-none");
	
	$("#catalog-filter div.all a").click(function() {
		var link = $(this);
		$(this).parent().parent().find("div.items-more").toggle("slow", function() {
			if ($(link).text() == "свернуть список") 
				$(link).text("показать все");
			else
				$(link).text("свернуть список");
		});
		return false;
	})
	
	$("div.menu-eat div.menu-all ul li:last").addClass("last");
	
	$("#catalog-more-photo div.top div").hide();
	if ($("#catalog-more-photo div.content div.in div.sm").length > 3) $("#catalog-more-photo div.bottom div").show();
	
	$("#catalog-more-photo div.top div").click(function() {
		var top = $("#catalog-more-photo div.content div.in").css('top');
		var height = $("#catalog-more-photo div.content div.in div.sm").length*115 - 15;
		top = parseInt(top);
		top += 115;
		$("#catalog-more-photo div.content div.in").css('top', top+"px");
		
		$("#catalog-more-photo div.bottom div").show();
		if (top === 0)
		{
			$("#catalog-more-photo div.top div").hide();
		}
	})
	
	$("#catalog-more-photo div.bottom div").click(function() {
		var top = $("#catalog-more-photo div.content div.in").css('top');
		var height = $("#catalog-more-photo div.content div.in div.sm").length*115 - 15;
		top = parseInt(top);
		top -= 115;
		$("#catalog-more-photo div.content div.in").css('top', top+"px");
		
		$("#catalog-more-photo div.top div").show();
		if (-1*(top - parseInt($("#catalog-more-photo div.content div.in").height())) >= height)
		{
			$("#catalog-more-photo div.bottom div").hide();
		}
	})
	
	var productLiCurrent = 0;
	var productSpeed = 165;
	var productLiWidth = 165;
	
	if ($('#product ul li').length <= 4)
	{
		$('#product div.right-ar').hide();
		$('#product div.left-ar').hide();
	}
	$('#product ul').css('width', $('#product ul li').length * productLiWidth);
	
	$('#product div.right-ar').click(function()
	{
		if (productLiCurrent == $('#product ul li').length-5 )
		{
			$('#product ul').animate({left: 0}, productSpeed);
			productLiCurrent = 0;
		}
		else 
		{
			$('#product ul').animate({ left: '-=' + $('#product ul li').width()}, productSpeed);
			productLiCurrent ++;
		}
	})
	
	$('#product div.left-ar').click(function(){
		if (productLiCurrent == 0)
		{
			$('#product ul').animate({ left: '-=' + ($('#product ul').width()-5*productLiWidth)}, productSpeed);
			productLiCurrent = $('#product ul li').length-5;
		}
		else 
		{
			$('#product ul').animate({ left: '+=' + $('#product ul li').width()}, productSpeed);
			productLiCurrent --;
		}		
	})
	
	$("table.personal_order tr").each(function() {
		 if ($(this).hasClass('b-another-data'))
        	$(this).hide();
	})
	
	$("table.personal_order td.num a").click(function(){
		$("#"+$(this).attr("id")+"-list").toggle();
		return false;
	});
	
	$("a.fancybox").fancybox({'titlePosition': 'inside'});
})
