$(document).ready(function(){
	$(document).click(function(){
		$("#ajax_response").fadeOut('slow');
	});
	$("#keyword").focus();
	var offset = $("#keyword").offset();
	var width = $("#keyword").width()-2;
	$("#ajax_response").css("left",offset.left); 
	$("#ajax_response").css("width",width);
	$("#keyword").keyup(function(event){
		 //alert(event.keyCode);
		 var keyword = $("#keyword").val();
		 if(keyword.length)
		 {
			 if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
			 {
				$("#ajax_response").fadeIn('slow');
				 $("#loading").css("visibility","visible");
				 $.ajax({
				   type: "POST",
				   url: "tpl_ajax_server_db_con.php",
				   data: "data="+keyword,
				   success: function(msg){	
					if(msg != 0)
					  $("#ajax_response").fadeIn("slow").html(msg);
					else
					{
					  $("#ajax_response").fadeOut('fast');
					  $("#ajax_response").html('');
					}
					$("#loading").css("visibility","hidden");
				   }
				 });
			 }
			 else
			 {
				switch (event.keyCode)
				{
				 case 40:
				 {
						
					  found = 0;
					  $("#ajax_response li").each(function(){
						 if($(this).attr("class") == "selected")
							found = 1;
					  });
					  if(found == 1)
					  {
						var sel = $("#ajax_response li[class='selected']");
						sel.next().addClass("selected");
						sel.removeClass("selected");
					  }
					  else
					  {
						$("#ajax_response li:first").addClass("selected");
					  }
					 }
				 break;
				 case 38:
				 {
					  found = 0;
					  $("#ajax_response li").each(function(){
						 if($(this).attr("class") == "selected")
							found = 1;
					  });
					  if(found == 1)
					  {
						var sel = $("#ajax_response li[class='selected']");
						sel.prev().addClass("selected");
						sel.removeClass("selected");
					  }
					  else
						$("#ajax_response li:last").addClass("selected");
				 }
				 break;
				case 13:
				{
					$("#pageloading").css("visibility","visible");
					var kwrdtmp = document.getElementById('keyword').value;
					$("#ajax_response").fadeOut("slow");
					$("#keyword").val($("#ajax_response li[class='selected'] a").text());
					var kwrd = document.getElementById('keyword').value;
					if(kwrd != "")
					{
						document.location.href="http://www.accessorypower.com/index.php?main_page=new_suggest_search_result&keyword=" + kwrd;
					}
					else if (kwrdtmp != "")
					{
						document.location.href="http://www.accessorypower.com/index.php?main_page=new_suggest_search_result&keyword=" + kwrdtmp;
					}
					setTimeout(function(){
						$("#loadingmsg").css("visibility","visible");
					},4000);
				}
				 break;
				}
			 }
		 }
		 else
			$("#ajax_response").fadeOut("slow");
	});
	$("#ajax_response").mouseover(function(){
		$(this).find("li a:first-child").mouseover(function () {
			  $(this).addClass("selected");
		});
		$(this).find("li a:first-child").mouseout(function () {
			  $(this).removeClass("selected");
		});
		$(this).find("li a:first-child").click(function () {
			  $("#keyword").val($(this).text());
			  $("#ajax_response").fadeOut("slow");
		});
	});
	$("#searchbtn2").click(function(){
		$("#pageloading").css("visibility","visible");
			setTimeout(function(){
				$("#loadingmsg").css("visibility","visible");
				//$("#pageloading h1").append("<strong>.</strong>");
		},4000);
	});
});