var oldStr = "";
var selectedIndex = -1;
var itemsInList;

var focussedLocation = false;
		
function showHintList(txt)
{
	var hintList = document.getElementById("hintList");
	hintList.style.left = getElementPosition("inpLocation").left;
	hintList.style.top = getElementPosition("hintList").top;
	
	var temp = txt.split("|||||");
	txt = temp[0];
	
	itemsInList = temp[1];
	
	hintList.innerHTML = txt;
		
	if (txt != "")
	{
		hintList.style.visibility = "visible";
	}
	else
	{
		hintList.style.visibility = "hidden";
	}
	
}

function hideHint()
{
	var hintList = document.getElementById("hintList");
	hintList.style.visibility = "hidden";
}

function getElementPosition(elemID)
{
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	
	while (offsetTrail)
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
	{
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	return {left:offsetLeft,top:offsetTop};

}

function onKeyPress(e) 
{
	if (!e) 
	{
		e = event;
	}
	
	// e.keyCode --> 40 is down, 38 is up:
	if (focussedLocation)
	{
	
		var location = document.getElementById('inpLocation');
	
		if (e.keyCode == 40)
		{
			// 'unselect' old selection:
			if (selectedIndex > -1)
			{
				document.getElementById('hint' + selectedIndex).className = "unSelectedHint";
				var selected = document.getElementById('hintlink' + selectedIndex);
				selected.className = "greyLinkNoUnderline";
			}
		
			if (selectedIndex < itemsInList - 1)
			{
				selectedIndex += 1;
			}
			
			// show selected
			document.getElementById('hint' + selectedIndex).className = "selectedHint";
			
			// set value to selected
			var selected = document.getElementById('hintlink' + selectedIndex);
			selected.className = "whiteBoldLinkNoUnderline";
			location.value = selected.name;
			
			// to stop firing off another event
			oldStr = selected.name;
		}
		else if (e.keyCode == 38)
		{
			// 'unselect' old selection:
			if (selectedIndex > -1)
			{
				document.getElementById('hint' + selectedIndex).className = "unSelectedHint";
				var selected = document.getElementById('hintlink' + selectedIndex);
				selected.className = "greyLinkNoUnderline";
			}
			
			if (selectedIndex > 0)
			{
				selectedIndex -= 1;
				document.getElementById('hint' + selectedIndex).className = "selectedHint";
				
				// set value to selected
				var selected = document.getElementById('hintlink' + selectedIndex);
				selected.className = "whiteBoldLinkNoUnderline";
				location.value = selected.name;
				
				// to stop firing off another event
				oldStr = selected.name;
			}
			else
			{
				selectedIndex = -1;
				// refocus to the input box
			}
			
		}
	}
	
}

function onLocationFocus(obj) 
{
	focussedLocation = true;
	if (obj.value == "Search for...")
		obj.value = "";
}

function onLocationBlur() 
{
	focussedLocation = false;
}

function showHint(str)
{
	// don't overwrite the keypresses of up/down on the hints.
	if (oldStr == str)
		return;

	selectedIndex = -1;

	xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		// Browser does not support HTTP Request
		return;
	} 
	
	oldStr = str;
	
	var url="gethint.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		showHintList(xmlHttp.responseText);
	} 
} 

function GetXmlHttpObject(handler)
{ 
	var objXMLHttp=null;
	
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return objXMLHttp
}

document.onkeydown = onKeyPress;
document.onclick = hideHint;