/*
	Filename         :	mysqllivesearch.js
	Created by       :	Mark Thomas
	Last modified by :	Mark Thomas
	Created          :	21 June 2005 14:26:59
	Last Updated     :	21 June 2005 14:27:47
	Comments         :	
*/


indicoAddLoad(function(){
	oIndico.liveMySQLSearch();
});

IndicoPage.prototype.liveMySQLSearch = function(){
	this.oSearchField = document.getElementById("fQuery");
	if(!this.oSearchField){
		return false;
	};
	this.oSearchField.parentNode.style.position = "relative";
	var oCont = document.createElement("div");
	oCont.id = "mySQLLiveSearch";
	oCont.style.top = this.oSearchField.offsetHeight + this.oSearchField.offsetTop + 1 + "px";
	oCont.style.left = this.oSearchField.offsetLeft + "px";
	oCont.style.width = this.oSearchField.offsetWidth - 2 + "px";
	this.oSearchField.parentNode.appendChild(oCont);
	this.oSearchField.setAttribute("autocomplete", "off");
	oCont = null;
	this.oSearchField.onkeyup = function(e){
		var oEvent = window.event || e;
		if(this.value.length > 2 && (oEvent.keyCode > 40 || oEvent.keyCode == 8)){
			var oXMLHTTP = oIndico.xmlHTTP();
			oXMLHTTP.open("GET", "cms/plugins/mysqlsearch/plugin." + oIndico.sLanguage + "?fQuery=" + this.value + "&sXML=<plugin nRecords=\"5\"/>", true);
			oXMLHTTP.onreadystatechange = function(){
				if(oXMLHTTP.readyState == 4){
					oIndico.__liveMySQLSearched(oXMLHTTP.responseText);
				};
			};
			oXMLHTTP.send(null);
		};
	};
	this.oSearchField.onkeypress = function(e){
		var oEvent = window.event || e;
		if(oEvent.keyCode == 13 && document.getElementById("mySQLLiveSearchHighLight")){
			if(document.getElementById("mySQLLiveSearchHighLight").getAttribute("href")){
				return false;
			};
		};
	};
	this.oSearchField.onkeydown = function(e){
		var oEvent = window.event || e;
		oHighLight = false;
		if(this.value.length > 2){
			if(oEvent.keyCode == 13){
				if(document.getElementById("mySQLLiveSearchHighLight")){
					window.location = document.getElementById("mySQLLiveSearchHighLight").getAttribute("href");
					return false;
				};
			};
			if(oEvent.keyCode == 40){//down
				oHighLight = document.getElementById("mySQLLiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("mySQLLiveSearch").getElementsByTagName("a")[0];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.nextSibling;
				};
			}else if(oEvent.keyCode == 38 ){//up
				oHighLight = document.getElementById("mySQLLiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("mySQLLiveSearch").getElementsByTagName("a")[document.getElementById("mySQLLiveSearch").getElementsByTagName("a").length-1];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.previousSibling;
				};
			}else if(oEvent.keyCode == 27){//esc
				document.getElementById("mySQLLiveSearch").style.visibility = "hidden";
			};
			if(oHighLight){
				oHighLight.setAttribute("id","mySQLLiveSearchHighLight");
			};
		};
	};
	this.oSearchField.onblur = function(){
		setTimeout('document.getElementById("mySQLLiveSearch").style.visibility = "hidden"', 150);
	};
};
IndicoPage.prototype.__liveMySQLSearched = function(sXML){
	var oXML = this.xmlDocument();
	sXML = sXML.replace(RegExp("<em>", "g"), "");
	sXML = sXML.replace(RegExp("</em>", "g"), "");
	oXML.loadXML(sXML);
	var aLinks = oXML.getElementsByTagName("h2");
	if(!aLinks.length){
		return document.getElementById("mySQLLiveSearch").style.visibility = "hidden";
	};
	document.getElementById("mySQLLiveSearch").innerHTML = "<strong>Quick results</strong>";
	var a;
	for(var i=0; i<aLinks.length; i++){
		a = document.createElement("a");
		a.setAttribute("href", aLinks[i].firstChild.getAttribute("href"));
		a.appendChild(document.createTextNode(aLinks[i].firstChild.firstChild.nodeValue));
		document.getElementById("mySQLLiveSearch").appendChild(a);
		a = null;
	};
	oXML = null;
	document.getElementById("mySQLLiveSearch").style.visibility = "visible";
};