var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
var pulldownon = false;
function liveSearchInit()
{
	if(navigator.userAgent.indexOf("Safari") > 0)
		document.getElementById('livesearch').addEventListener("keydown", liveSearchKeyPress, false);
	else if(navigator.product == "Gecko")
	{
		document.getElementById('livesearch').addEventListener("keypress", liveSearchKeyPress, false);
		document.getElementById('livesearch').addEventListener("blur", liveSearchHideDelayed, false);
	}
	else
	{
		document.getElementById('livesearch').attachEvent('onkeydown', liveSearchKeyPress);
		isIE = true;
	}
	document.getElementById('livesearch').setAttribute("autocomplete", "off");
}
function liveSearchHideDelayed()
{
	window.setTimeout("liveSearchHide()", 400);
}
function liveSearchHide()
{
	if(pulldownon == false)
	{
		document.getElementById("LSResult").style.display = "none";
		var highlight = document.getElementById("LSHighlight");
		if(highlight)
			highlight.removeAttribute("id");
	}
}
function togglePulldown(pulldown)
{
	if(pulldownon == false)
	{
		pulldownon = true;
		liveSearchProcessReqChange(pulldown);
	}
	else
	{
		pulldownon = false;
		liveSearchHide();
	}
}
function liveSearchKeyPress(event)
{
	//KEY DOWN	
	if(event.keyCode == 40)
	{
		highlight = document.getElementById("LSHighlight");
		if(!highlight) highlight = document.getElementById("LSResult").firstChild.firstChild;
		else
		{
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if(highlight) highlight.setAttribute("id", "LSHighlight");
		if(!isIE) event.preventDefault();
	} 
	//KEY UP
	else if(event.keyCode == 38) 
	{
		highlight = document.getElementById("LSHighlight");
		if(!highlight) highlight = document.getElementById("LSResult").firstChild.lastChild;
		else
		{
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if(highlight) highlight.setAttribute("id", "LSHighlight");
		if(!isIE) event.preventDefault();
	} 
	//ESC
	else if(event.keyCode == 27)
	{
		highlight = document.getElementById("LSHighlight");
		if(highlight) highlight.removeAttribute("id");
		document.getElementById("LSResult").style.display = "none";
	} 
	//BACKSPACE - required for IE
	else if(event.keyCode == 8 && isIE) liveSearchStart();
}
function liveSearchStart()
{
	if(t) window.clearTimeout(t);
	t = window.setTimeout("liveSearchDoSearch()",200);
}
function liveSearchDoSearch()
{
	if(typeof liveSearchRoot == "undefined") liveSearchRoot = "";
	if(typeof liveSearchRootSubDir == "undefined") liveSearchRootSubDir = "";
	if(typeof liveSearchParams == "undefined") liveSearchParams2 = "";
	else liveSearchParams2 = "&" + liveSearchParams;
	if(liveSearchLast != document.forms.searchform.q.value)
	{
		if(document.forms.searchform.q.value == "")
		{
			liveSearchHide();
			liveSearchLast = "";
			return false;
		}
		liveSearchLast = document.forms.searchform.q.value;
		
		pulldownon = false;
		request = 'http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=OSTRl6TV34EWtFB.tyjtdeHYzfSvmrKdn5K.8EURYqNTywdo0odoWJRnZyu9ag--&output=json&callback=liveSearchProcessReqChange&query='+document.forms.searchform.q.value;
		liveSearchReq = new JSONscriptRequest(request);
		liveSearchReq.buildScriptTag();
		liveSearchReq.addScriptTag();
	}
}
function liveSearchProcessReqChange(jData)
{
	if(jData)
	{
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var html = '<ul class=\'LSRes\'>';
		for(var i=0, count=jData['ResultSet']['Result'].length; i<count; i++)
			html += '<li class="LSRow" onmouseover="liveSearchHover(this);" onclick="liveSearchClicked(this);"><a href="/?q='+escape(jData['ResultSet']['Result'][i])+'">'+jData['ResultSet']['Result'][i]+'</a></li>';
		html += '</ul>';
		res.innerHTML = html;
	}
	liveSearchReq.removeScriptTag();
}
function liveSearchSubmit()
{
	var highlight = document.getElementById("LSHighlight");
	if(highlight && highlight.firstChild)
	{
		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.firstChild.getAttribute("href");
		return false;
	} 
	else return true;
}
function liveSearchHover(el)
{
	highlight = document.getElementById("LSHighlight");
	if(highlight) highlight.removeAttribute("id");
	el.setAttribute("id","LSHighlight");
}
function liveSearchClicked(el)
{
	highlight = document.getElementById("LSHighlight");
	if(highlight) highlight.removeAttribute("id");
	el.setAttribute("id","LSHighlight");
	return liveSearchSubmit();
}
liveSearchInit();

// http://www.theurer.cc/blog/2005/12/15/web-services-json-dump-your-proxy/
function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);  
}
JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}