$(document).ready(function() {
	var url = (document.location + "").split("/");
	var base_url = url[0] + "//" + url[2] + "/";

	if(url[3] == "show" || url[3] == "shownew")
	{
		var  where = document.referrer;
		if(where == 0)
		{
			$("#back_link").html("");
		}
		else
		{
			if(where != base_url + "search/")
			{
				$("#back_link").html("<a href="+where+" class='link' title='Вернутся на предыдущую страницу'><b><<< Назад</b></a>");
			}
		}
	}
	
	$("span.menu_text").hover(function() {
		$("div.sub_menu_box").hide();
		$(this).css("color", "#ff6600");
		$(this).css("background-position", "5px -29px");
		var id = $(this).attr("id");
		if($("*").index($("#" + id + "_box")[0]) != -1)
		{
			$("#" + id + "_box").show();
			if($("#" + id + "_box").width() < $(this).width())
			{
				$("#" + id + "_box").width($(this).width());
			}
		}
	}, function() {
		$(this).css("color", "#ffffff");
		$(this).css("background-position", "5px -2px");
		var id = $(this).attr("id");
		if($("*").index($("#" + id + "_box")[0]) != -1)
		{
			$("#" + id + "_box").hide();
		}
	});
	
	$("div.sub_menu_box").hover(function() {
		$(this).show();
	}, function() {
		$(this).hide();
	});
	
	$("#exit").click(function() {
		$.post(base_url + "pub/exitAdmin/", {}, exit_result);
	});
	
	function exit_result(data)
	{
		window.location.reload();
	}
	
	$("span.orange_prod").hover(function() {
		$(this).css("color", "#000000");
		$(this).css("text-decoration", "none");
	}, function() {
		$(this).css("color", "#ff6600");
		$(this).css("text-decoration", "underline");
	});
	
	$("span.span_link").hover(function() {
		$(this).css("color", "#000000");
		$(this).css("text-decoration", "none");
	}, function() {
		$(this).css("color", "#0000ff");
		$(this).css("text-decoration", "underline");
	});
	
	$("span.orange_prod").click(function() {
		var id = $(this).attr("id") + "_box";
		click_prod(id);
	});
	
	$("div.prod_box").click(function() {
		var id = $(this).attr("id");
		click_prod(id);
	});
	
	function segment(n)
	{
		var url = (document.location + "").split("/");
		return  (n > 0) ? url[n + 2] : "error";
		
	}
	
	if(segment(1) == "products" && segment(2) != "")
	{
		click_prod("prod_" + segment(2) + "_box");
	}
	
	function click_prod(id)
	{
		$("div.cutter").each(function() {
			if($(this).css("display") != "none")
			{
				$(this).animate({"height": 150}, "slow");
			}
		});
		
		if($("#" + id).parent("div.cutter").height() <= 200)
		{
			$("#" + id).parent("div.cutter").animate({"height": $("#" + id).height()}, "slow");
		}
		else
		{
			$("#" + id).parent("div.cutter").animate({"height": 150}, "slow");
		}
	}
	
	$("td.num_page").hover(function() {
		var id = $(this).attr("id").split("_");
		if($("#page_" + id[1]).css("display") == "none")
		{
			$(this).css("background", "#ff6600");
		}
	}, function() {
		var id = $(this).attr("id").split("_");
		if($("#page_" + id[1]).css("display") == "none")
		{
			$(this).css("background", "#ffffff");
		}
	});
	
	$("#search").keypress(function(key) {
		if(key.keyCode == 13)
		{
			$("#search_button").click();
		}
	});
	
	$("#search_button").click(function() {
		if($.trim($("#search").val()) != "")
		{
			$("#search_form").submit();
		}
	});
});

function page_click(id)
{
	var page_id = "";
	$("div.lit_page").each(function() {
		if($(this).css("display") != "none")
		{
			page_id = $(this).attr("id");
		}
	});
	var part_id = page_id.split("_");
	var td_id = id.split("_");
	if(part_id[1] != td_id[1])
	{
		$("#page_" + part_id[1]).animate({"height": "hide"}, "slow");
		$("#numpage_" + part_id[1]).css("background", "#ffffff");
		$("#numpage_" + part_id[1]).css("cursor", "pointer");
		
		var this_id = id.split("_");
		$("#page_" + this_id[1]).animate({"height": "show"}, "slow");
		$("#" + id).css("background", "#ff6600");
		$("#" + id).css("cursor", "default");
		
		$("td.num_page").hover(function() {
			var id = $(this).attr("id").split("_");
			if($("#page_" + id).css("display") == "none")
			{
				$(this).css("background", "#ff6600");
			}
		}, function() {
			var id = $(this).attr("id").split("_");
			if($("#page_" + id).css("display") == "none")
			{
				$(this).css("background", "#ffffff");
			}
		});
	}
}
