var price = new Object();
var request = new Object();
var active_reload_element = "0";

function wait_redirect(url, text)
{
	$('body').append ("<div id='wait' class='overbody'></div>");
	$("#wait").width(screen.width);
	$("#wait").height(screen.height);
	$("#wait").append("<h2>" + text + "</h2>");

	$("#wait > h2").css("margin-top", Math.round ($(window).height()  / 2 ) - 20);
	$("#wait").show();
	
	location.replace(url);
}

function history_back(elem)
{
	if (history.length > 1)
	{
		history.back();
	}
	else
	{
		 $(elem).parent().submit();
	}

	return false;
}

function update_table_class()
{
	if ($("table.search-result > tbody > tr:first").attr("class") != "")
	{
		var class_name = "";
		var ind = 1;

		$("table.search-result > tbody > tr").each(function()
		{
			class_name = $(this).attr("class");
			$(this).removeClass();
			
			if ((ind % 2) != 0)
				$(this).addClass("w " + class_name);
			else
				$(this).addClass("b " + class_name);

			ind++;
		});
	}
	else
	{
		$("table.search-result > tbody > tr:odd").addClass("b");
		$("table.search-result > tbody > tr:even").addClass("w");
	}
}

// обработчик событий для set-form
function bind_hover_for_set_form()
{
	$(".set-form:visible").hover(
		function()
		{
			 $(document).unbind("click");
		},
		function() 
		{	
			if ($(".set-form:visible").length)
			{
				$(document).unbind("click");
				$(document).click(function(event){ 
					
					var id = $(event.target).attr("id");
					if (!id) 
					{
						$(event.target).attr("id","clicked");
						id = "clicked";
					};
					if (!$("div.calendar").find("#"+ id).length && !$("div.set-form").find("#"+ id).length && $("#"+ id).attr("class") != "edit" && $("#"+ id).attr("class") != "set" && $("#"+ id).attr("class") != "delete")
					{
	 					$(".set-form").hide();
	 					$(document).unbind("click");
					};
					if (id == "clicked")
						$(event.target).removeAttr("id");;
				});
			};
		}
	);
};

//обработчик нажатия клавиш на документе
function KeyDown(e)
{	
	if (e.which == 27)
	{
		$(".search-result").find("ul.menu").hide();
		$(".set-form").hide();
		$(".div-help").hide();
		$(document).unbind("click");
	};
}

function LoadCss(css_href)
{
	var css = document.createElement('link')
	css.rel = "stylesheet"
	css.href = css_href;
	css.type = "text/css"
	css.media = "all"
	document.getElementsByTagName('head')[0].appendChild(css);
}

$(document).ready(function(){

	$("li.top").each(function()
	{
		if ($(this).children().length == 0)
			$(this).remove();
	});

	$(".set-form").find("input").each(function()
	{	
		$(this).keydown(function(e)
		{
			if (e.which == 13)
			{
				$(".set-form").hide();
				$(this).parent().parent().find("li:last > input[type='button']").click();
			};
		});
		$(this).hover(function()
		{	
			$(this).focus();
		},function()
		{	
		});
	});

	$(".set-form").find("input[type='button']").mouseup(function()
	{	
		$(".set-form").hide();
		$(".set-form").unbind("hover");
		$(document).unbind("click");
	});

	$(".set-form").find("textarea").hover(
		function()
		{	
			$(this).focus();
		},
		function()
		{
		}
	);

	$(document).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions)
	{	
		$("li.top").each(function()
		{
			if ($(this).children().length == 0)
				$(this).remove();
		});

		if (ajaxOptions['type'] == 'POST')
		{
			switch (ajaxEventComplete)
			{	
				case 4:
				case 31:
				case 36:
					$(".set-form").find("input").each(function()
					{	
						$(this).keydown(function(e)
						{
							if (e.which == 13)
								$(this).parent().parent().find("li.foot > input[type='button']").click();
						});
						$(this).hover(function()	
						{	
							$(this).focus();
						},function()
						{
						});
					});
					$(".set-form").find("textarea").each(function()
					{	
						$(this).hover(function()
						{	
							$(this).focus();
						},function()
						{
						});
					});
				case 58:
				case 59:
					$(".set-form").unbind("hover");
					$(document).unbind("click");
					$(".set-form").find("input[type='button']").mouseup(function()
					{
						$(".set-form").unbind("hover");
						$(document).unbind("click");
						$(".set-form").hide();
					});
				break;
				case 80:
					$("ul.alfabetic > li > a.selected ").removeClass();
					$("ul.alfabetic > li:first > a").addClass('selected');
				case 2:
				case 54:
					update_table_class();
					
					$(".search-result").find("ul.menu").hover(
						function(){},
						function()
						{
							$(document).click(function(event)
							{	
								var id = $(event.target).attr("id");
								if (!id) 
								{
									$(event.target).attr("id","clicked");
									id = "clicked";
								};
								if ($("#"+ id).attr("class") != "menu" && $("#"+ id).attr("class") != "favorite-add" && $("#"+ id).attr("class") != "favorite-del")
								{
	 								$(".search-result").find("ul.menu").hide();
					 				$(document).unbind("click");
								};
								if (id == "clicked")
									$(event.target).removeAttr("id");;
							});
						}
					);

					FindWord();
					var count  = $("#count").val();
					if (count > 0)
						$("#next").show();
					else
						$("#next").hide();
				break;
				case 16:
					$("ul.alfabetic > li > a.selected ").removeClass();
					$("ul.alfabetic > li:first > a").addClass('selected');
				case 66:
				case 67:
				case 68:
					update_table_class();
					$("ul.submenu").find("a.selected").removeClass();
					$("#"+$("#mode").val()+"").toggleClass("selected");
					FindWord();

					var count  = $("#count").val();
					if (count > 0)
						$("#next").show();
					else
						$("#next").hide();
				break;
				case 10:
				case 7:
					update_table_class();
					$("ul.submenu").find("a.selected").removeClass();
					$("#"+$("#mode").val()+"").toggleClass("selected");
				
					var count = $("input#count").val();
					var t = $("a#in").text();
					$("a#in").text(t.replace(/\(.*\)/g, "(" + count + ")"));
				break;	
			}
		}
		else if (ajaxOptions['type'] == 'GET')
		{
		
		}
	});

	$(document).keydown(KeyDown);

});
