/*
// +----------------------------------------------------------+
// | The new livesearch based on the original by Bitflux GmbH |
// | implementing YAHOO.util.Connect                          |
// +----------------------------------------------------------+
// | Copyright (c) 2006 CentralNic Ltd                        |
// +----------------------------------------------------------+
// | Refer to orginial copyrights (Bitflux GmbH / Yahoo)      |
// +----------------------------------------------------------+
// | Author: Sivananda Ganesamoorthy                          |
// +----------------------------------------------------------+
*/

var t = null;
var liveSearchLast = "";
var isIE = false;

function liveSearchInit() {
	if(!document.getElementById) return false;
	if(!document.getElementById(inputid)) return false;
	
	var query = document.getElementById(inputid);
	document.getElementById("search").onsubmit=function()
	{
		var highlight = document.getElementById("search-results-highlight");
		if (highlight && highlight.firstChild) {
			window.location = highlight.firstChild.getAttribute("href");
			return false;
		} 
		else {
			return true;
		}	
	};
	YAHOO.util.Event.addListener(query,"keypress",liveSearchStart);
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		YAHOO.util.Event.addListener(query,"keydown",liveSearchKeyPress);
	} else if (navigator.product == "Gecko") {
		YAHOO.util.Event.addListener(query,"keypress",liveSearchKeyPress);
		YAHOO.util.Event.addListener(query,"blur",liveSearchHideDelayed);
	} else {
		document.getElementById('query').attachEvent('onkeydown',liveSearchKeyPress);
		isIE = true;
	}
	
	document.getElementById('query').setAttribute("AutoComplete","off");

}

function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",400);
}
	
function liveSearchHide() {
	document.getElementById("search-results").style.display = "none";
	var highlight = document.getElementById("search-results-hightlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("search-results-highlight");
		if (!highlight) {
			highlight = document.getElementById("search-results").firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight && YAHOO.util.Dom.getStyle("search-results","display") == "block") {
			highlight.setAttribute("id","search-results-highlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("search-results-highlight");
		alert(highlight.previousSibling);
		if (!highlight) {
			highlight = document.getElementById("search-results").lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight && YAHOO.util.Dom.getStyle("search-results","display") == "block") {
				highlight.setAttribute("id","search-results-highlight");
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("search-results-highlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("search-results").style.display = "none";
	}
	// Back Space
	else if (event.keyCode == 8) {
		liveSearchDoSearch();
	}	
}

function liveSearchStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchDoSearch() {
	if (liveSearchLast != document.forms.search.q.value) {
		var responseSuccess = function(o)
		{
			if(!o.responseText){
				liveSearchHide();
				return false;
			}
			if(document.forms.search.q.value == "") {
				YAHOO.util.Dom.setStyle("search-results","display","none");
				if(navigator.userAgent.match("compatible; MSIE")) {
					YAHOO.util.Dom.setStyle("column-b","zIndex","0");
					YAHOO.util.Dom.setStyle("content","zIndex","0");
				}
			} else {
			YAHOO.util.Dom.setStyle("search-results","display","block");
			document.getElementById("search-results").innerHTML = o.responseText;
			if(navigator.userAgent.match("compatible; MSIE")) {
				YAHOO.util.Dom.setStyle("column-b","zIndex","-1");
				YAHOO.util.Dom.setStyle("content","zIndex","-1");
			}
			
			// Opacity can be set here for a bit of spice	
			// YAHOO.util.Dom.setStyle("search","opacity",0.9);

			}
		};
		
		var responseFailure = function(o)
		{
			document.getElementById("search-results").style.display = "block";
			document.getElementById("search-results").innerHTML = 
										'There has been an error processing your request';
		};
		
		var callback = 
		{
			success : responseSuccess,
			failure : responseFailure,
			argument: null
		};

		var cObj = YAHOO.util.Connect.asyncRequest("GET","/util/livesearch.php?q="+document.forms.search.q.value,callback,null);
		liveSearchLast = document.forms.search.q.value;
	}
}
