var currentSelection = 0;
var currentUrl = '';

google.setOnLoadCallback(function()
{
	// Register keypress events on the whole document
	$(document).keydown(function(e) {
		switch(e.keyCode) {
			// User pressed "up" arrow
			case 38:
				navigate('up',e);
			break;
			// User pressed "down" arrow
			case 40:
				navigate('down',e);
			break;
			// User pressed "enter"
			case 13:
				if(currentUrl != '') {
					window.location = currentUrl;
				}
			break;
		}
	});

	// Add data to let the hover know which index they have
	for(var i = 0; i < $("#loadingSearchBlock a").size(); i++) {
		$("#loadingSearchBlock a").eq(i).data("number", i);
	}

	// Simulote the "hover" effect with the mouse
	$("#loadingSearchBlock tr").hover(
		function () {
			currentSelection = $(this).data("number");
			setSelected(currentSelection);
		}, function() {
			$("#loadingSearchBlock tr").removeClass("itemhover");
			currentUrl = '';
		}
	);
});

function navigate(direction,e) {
	// Check if any of the menu items is selected
	if($("#loadingSearchBlock  .itemhover").size() == 0) {
		currentSelection = -1;
	}

	if(direction == 'up' && currentSelection != -1) {
		if(currentSelection != 0) {
			currentSelection--;
		}
	} else if (direction == 'down') {
		if(currentSelection != $("#loadingSearchBlock a").size() -1) {
			currentSelection++;
		}
	}
	setSelected(currentSelection,e);
}

function setSelected(menuitem,e) {
	$("#loadingSearchBlock  tr").removeClass("itemhover");
	$("#loadingSearchBlock  a").eq(menuitem).focus();
	$("#loadingSearchBlock  tr").eq(menuitem).addClass("itemhover");
	//nur an wenn scroll box
	//$('#loadingSearchBlock').scrollTo( $("#loadingSearchBlock  a").eq(menuitem), 10, null );
	currentUrl = $("#loadingSearchBlock  a").eq(menuitem).attr("href");
	e.preventDefault();
}
