var xhr = null;
var isSearchSubmit = 0;
$(document).ready(function() {
	var keyCode = 0;
	var keyIndex = -1;
	$('#txtKeyword').attr("autocomplete","off");
	$("#txtKeyword").focus();

	$('#txtKeyword').keydown(function(e) {
		keyCode = getKeyCode(e);
		if(keyCode == 38) {	// DOWN ARROW KEY
			listLength = $('#autoSuggestList li').length;
			if(keyIndex == -1) {
				keyIndex = listLength - 1;
				$('#autoList'+keyIndex).attr('className','addHighlight');
			}else {
				if(keyIndex > 0) {
					$('#autoList'+keyIndex).attr('className','');
					keyIndex--;
					$('#autoList'+keyIndex).attr('className','addHighlight');
				}
			}

		}else if(keyCode == 40) {		// UP ARROW KEY
			listLength = $('#autoSuggestList li').length;

			if(keyIndex < (listLength-1)) {
				if(keyIndex >= 0) {
					$('#autoList'+keyIndex).attr('className','');
				}
				keyIndex++;
				$('#autoList'+keyIndex).attr('className','addHighlight');
			}
		}else if(keyCode == 13) {		// ENTER KEY
				if(keyIndex != -1) {
					fill($('#autoList'+keyIndex).attr('innerHTML'));
					$('#suggestionList').hide();
					$('#suggestionSearchList').hide();
					keyIndex = -1;
					return false;
				}
		}else if(keyCode == 27){	// FOR ESC KEY
			keyIndex = -1;
			$('#suggestionList').hide();
			$('#suggestionSearchList').hide();
		}else if(keyCode == 9){		//For Tab key
			keyIndex = -1;
			$('#suggestionList').hide();
			$('#suggestionSearchList').hide();
		}else {
			keyIndex = -1;
		}
	})

	addEvent(document, 'click', function (){
		$('#suggestionList').hide();
		$('#suggestionSearchList').hide();
	})


});
function getKeyCode(keyStroke){
	if (document.all)
		return event.keyCode;
	else if (0 != keyStroke.which)
		return keyStroke.which;
	else
		return keyStroke.keyCode;
}

function lookup(e,txtKeyword) {
	txtKeyword = jQuery.trim(txtKeyword);
	if(txtKeyword.length == 0) {
		$('#txtKeyword').attr("value",txtKeyword);
		// Hide the suggestion box.
		$('#suggestionList').hide();
	} else {
		keyCode = getKeyCode(e);
		if(keyCode == 40 || keyCode == 38 || keyCode == 13 || keyCode == 27) {
			keyCode = null;
			return false;
		}
		if(txtKeyword.length >= 3) {
//			$.post(siteURL+"index.php?action=autosuggest", {queryString: ""+txtKeyword+""}, function(data){
//				if(data.length >0 && stopFlag == 0) {
//					$('#suggestionList').show();
//					$('#autoSuggestionsList').html(data);
//				}else{$('#suggestionList').hide();}
//			});

			if(xhr)
				xhr.abort();

		 	xhr=$.ajax({
				type: "POST",
		   		url: siteURL+"index.php?action=autosuggest",
		   		data: "queryString="+txtKeyword,
		   		success: function(data){
	     			if(data.length >0 ) {
						$('#suggestionList').show();
						$('#autoSuggestionsList').html(data);
					}else{$('#suggestionList').hide();}
   				}
			});
		}
	}
} // lookup
function fill(thisValue){
	if(keyCode == 40 || keyCode == 38) {
		keyCode = null;
		return false;
	}
	$('#txtKeyword').val(thisValue);
	setTimeout("$('#suggestionList').hide();", 200);
	setTimeout("$('#suggestionSearchList').hide();", 20);
}

function searchLookup(e,txtKeyword) {
//	alert("lookup");
	txtKeyword = jQuery.trim(txtKeyword);
	if(txtKeyword.length == 0) {
		$('#txtKeyword').attr("value",txtKeyword);
		// Hide the suggestion box.
		$('#suggestionSearchList').hide();
	} else {
		keyCode = getKeyCode(e);
		if(keyCode == 40 || keyCode == 38 || keyCode == 13 || keyCode == 27) {
			keyCode = null;
			return false;
		}
		if(txtKeyword.length >= 3) {
//			$.post(siteURL+"index.php?action=autosuggest", {queryString: ""+txtKeyword+""}, function(data){
//				if(data.length >0) {
//					$('#suggestionSearchList').show();
//					$('#autoSuggestionsList').html(data);
//				}else{$('#suggestionSearchList').hide();}
//			});

			if(xhr)
				xhr.abort();

		 	xhr=$.ajax({
				type: "POST",
		   		url: siteURL+"index.php?action=autosuggest",
		   		data: "queryString="+txtKeyword,
		   		success: function(data){
	     			if(data.length >0 ) {
						$('#suggestionSearchList').show();
						$('#autoSuggestionsList').html(data);
					}else{$('#suggestionSearchList').hide();}
   				}
			});
		}
	}
} // lookup

function formSubmit() {
	if(xhr)
		xhr.abort();
	if(isSearchSubmit ==1){
		return false;
	}else{
		isSearchSubmit = 1;
		$('#frmSearch').submit();
		return false;
	}
}

function showHideSearch(advanced) {
	if(advanced == 1) {
		$("#simpleSearchLink").hide();
		$("#advancedSearch").show('slow');
		$("#action").attr("value","advancedSearchFilter");
	}else {
		$("#simpleSearchLink").show();
		$("#advancedSearch").hide('slow');
		$("#action").attr("value","search");
	}
}

function changeHidValue(obj){
	val = obj.options[obj.selectedIndex].text;
	$('#hidStarSign').attr('value',val);
}

