/*
// +----------------------------------------------------------------------+
// | 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 suggestFieldId = "searchField";
var suggestScript = "/.suggest.php";
var suggestReq = false;
var t = null;
var suggestLast = "";
	
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	suggestReq = new XMLHttpRequest();
}

function suggestInit() {
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById('search').addEventListener("keydown",suggestKeyPress,false);
//		document.getElementById('search').addEventListener("blur",suggestHide,false);
	} else if (navigator.product == "Gecko") {
		
		document.getElementById('search').addEventListener("keypress",suggestKeyPress,false);
		document.getElementById('search').addEventListener("blur",suggestHideDelayed,false);
		
	} else {
		document.getElementById('search').attachEvent('onkeydown',suggestKeyPress);
//		document.getElementById('search').attachEvent("onblur",suggestHide,false);
		isIE = true;
	}
	
	document.getElementById('search').setAttribute("autocomplete","off");

}

function suggestHideDelayed() {
	window.setTimeout("suggestHide()",400);
}
	
function suggestHide() {
	document.getElementById("searchSuggestBox").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function suggestKeyPress(event) {
	
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("searchSuggestContent").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("searchSuggestBox").firstChild.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("searchSuggestBox").style.display = "none";
	} 
}
function suggestStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("suggestDoSearch()",200);
}

function suggestDoSearch() {
	
	var suggestField = document.getElementById(suggestFieldId);

	if (typeof suggestRoot == "undefined") {
		suggestRoot = "";
	}
	if (typeof suggestRootSubDir == "undefined") {
		suggestRootSubDir = "";
	}
	if (typeof suggestParams == "undefined") {
		suggestParams2 = "";
	} else {
		suggestParams2 = "&" + suggestParams;
	}
	if (suggestLast != suggestField.value) {
	if (suggestReq && suggestReq.readyState < 4) {
		suggestReq.abort();
	}
	if (suggestField.value == "") {
		suggestHide();
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		suggestReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	suggestReq.onreadystatechange= suggestProcessReqChange;
	suggestReq.open("GET", suggestScript+"?"+suggestFieldId+"=" + suggestField.value + suggestParams2);
	suggestLast = suggestField.value;
	suggestReq.send(null);
	}
}

function suggestProcessReqChange() {
	
	if (suggestReq.readyState == 4) {
		var  res = document.getElementById("searchSuggestBox");
		var  sh = document.getElementById("searchSuggestContent");
		res.style.display = "block";
		sh.innerHTML = suggestReq.responseText;		
		new Effect.Opacity(res, {duration:0.0, from:1.0, to:0.9});		 
	}
}

function suggestSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = suggestRoot + suggestRootSubDir + highlight.firstChild.nextSibling.getAttribute("href");
		return false;
	} 
	else {
		return true;
	}
}

