
mtLibAddEvent(function(){
	oMTLib.liveSearch();
}, "load");

oMTLib.liveSearch = function(){
	this.oSearchField = document.getElementById("fQuery");
	if(!this.oSearchField){
		return false;
	};
	if(!oMTLib.xmlHTTP()){
		return;
	};
	this.oForm = document.getElementById("fFormSearch");
	this.oFormButton = document.getElementById("fSubmit");
	this.oSearchField.parentNode.style.position = "relative";
	var oCont = document.createElement("div");
	oCont.id = "LiveSearch";
	oCont.style.top = this.oSearchField.offsetHeight + this.oSearchField.offsetTop + 1 + "px";
	oCont.style.left = this.oSearchField.offsetLeft + "px";
	oCont.style.width = this.oSearchField.parentNode.offsetWidth + 2 + "px";
	this.oSearchField.parentNode.appendChild(oCont);
	this.oSearchField.setAttribute("autocomplete", "off");
	this.oAnim = new oMTLib.animation(oCont.id);
	this.oAnim.nOpacity = 5;
	oCont = null;
	var self = this;
	this.oSearchField.onkeyup = function(e){
		var oEvent = window.event || e;
		if(this.value.length > 2 && (oEvent.keyCode > 40 || oEvent.keyCode == 8)){
			self.oFormButton.className = "loading";
			Ajax.request({
				uri			: "cmsincludes/i_google_search.asp?fQuery=" + this.value + "&bAJAXify=true&sDocumentElementTag=results&sPattern=<a href%3D\"%25u%25\">%25t%25<p>%25s%25</p></a>",
				callback	: oMTLib.__liveSearched, 
				scope		: oMTLib
			});
		}else{
			self.oFormButton.className = "submit";
		};
	};
	this.oSearchField.onkeypress = function(e){
		var oEvent = window.event || e;
		if(oEvent.keyCode == 13 && document.getElementById("LiveSearchHighLight")){
			if(document.getElementById("LiveSearchHighLight").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("LiveSearchHighLight")){
					if(document.getElementById("LiveSearchHighLight").href){
						window.location = document.getElementById("LiveSearchHighLight").getAttribute("href");
						return false;
					};
				};
			};
			if(oEvent.keyCode == 40){//down
				oHighLight = document.getElementById("LiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("LiveSearch").getElementsByTagName("a")[0];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.nextSibling;
				};

			}else if(oEvent.keyCode == 38 ){//up
				oHighLight = document.getElementById("LiveSearchHighLight");
				if(!oHighLight){
					oHighLight = document.getElementById("LiveSearch").getElementsByTagName("a")[document.getElementById("LiveSearch").getElementsByTagName("a").length-1];
				}else{
					oHighLight.removeAttribute("id");
					oHighLight = oHighLight.previousSibling;
				};
			}else if(oEvent.keyCode == 27){//esc
				//document.getElementById("LiveSearch").style.visibility = "hidden";
				self.oAnim.fade(50);
			};
			if(oHighLight){
				oHighLight.setAttribute("id","LiveSearchHighLight");
				oHighLight.scrollIntoView();
			};
		};
	};
	this.oSearchField.onblur = function(){
		setTimeout('document.aMTLibAnimations["LiveSearch"].fade(50)', 150);
	};
};
oMTLib.__liveSearched = function(oXML){
	var aLinks = oXML.responseXML.getElementsByTagName("a");
	this.oFormButton.className = "submit";
	if(!aLinks.length){
		return document.getElementById("LiveSearch").style.visibility = "hidden";
	};
	document.getElementById("LiveSearch").innerHTML = "<h2>Search results</h2>";
	var a, p, s;
	for(var i=0; i<aLinks.length && i<10; i++){
		a = document.createElement("a");
		s = document.createElement("strong");
		p = document.createElement("span");
		a.setAttribute("href", aLinks[i].getAttribute("href"));
		s.appendChild(document.createTextNode(aLinks[i].firstChild.nodeValue));
		if(aLinks[i].childNodes[1].firstChild)
			p.appendChild(document.createTextNode(aLinks[i].childNodes[1].firstChild.nodeValue));
		a.appendChild(s);
		a.appendChild(p);
		document.getElementById("LiveSearch").appendChild(a);
		a = null;
		p = null;
	};
	oXML = null;
	document.getElementById("LiveSearch").style.visibility = "visible";
	this.oAnim.fade(50, 1);
};