var xmlhttp;
var currentSelection=0;
var userSearch="";
 
function showSuggestion(e) {
	if((e.keyCode >= 37 && e.keyCode <= 40) || e.keyCode == 13) {
		return;
	}
	if(e.keyCode == 27) {
		document.getElementById("searchSuggestion").style.display="none";
		return;
	}
	str = document.getElementById("searchbar").value;
	userSearch=str;
	currentSelection=0
	if (str.length==0) {
		document.getElementById("searchSuggestion").innerHTML="";
		document.getElementById("searchSuggestion").style.display="none";
		return;
	}
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url="noindex/getSearchSuggestion.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		var response = xmlhttp.responseText;
		document.getElementById("searchSuggestion").innerHTML=xmlhttp.responseText;
		if (response == "") {
			document.getElementById("searchSuggestion").style.display="none";
		} else {
			document.getElementById("searchSuggestion").style.display="block";
		}
	}
}

function hideSuggestionDiv() {
	document.getElementById("searchSuggestion").style.display="none";
}
function showSuggestionDiv() {
	document.getElementById("searchSuggestion").style.display="block";
}

function setSearchQuery(str) {
	document.getElementById("searchbar").value = str;
	userSearch=str;
	document.getElementById("searchSuggestion").style.display="none";
}

function setListenersMS() {
	document.getElementById("searchbar").attachEvent('onkeyup',showSuggestion);
	document.getElementById("searchbar").attachEvent('onblur',hideSuggestionDiv);
	document.getElementById("searchSuggestion").attachEvent('onmouseover',function () {document.getElementById("searchbar").detachEvent('onblur',hideSuggestionDiv);});
	document.getElementById("searchSuggestion").attachEvent('onmouseout',function () {document.getElementById("searchbar").attachEvent('onblur',hideSuggestionDiv);});
	document.getElementById("searchbar").attachEvent('onkeydown',scrollThroughSuggestions);
}

function setListeners() {
	document.getElementById("searchbar").addEventListener('keyup',showSuggestion,false);
	document.getElementById("searchbar").addEventListener('blur',hideSuggestionDiv,false);
	//document.getElementById("searchSuggestion").addEventListener('focus',showSuggestionDiv,false);
	document.getElementById("searchSuggestion").addEventListener('mouseover',function () {document.getElementById("searchbar").removeEventListener('blur',hideSuggestionDiv,false);},false);
	document.getElementById("searchSuggestion").addEventListener('mouseout',function () {document.getElementById("searchbar").addEventListener('blur',hideSuggestionDiv,false);},false);
	document.getElementById("searchbar").addEventListener('keydown',scrollThroughSuggestions,false);
}

function scrollThroughSuggestions(e) {
	//keyCode 13 is Enter key
	if(e.keyCode == 13) {
		if(document.getElementById("searchSuggestion").style.display!="none" && currentSelection>0) {
			setSearchQuery(suggestions[currentSelection-1].innerHTML);
		}
	}
	//keyCode 38 is Up key, keyCode 40 is Down key
	if(e.keyCode == 38 || e.keyCode == 40) {
		suggestions = document.getElementById("searchSuggestion").getElementsByTagName("A");
		if(document.getElementById("searchSuggestion").style.display=="none" && suggestions.length>0) { document.getElementById("searchSuggestion").style.display="block"; return; }
		if(currentSelection>0) { suggestions[currentSelection-1].style.backgroundColor=""; }
		if(e.keyCode == 38) { 
			currentSelection = (currentSelection-1)%(suggestions.length+1); 
			if(currentSelection<0) { currentSelection = suggestions.length; }
		}
		if(e.keyCode == 40) { 
			currentSelection = (currentSelection+1)%(suggestions.length+1); 
		}
		if(currentSelection>0) { suggestions[currentSelection-1].style.backgroundColor="orange"; }
	}
}