$(document).ready(function(){
	 $("input").blur(function(){
		// $('#searchresults').empty();
	 	$('#suggestions').fadeOut();
	});
});

var xhr;
function lookup() {
	var queryString =  $('#inputString').val();
	var storeId = $("#new_search_wrapper").attr('rel');
	$('#inputString').attr('value').replace(/\u000d\u000a/g,'\u000a').replace(/\u000a/g,'\u000d\u000a');
	var currentLength = $('#inputString').val().length;
	//alert(currentLength);
	if(currentLength < 1) {
		$('#suggestions').hide(); // Hide the suggestions box
	} else if (currentLength >= 1) {
		//alert('length is good');
		if(xhr !== undefined) { 
			//xhr.abort();
		} 
			xhr = $.getJSON("/template/auto_comp/rpc.php?queryString="+queryString+"&rs="+storeId+"&surl="+searchUrl, function(data) {
					var innerDIv = '';
					var x = 0;
				
				$(data.prods).each(function(i) { 
						if( x <= 10)
						{
							var pname = data.prods[i].name;
							//var pimg = data.prods[i].img;
							//var ptag = data.prods[i].tagline;
							var purl = data.prods[i].url;
							//innerDIv = innerDIv + "<a class='searchresults_link' href='"+purl+"' rel='"+x+"' id='"+x+"'><span class='ac_img'><img src='"+pimg+"' /></span><span class='searchheading'>"+pname+"</span><span>"+ptag+"</span></a>";
							innerDIv = innerDIv + "<a class='searchresults_link' href='"+purl+"' rel='"+x+"' id='"+x+"'><span class='searchheading'>"+pname+"</span></a>";
							x++;
						}
			  });
				innerDIv = innerDIv + "<a class='searchresults_link' href='"+searchUrl+"?gsearch="+queryString+"' id='"+x+"'><span class='searchheading'><i><u>"+searchText+" <b>'"+queryString+"'</b></u></i></span></a>";
				$('#searchresults').html(innerDIv);
				$('#suggestions').show();
			});
	}
}

	var timeout;

  $(document).ready(function(){
		$(document).unbind('keypress'); 
		$('#inputString').keydown(function(e){
	// Register keypress events on the whole document

		  var keyCode = e.keyCode || e.which,
			  arrow = {up: 38, down: 40, enter: 13 };

		  switch (keyCode) {
			case arrow.up:
				//alert('up');
				navigate('up');
			break;
			case arrow.down:
				//alert('down');
				navigate('down');
			break;
			case arrow.enter:
				if(currentUrl != '') 
				

				{
					//$('#searchform').bind('submit',function() {
					window.location = currentUrl;
					return false;
					//});
				}
			break;
			default:
				//$('#searchresults').empty();
					if(timeout) {
					clearTimeout(timeout);
					timeout = null;
					}
				timeout = setTimeout(lookup, 0);
			break;
		  }

	// Add data to let the hover know which index they have
	for(var i = 0; i < $(".searchresults_link").size(); i++) {
		$(".searchresults_link").eq(i).data("number", i);
	}
	
	// Simulote the "hover" effect with the mouse
	$(".searchresults_link").hover(
		function () {
			currentSelection = $(this).data("number");
			setSelected(currentSelection);
		}, function() {
			$(".searchresults_link").removeClass("itemhover");
			currentUrl = '';
			$('#searchform').attr('action', currentUrl);

		}
	)
		
});

function navigate(direction) {
	// Check if any of the menu items is selected
	if($("#suggestions .itemhover").size() == 0) {
		currentSelection = -1;
	}
	
	if(direction == 'up' && currentSelection != -1) {
		if(currentSelection != 0) {
			currentSelection--;
		}
	} else if (direction == 'down') {
		if(currentSelection != $(".searchresults_link").size() -1) {
			currentSelection++;
		}
	}
	setSelected(currentSelection);
}

function setSelected(menuitem) {
	$(".searchresults_link").removeClass("itemhover");
	$(".searchresults_link").eq(menuitem).addClass("itemhover");
	currentUrl = $(".searchresults_link").eq(menuitem).attr("href");
	$('#searchform').attr('action',currentUrl);
}

  });

