/**
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
	// Safely inject CSS3 and give the search results a shadow
	var cssObj = { 'box-shadow' : '#888 5px 10px 10px', // Added when CSS3 is standard
		'-webkit-box-shadow' : '#888 5px 10px 10px', // Safari
		'-moz-box-shadow' : '#888 5px 10px 10px'}; // Firefox 3.5+
	$("#suggestions").css(cssObj);
	
	// Fade out the suggestions box when not active
	 $("input").blur(function(){
	 	$('#suggestions').fadeOut();
	 });
});
**/
var currentSelection = 0;
var currentBookcoach = '';
var oldInputString = '';

function lookup(inputString) {
	if (inputString != oldInputString)
	{
		if(inputString.length == 0) {
			$('#suggestions').fadeOut(); // Hide the suggestions box
			oldInputString = '';
		} else {
			$.get("rpc_suggestBookcoach.php", {queryString: ""+inputString+""}, function(data) { // Do an AJAX call
				$('#suggestions').fadeIn(); // Show the suggestions box
				$('#suggestions').html(data); // Fill the suggestions box

				// Add data to let the hover know which index they have
				for(var i = 0; i < $("#suggestions ul li a").size(); i++) {
					 $("#suggestions ul li a").eq(i).data("number", i);
				}

				// Simulote the "hover" effect with the mouse
				$("#suggestions ul li a").hover(
					function () {
						currentSelection = $(this).data("number");
						setSelected(currentSelection);
					}, function() {
						$("#suggestions ul li a").removeClass("itemhover");
						currentBookcoach = '';
					}
				);
			});
			oldInputString = inputString;
		}
	}
}

if ($.browser.msie)
{
	$(document).keyup(function(e) {
		switch(e.keyCode) { 
			 // User pressed "up" arrow
			 case 38:
					navigate('up');
			 break;
			 // User pressed "down" arrow
			 case 40:
					navigate('down');
			 break;
			 // User pressed "enter"
			 case 13:
					if (currentBookcoach != 0)
					{
						e.preventDefault();
						if (currentBookcoach != '')
						{
							$('#inputString').val(currentBookcoach);
							document.quickSearchForm.submit();
							// window.location = currentBookcoach;
						}
					}
					else
					{
						document.quickSearchForm.submit();
					}
			 break;
		}
	});
}
else
{
	$(document).keypress(function(e) {
		switch(e.keyCode) { 
			 // User pressed "up" arrow
			 case 38:
					navigate('up');
			 break;
			 // User pressed "down" arrow
			 case 40:
					navigate('down');
			 break;
			 // User pressed "enter"
			 case 13:
					if (currentBookcoach != 0)
					{
						e.preventDefault();
						if (currentBookcoach != '')
						{
							$('#inputString').val(currentBookcoach);
							document.quickSearchForm.submit();
							// window.location = currentBookcoach;
						}
					}
					else
					{
						document.quickSearchForm.submit();
					}
			 break;
		}
	});
}

function navigate(direction) {
   // Check if any of the menu items is selected
   if($("#suggestions ul li .itemhover").size() == 0) {
      currentSelection = -1;
   }
   
   if(direction == 'up' && currentSelection != -1) {
      if(currentSelection != 0) {
         currentSelection--;
      }
   } else if (direction == 'down') {
      if(currentSelection != $("#suggestions ul li").size() -1) {
         currentSelection++;
      }
   }
   setSelected(currentSelection);
}


function setSelected(menuitem) {
   $("#suggestions ul li a").removeClass("itemhover");
   $("#suggestions ul li a").eq(menuitem).addClass("itemhover");
   currentBookcoach = $("#suggestions ul li a").eq(menuitem).attr("value");
}

