	var lastText		= "";
	var timerId		= 0;
	var searchUpdateTimeout	= 100;

	// Searches form functions

	function createSearchForm()
	{
		if ($("#searchFormId").length)
			return;

		$("body").append('\
<div id="searchFormId" style="display: none; position: absolute; z-index: 10; width: 408px; border-bottom: 1px dotted lightgray; border-left: 1px dotted lightgray; background-color: white;">\
	<div id="yellowhiSearch">\
		<span id="searchElementsId"></span>\
	</div>\
	<div style="font-size: 85%; padding: 0px 2px 2px 0px; width: 405px; text-align: right; display: block;background-color: #539aba;"><a href="#" style="color:white" onclick="closeSearchForm(); return false;"><b>закрыть список</b></a></div>\
</div>\
');
	}

	function addQueryHref(query, results)
	{
		var query_escaped = query.replace(new RegExp('"', "g"), '\\"');

		$("#searchElementsId").append('<div style="display: block; width: 400px;height: 12px; padding:4px; color:#8aaf11;border-bottom: 1px dotted lightgray;" onclick=\'setSearchText("' + query_escaped + '"); return false;\' onmouseout="this.style.backgroundColor=\'#FFFFFF\';" onmouseover="this.style.backgroundColor=\'#f7f7f7\';"><a href="#"style="float:left;margin-left:10px;color:black;" onclick=\'setSearchText("' + query_escaped + '"); return false;\'>' + query + '</a><span style="float: right; padding-right: 5px;font-weight:bold;color:lightgray;">' + results + '</span></div>');
	}

	function closeSearchForm()
	{
		clearTimeout(timerId);
		hideSearchForm();
	}

	function hideSearchForm()
	{
		$("#searchFormId").hide();
	}

	function setSearchText(result)
	{
		$("#searchTextId").val(result);

		lastText = result.toLowerCase();

		hideSearchForm();
	}

	function updateSearchForm(show)
	{
		var searchForm = $("#searchFormId");
		var searchText = $("#searchTextId");

		if (show)
			searchForm.show();

		searchForm.css("top", searchText.offset().top + searchText[0].offsetHeight);
		searchForm.css("left", searchText.offset().left+1);
	}

	// Get searches functions

	function initSearches()
	{
		$("#searchTextId").attr("autocomplete", "off");
		startSearchesTimer();
	}

	function startSearchesTimer()
	{
		timerId = setTimeout("processSearches()", searchUpdateTimeout);
	}

	function checkSearches(data, textStatus)
	{
		createSearchForm();

		$("#searchElementsId").empty();

		var answers = $("answer", data);
		if (answers.length == 0)
		{
			hideSearchForm();
			return;
		}

		for (var i = 0; i < answers.length; i++)
		{
			var answer = answers.eq(i);
			addQueryHref($("query", answer).text(), $("results", answer).text());
		}

		updateSearchForm(true);
	}

	function getSearches(input)
	{
		$.get("./header_search.php", {text: input}, checkSearches, "xml");
	}

	function processSearches()
	{
		var text = $("#searchTextId").val().toLowerCase();

		if (text == "" || text.length < 3)
		{
			lastText = text;
			startSearchesTimer();
			hideSearchForm();
			return;
		}

		if (text != lastText)
		{
			lastText = text;
			getSearches(text);
		}

		startSearchesTimer();
	}