/*
** $Id$
**
** 	Zen LiveSearch (C) 2006 damonparker.org <damonp@damonparker.org>
** 	Based on origninal work by Bitflux GmbH <devel@bitflux.ch>
**
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of the GNU General Public License
**	as published by the Free Software Foundation; either version 2
**	of the License, or (at your option) any later version.
**
**	This program is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program; if not, write to the Free Software
**	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/*
// +----------------------------------------------------------------------+
// | Origninal Work Copyright (c) 2004 Bitflux GmbH <devel@bitflux.ch>    |
// +----------------------------------------------------------------------+
// | 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                           |
// +----------------------------------------------------------------------+
*/

var LSReq = false;
var t = null;
var LSLast = "";
var LSHost = "http://"+window.location.hostname;

var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	LSReq = new XMLHttpRequest();
}

function LSInit() {

	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById('livesearch').addEventListener("keydown",LSKeyPress,false);
//		document.getElementById('livesearch').addEventListener("blur",LSHide,false);
	} else if (navigator.product == "Gecko") {

		document.getElementById('livesearch').addEventListener("keypress",LSKeyPress,false);
		document.getElementById('livesearch').addEventListener("blur",LSHideDelayed,false);

	} else {
		document.getElementById('livesearch').attachEvent('onkeydown',LSKeyPress);
//		document.getElementById('livesearch').attachEvent("onblur",LSHide,false);
		isIE = true;
	}

	document.getElementById('livesearch').setAttribute("autocomplete","off");

}

function LSHideDelayed() {
	window.setTimeout("LSHide()",400);
}

function LSHide() {
	document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function LSKeyPress(event) {

	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSShadow").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("LSResult").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("LSResult").style.display = "none";
	}
}
function LSStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("LSDoSearch()",200);
}

function LSClearSearchBy()
	{
	if (document.getElementById('livesearch').value == 'live search')
		{
		document.getElementById('livesearch').value = '';
		}
	}

function LSDoSearch() {

	if (typeof LSHost == "undefined") {
		LSHost = "";
	}
	if (typeof LSHostSubDir == "undefined") {
		LSHostSubDir = "";
	}
	if (typeof LSParams == "undefined") {
		LSParams = "";
	}
	if (LSLast != document.forms.searchform.keyword.value) {
	if (LSReq && LSReq.readyState < 4) {
		LSReq.abort();
	}
	if ( document.forms.searchform.keyword.value == "") {
		LSHide();
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		LSReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	LSReq.onreadystatechange= LSProcessReqChange;
	LSReq.open("GET", LSHost + "/livesearch.php?keyword=" + document.forms.searchform.keyword.value + LSParams);
	LSLast = document.forms.searchform.keyword.value;
	LSReq.send(null);
	}
}

function LSProcessReqChange() {

	if (LSReq.readyState == 4) {
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var  sh = document.getElementById("LSShadow");

		sh.innerHTML = LSReq.responseText;

	}
}

function LSSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = LSHost + LSHostSubDir + highlight.firstChild.nextSibling.getAttribute("href");
		return false;
	}
	else {
		return true;
	}
}

function LSCloseResults() {
    document.getElementById("LSResult").style.display = "none";
}
