function isValidForAutocomplete( e )
{
   //		backspace=8, ctrl=17, alt=18, enter=13, escape=27, space=32, arrow left=37, arrow up=38, arrow down=40
   return 	/*e!=8 &&*/ e!=17 && e!=18 && e!=13 && e!=27 && e!=32 && e!=37 && e!=38 && e!=40;
}

function autocompleteSuggestionsKeys( t, event, loggedSearchString )
{	
	// Key Checks
	switch (event.keyCode)
	{		
		case 27: // escape
			$("#autoSuggestions").hide();
			$("#mainSearchInput").val(loggedSearchString);
			break;
			
		case 38: // up
		
			// check and return logged search terms if pressing up from first list item
			if ( $("#autoSuggestionsList li.hoverOver").is(":first-child") )
			{								
				$("#autoSuggestionsList ul li:first").removeClass("hoverOver");
				$("#mainSearchInput").val(loggedSearchString);				
				break;
			}
			
			// add class to previous and remove other's class
			$("#autoSuggestionsList .hoverOver").prev().addClass("hoverOver").siblings().removeClass("hoverOver");
			
			// checks if current list item is not empty and changes value in input field
			if ( $("#autoSuggestionsList .hoverOver").text() != "" )
			{
				$(t).val( $("#autoSuggestionsList .hoverOver").text() );	
			}						

			break;		
			
		case 39: // right
			$("#autoSuggestions").hide();
			break;
			
		case 40: // down
			//$("#autoSuggestions").show();
			
			if( ! $("#autoSuggestionsList ul li").hasClass("hoverOver") )
			{
				$("#autoSuggestions").show();
				$("#autoSuggestionsList ul li:first").addClass("hoverOver");
			}
			else
			{
				$("#autoSuggestionsList .hoverOver").next().addClass("hoverOver").siblings().removeClass("hoverOver");
			}
			
			if ( $("#autoSuggestionsList .hoverOver").text() != "" )
			{
				$(t).val( $("#autoSuggestionsList .hoverOver").text() );	
			}
			break;
		
		case 13: // enter		
			if (validSearch(document.searchForm, defaultSearchText, validSearch, true))
			{
				$("#autoSuggestions").hide();
				document.searchForm.submit();
			}
			break;
			
		case 53: // 5, looking for % and replace
			$(t).val( $(t).val().replace(/%/g,"") );
			break;
	};	
}

// This Extends JavaScript's String object. Should be moved to common.js
String.prototype.fulltrim = function() {
	// 11-24-2009	Bug #47980	Added '&' so pop-up doesn't disappear
	return this.replace(/\s{2,}/g, " ").replace(/(?:^[\s&]+|[\s&]+$)/g, ""); //inside then left and right trim
};
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, ""); //left and right trim
};

function autocompleteGlobal( t )
{
	var searchValue = $(t).val();
	
	DWRHelper.getSuggestedSearch( searchValue.fulltrim(), function( list )
	{
		if( list.length < 1 ) {
			$("#autoSuggestions").hide();
			return;
		}
		
		var results = '<ul>';
		for(var i=0; i<list.length; i++)
		{
			var searchIndex = list[i].toUpperCase().indexOf(searchValue.toUpperCase());
 			if(searchIndex != -1)
			{
				text = "<b>" + list[i].substring(0,searchIndex) + "</b>" + list[i].substring(searchIndex,searchIndex+searchValue.length) + "<b>" + list[i].substring(searchIndex+searchValue.length) + "</b>";
			}
			else
			{
				text =  "<b>" + list[i] + "</b>";				
			}
			
			results += "<li>" + text + "</li>";
		}
		results += "</ul>";
		
		$("#autoSuggestions").show();
		$("#autoSuggestionsList").html( results );			
	});
}

jQuery(document).ready(function(){

	var loggedSearchString = '';
	var timeout;
	var searchTerm;
	
	$("#mainSearchInput").live('keyup', function(event) {		
		
		if(timeout != null) 
			clearTimeout(timeout);
		
		//If search value is empty hide
		if ($("#mainSearchInput").val() == "")
		{
			$("#autoSuggestions").hide();
		}
		
		// log typed search
		if ( event.keyCode != 38 && event.keyCode != 40 && event.keyCode != 27) // if not up, down, and escape
		{
			loggedSearchString = $("#mainSearchInput").val();		
		}		
		
		// keyboard keys
		autocompleteSuggestionsKeys( this, event, loggedSearchString );
		
		// Valid Keys and Length then send to server
		var regexPattern = /\*|\?|%|;|\\/;
		
		if ( isValidForAutocomplete( event.keyCode ) && this.value.length > 2 && this.value.match(regexPattern)==null )
		{
			searchTerm = this;
			timeout = setTimeout( function() { autocompleteGlobal( searchTerm ) }, 200 );
		}
	});
	
	// on mouse over
	$(".suggestionList ul li").live('mouseover',function(e) {
		$(this).addClass("hoverOver");
		$(this).siblings().removeClass("hoverOver");
	});
	
	// on blur search input
	$('#mainSearchInput').blur(function(){
		//$("#autoSuggestions").fadeOut('25');
		$("#autoSuggestions").hide();
	});
			
	// on list item click submit	
	$("#autoSuggestionsList ul li").live('mousedown', function(){		
		var suggestion = $(this).text();
		$("#mainSearchInput").val(suggestion);
		$("#autoSuggestions").hide();
		document.searchForm.submit();
	});
});