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.query.value)
	{
		if(document.forms.searchform.query.value == "")
		{
			liveSearchHide();
			liveSearchLast = "";
			return false;
		}
		liveSearchLast = document.forms.searchform.query.value;
		
		pulldownon = false;
		request ='http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=OSTRl6TV34EWtFB.tyjtdeHYzfSvmrKdn5K.8EURYqNTywdo0odoWJRnZyu9ag--&output=json&callback=liveSearchProcessReqChange&query='+document.forms.searchform.query.value;
		liveSearchReq = new JSONscriptRequest(request);
		liveSearchReq.buildScriptTag();
		liveSearchReq.addScriptTag();
	}
}
// http://onwebdevelopment.blogspot.com/2008/02/hacking-google-suggest-complete-with.html
// http://ispiders.blogspot.com/2007/09/google-search-json-api.html
var google = {
	ac:{
		Suggest_apply:function(frameElement, keyword, results, arr)
		{
			debug(keyword+' --------------');
			for(var i = 0; i < results.length; i=i+2)
			{
				if(i > 0)
				{
				debug(results[i-1]);
				}
			}
		}
	}
};
function liveSearchProcessReqChange(jData)
{
//	console.log(jData);
	if ((jData['Error'] != undefined)) {
		return false;
	}
	if((jData != '\n') && (jData != null))	{
		if ((jData['ResultSet']['Result'] != null)) {
			var  res = document.getElementById("LSResult");
			res.style.display = "block";
			var html = '<ul class=\'LSRes\'>';
			//var link_count = jData['ResultSet']['Result'].length;
			//console.log(jData['ResultSet']['Result']);
			if (Object.isArray((jData['ResultSet']['Result']))) {
				for(var i=0, count=jData['ResultSet']['Result'].length; i<count; i++)
				html += '<li class="LSRow" onmouseover="liveSearchHover(this);" onclick="liveSearchClicked(this);"><a href="/search/result?query='+escape(jData['ResultSet']['Result'][i].replace(' ',"+").replace(' ',"+").replace(' ',"+").replace(' ',"+"))+'">'+jData['ResultSet']['Result'][i]+'</a></li>';
			} else {			
				html += '<li class="LSRow" onmouseover="liveSearchHover(this);" onclick="liveSearchClicked(this);"><a href="/search/result?query='+escape(jData['ResultSet']['Result'].replace(' ',"+").replace(' ',"+").replace(' ',"+").replace(' ',"+"))+'">'+jData['ResultSet']['Result']+'</a></li>';
		
			}
			html += '</ul>';
			res.innerHTML = html;
		}
	}
}
function liveSearchSubmit()
{
	var highlight = document.getElementById("LSHighlight");
	if(highlight && highlight.firstChild)
	{
		$("livesearch").value = highlight.firstChild.innerHTML; 
		return false;	
	}
}
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");
	$("livesearch").value = highlight.firstChild.innerHTML;
	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);
}