/**
 * Searh
 * @version 1.0.3
 */
var search_last_text		= "";
var search_timer_id		= 0;
var search_update_timeout	= 100;
var hasCursor			= false;
var cursorLocation		= "";
var cursorIndex			= -1;

function search_init()
{
	if (!$("#search_form").length)
		return;

	$("#search_text").attr("autocomplete", "off");
	$("#search_form_close_link A").bind("click", search_form_hide);

	$("#search_text").keyup(search_key_handler);
	$("#search_text").keydown(search_key_enter_handler);

	$("#search_elements a").live("mouseenter", search_mouse_handler);

	search_timer_start();
}

function search_mouse_handler(event)
{
	cursorIndex = $("#search_elements a").index(this);
	cursorLocation = this.href;
	$("#search_elements a").removeClass("search_hover");
	$(this).addClass("search_hover");
}

function search_key_handler(event)
{
	switch (event.keyCode)
	{
		// arrow down
		case 40:
			search_form_move_down();
			break;

		// arrow up
		case 38:
			search_form_move_up();
			break;

		// escape
		case 27:
			search_form_hide();
		default:
			hasCursor = false;
			cursorIndex = -1;
			cursorLocation = "";
			break;
	}
}

function search_get_links()
{
	var elements = $("#search_elements a");
	$(elements[cursorIndex]).removeClass("search_hover");
	return elements;
}

function search_move(current_element)
{
	hasCursor = true;
	cursorLocation = current_element[0].href;
	current_element.addClass("search_hover");
}

function search_form_move_down()
{
	var elements = search_get_links();

	if (hasCursor)
		cursorIndex++;

	if (!hasCursor || cursorIndex > elements.length - 1)
		cursorIndex = 0;

	search_move($(elements[cursorIndex]));
}

function search_form_move_up()
{
	var elements = search_get_links();

	if (hasCursor)
		cursorIndex--;

	if (!hasCursor || cursorIndex < 0)
		cursorIndex = elements.length - 1;

	search_move($(elements[cursorIndex]));
}

function search_key_enter_handler(event)
{
	var hasCursorAndEnter = event.keyCode == 13 && hasCursor;

	if (hasCursorAndEnter)
		location.href = cursorLocation;

	return !hasCursorAndEnter;
}

function search_set_text(result)
{
	$("#search_text").val(result);

	search_last_text = result.toLowerCase();

	search_form_hide();
}

function search_timer_start()
{
	search_timer_id = setTimeout("search_process()", search_update_timeout);
}

function search_check(data)
{
	$("#search_elements").empty();

	var items = $("item", data);
	if (items.length == 0)
	{
		search_form_hide();
		return;
	}

	for (var i = 0; i < items.length; i++)
		$("#search_elements").append(items.eq(i).text());

	search_form_update();
}

function search_get(text)
{
	$.get("/search_ajax/", {'text': text}, search_check, "xml");
}

function search_process()
{
	var text = $("#search_text").val().toLowerCase();

	if (text == "" || text.length < 3)
	{
		search_last_text = text;
		search_timer_start();
		search_form_hide();
		return;
	}

	if (text != search_last_text)
	{
		search_last_text = text;
		search_get(text);
	}

	search_timer_start();
}

function search_form_hide()
{
	$("#search_form").hide();
	return false;
}

function search_form_update()
{
	var search_form = $("#search_form");
	var search_text = $("#search_text");

	search_form.show();
	search_form.css("top", search_text.offset().top + search_text[0].offsetHeight - 1);
	search_form.css("left", search_text.offset().left);
}

/**
 * Search options
 * @version 1.0.2
 */
function search_options_init()
{
	$("input:radio[name='where']").bind("click", search_options_show);
	$("#search_options_toggle").bind("click", search_options_toggle);
}

function search_options_show()
{
	var type = $(this).val();

	search_options_hide();

	if ($("#search_options").attr("extended") != "on")
		return;

	$("#" + type + "_options").show();
}

function search_options_hide()
{
	$("#soft_options, #news_options, #games_options, #blogs_options, #comm_options").hide();
}

function search_options_toggle()
{
	var search_options = $("#search_options");

	if (search_options.attr("extended") != "on")
	{
		search_options.attr("extended", "on");
		$("#search_options_more").hide();
		$("#search_options_less").show();
	}
	else
	{
		search_options.attr("extended", "");
		$("#search_options_more").show();
		$("#search_options_less").hide();
	}

	$("input:radio[name='where']:checked").click();

	return false;
}

$(search_init);
