	/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

*/
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var queryTarget = "livesearch_reply?q=";

var searchForm = null;
var searchInput = null; 

var isIE = false;

function getElementDimensions(elemID) {
    var base = document.getElementById(elemID);
    var offsetTrail = base;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop, 
	width:base.offsetWidth, height: base.offsetHeight,
	bottom: offsetTop + base.offsetHeight, 
	right : offsetLeft + base.offsetWidth};
}

function liveSearchInit() {
	searchInput = document.getElementById('searchGadget');
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		searchInput.addEventListener("keydown",liveSearchKeyPress,false);
		searchInput.addEventListener("focus",liveSearchDoSearch,false);
//		searchInput.addEventListener("blur",liveSearchHide,false);
	} else if (navigator.product == "Gecko") {
		searchInput.addEventListener("keypress",liveSearchKeyPress,false);
		searchInput.addEventListener("blur",liveSearchHideDelayed,false);
	} else {
		searchInput.attachEvent('onkeydown',liveSearchKeyPress);
//		searchInput.attachEvent("onblur",liveSearchHide);
		isIE = true;
	}
			
	searchInput.setAttribute("autocomplete","off");

	var pos = getElementDimensions('searchGadget');	
	result = document.getElementById('LSResult');
	result.style.position="absolute";
	result.style.top = pos.bottom + "px";
	result.style.left = pos.left + "px";	
	result.style.width = pos.width + "px";
}

function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",400);
}
	
function liveSearchHide() {
	document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function findNext(object, specifier) {
 var cur = object;
 var prev = object;
 try {
 while (cur != undefined) {
 	prev = cur; // [AC] Keep record of previous li to prevent highlight falling off the bottom of the list
	cur = cur.nextSibling;
	if (specifier(cur) == true) return cur;
 }
 return prev;
 } catch(e) {};
  return prev; // [AC] This is the statement that is triggered when highlight leaves the bottom of the list...
}

function findPrev(object, specifier) {
 var cur = object;
 try {
	 while (cur != undefined) {
		cur = cur.previousSibling;
		if (specifier(cur) == true) return cur
 	}
 } catch(e) {};
 return null;
}


function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSShadow").getElementsByTagName('li')[0];
		} else {
			highlight.removeAttribute("id");
			highlight = findNext(highlight, function (o) {return o.nodeName.toLowerCase()=='li';});

		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			var set = document.getElementById("LSShadow").getElementsByTagName('li');
			highlight = set[set.length];
		} 
		else {
			highlight.removeAttribute("id");
			highlight = findPrev(highlight, function (o) {return o.nodeName.toLowerCase()=='li';});
		}
		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";
	} 
}
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 (liveSearchLast != searchInput.value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( searchInput.value == "") {
		liveSearchHide();
		return false;
	}

	liveSearchReq = Sarissa.getXmlHttpRequest();
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	liveSearchReq.open("GET", liveSearchRoot + queryTarget + searchInput.value );
	liveSearchLast = searchInput.value;
	liveSearchReq.send(null);
	}
}


function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("LSResult");
		if (liveSearchReq.status > 299 || liveSearchReq.status < 200  ||
			liveSearchReq.responseText.length < 10) return;	
		res.style.display = "block";
		var  sh = document.getElementById("LSShadow");
		
		sh.innerHTML = liveSearchReq.responseText;
		 
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	
	if (highlight){
		target = highlight.getElementsByTagName('a')[0];
		window.location = liveSearchRoot + liveSearchRootSubDir + target;
		return false;
	} 
	else {
		return true;
	}
}



if (window.addEventListener) window.addEventListener("load",liveSearchInit,false);
else if (window.attachEvent) window.attachEvent("onload", liveSearchInit);

