$(document).ready(function(){

	//слайдер для новостей
	var link_news = $("#news .link");
	
	$("#all_hidden").click(function(){
		$("#news .link div").each(function(){$(this).css({backgroundPosition: "0 0"});});
		$("#news div.cont:visible").each(function(){$(this).slideUp(200);});
		return false;
	});
		
	link_news.click(function(){
		block_slider_vis = $(this).parent().next().find("div.cont:visible");
		block_slider = $(this).parent().next().find("div.cont");
		if (block_slider_vis.length == 1)
		{
			block_slider.slideUp(200);
			$(this).find("div").css({backgroundPosition: "0 0"});
		}
		else
		{
			block_slider.slideDown(200);
			$(this).find("div").css({backgroundPosition: "0 -12px"});
		}
		return false;
	});
	
	//добавление товара в коризну и в закладки	
	/*var call = 0;
	var favorite = 0;
	$("a.basket").click(function(){
		enter_mess("b-", $(this), "Товар успешно добавлен в коризну");
		return false;
	});
	$("a.favorite").click(function(){
		enter_mess("f-", $(this), "Товар успешно добавлен в закладки");
		return false;
	});
	
	function enter_mess(e, elem, str){
		var	b_message = $("div.catalog .b-message");
		var	f_message = $("div.catalog .f-message");
		conteiner = elem.parent();
		random_id = Math.floor(Math.random()*1000);
		if (b_message.length == 1)
		{
			b_message.remove();
		}
		if (f_message.length == 1)
		{
			f_message.remove();
		}
		
		clearTimeout(call);
		
		conteiner.append("<div class='" + e + "message' id='" + e + random_id + "'>" + str + "</div>");
		conteiner.find("div." + e + "message").animate({opacity: 1}, 300);
		call = setTimeout(function(){remove_mes_basket(random_id, call, e)}, 1100);
		return false;
	}
	
	function remove_mes_basket(id, timeout, name){
		$("#" + name + id).animate({opacity: 0}, 300, function(){$(this).remove();});
		clearTimeout(timeout);
	}*/
	
	//функции для всплывающих менюшек
	$("#catalog").hover(
		function()
		{
			if ($("#sections div.item").hasClass("close"))
				$("#sections div.item").stop().animate({width: "100%"}, 150);
			$("#sections").find("div.wrap").stop(true,true).slideUp(200);
			$("#sections_menu").stop(true,true).delay(150).slideDown(200);
		},
		function()
		{
			if ($("#sections div.item").hasClass("close"))
				$("#sections div.item").stop().animate({width: "18px"}, 150);
			$("#sections_menu").stop(true,true).hide(200);
			$("#sections").find("div.wrap").stop(true,true).delay(150).slideDown(200);
		}
	);
	
	
	$("#sections").hover(
		function()
		{
			if ($("#sub_sections div.item").hasClass("close"))
				$("#sub_sections div.item").stop().animate({width: "100%"}, 200);
			$("#sub_sections").find("div.wrap").stop(true,true).slideUp(200);
			$("#sub_menu").stop(true,true).delay(150).slideDown(200);
		},
		function()
		{
			if ($("#sub_sections div.item").hasClass("close"))
				$("#sub_sections div.item").stop().animate({width: "18px"}, 200);
			$("#sub_menu").stop(true,true).hide(200);
			$("#sub_sections").find("div.wrap").stop(true,true).delay(150).slideDown(200);
		}
	);
	
	$("#sub_sections").hover(
		function()
		{
			if ($("#category div.item").hasClass("close"))
				$("#category div.item").stop().animate({width: "100%"}, 200);
			$("#category").find("div.wrap").stop(true,true).slideUp(200);
			$("#category_menu").stop(true,true).delay(150).slideDown(200);
		},
		function()
		{
			if ($("#category div.item").hasClass("close"))
				$("#category div.item").stop().animate({width: "18px"}, 200);
			$("#category_menu").stop(true,true).hide(200);
			$("#category").find("div.wrap").stop(true,true).delay(150).slideDown(200);
		}
	);

	/*if (!$("#category div.item").hasClass("close"))
	{
		$("#category").bind("mouseenter",function()
			{
				$("#category").find("div.wrap").slideUp(200);
				$("#category_menu").delay(200).slideDown(200);
			}
		);
	}
	
	$("#value").bind("mouseover",function()
		{
			$("#category_menu").stop(true,true).hide(200);
			$("#category").find("div.wrap").stop(true,true).delay(150).slideDown(200);
		}
	);*/

	$("div.arr_menu_f").bind("mouseover",function()
		{
			$(this).prev().stop(true,true).slideDown(200);
			$(this).css("backgroundPosition","center -7px");
		}
	);
	
	$("#category_menu li").hover(
		function()
		{
			$(this).find("div.sub_menu").stop(true,true).delay(150).show(150);
		},
		function()
		{
			$(this).find("div.sub_menu").stop(true,true).hide(150);
		}
	);
	
	$("div.hidden_result").bind("mouseleave",function()
		{
			$("div.hidden_result div.cont").stop(true,true).slideUp(200);
			$("div.arr_menu_f").css("backgroundPosition","center 3px");
		}
	);
	
	$("#f_wrap div.close, #f_overlay").click(function(){
		$("#f_overlay, #f_wrap").css("display","none");
	});
	
	
});

function openForm(el){
	$("html,body").animate({scrollTop: 0}, 300);
	$("#f_wrap div.act").removeClass("act").css("display","none");
	$("#"+el).addClass("act").css("display","block");
	heightWin = $(window).height();
	heightEl = $("#f_wrap").height();
	if (heightWin<=heightEl)
		$("#f_wrap").css("top",0);
	else
		$("#f_wrap").css("top",heightWin/2 - heightEl/2 - 30);
	$("#f_overlay").css("display","block");
	$("#f_wrap").fadeIn(300);
	return false;
}

prevOpen = -1;
function showCompanyInfo(idx){
	if (idx != prevOpen){
		$("#company_"+idx).css("display","block").prev().find("label").addClass("act");
		$("#company_"+prevOpen).css("display","none").prev().find("label").removeClass("act");
	}
	prevOpen = idx;
}


function historyItem(e){
	if (e.parents("div.item").hasClass("act"))
		e.parents("div.item").find("div.tbl_history").slideUp(300, function(){e.parents("div.item").removeClass("act").css("background-color","#e8e5de");});
	else
		e.parents("div.item").css("background-color","#cdc9be").find("div.tbl_history").slideDown(300, function(){e.parents("div.item").addClass("act");});
	return false;
}

function billsItem(e){
	if (e.parents("div.item").hasClass("act"))
		e.parents("div.item").find("div.others_fields").slideUp(300, function(){e.parents("div.item").removeClass("act");});
	else
		e.parents("div.item").find("div.others_fields").slideDown(300, function(){e.parents("div.item").addClass("act");});
	return false;
}
