$(document).ready(function(){
function jsuggest(event)  {
   var inputString=$(event.target).val();
   if(inputString.length == 0) {
      $('#suggestions').fadeOut(); // Hide the suggestions box
   } else {
      $.get("acs.php", {s: ""+inputString+""}, function(data) { // Do an AJAX call
         $('#suggestions').fadeIn(); // Show the suggestions box
         $('#suggestions').html(data); // Fill the suggestions box
	 $('#suggestions li').mousemove(function() {
		$(".jsg-focus").removeClass("jsg-focus");
		$(this).addClass("jsg-focus");
		});
      });
   }
}
function suggest_hdlr(event) {
	var sugg=$('#suggestions');
	var currfoc=$("li.jsg-focus",sugg);
	switch(event.keyCode)
	{
	case 13:
	event.preventDefault();
	window.location=$("a.jsg-enterlink",currfoc).attr("href");
	break;
	case 27:
	
	sugg.fadeOut();
	break;
	case 38: //up
	event.preventDefault();
	if(!$(".jsg-first").hasClass("jsg-focus"))
	currfoc.removeClass("jsg-focus").prev().addClass("jsg-focus");	
	break;
	case 40: //dn
	event.preventDefault();
	if(sugg.find("ul li:last").hasClass("jsg-focus"))
	{
	currfoc.removeClass('jsg-focus');
	$(".jsg-first").addClass("jsg-focus");
	}
	else
	currfoc.removeClass("jsg-focus").next().addClass("jsg-focus");	
	break;
	default:
	jsuggest(event);
	
	
	}
	return false;
}
$("#query").keyup(suggest_hdlr).focus(jsuggest).blur(function()
	{
	$('#suggestions').fadeOut();
	});



});
