/* ==================================================================================
SearchSuggest v.1.01

Optimized for: IE 6.0, IE 7.0 and FireFox 7.0

Created by Thomas Reichelt

added Updates:
BugFix for IE and FireFox Cache-Control 2007/10/19
KeyEvents for KeyDown and Keyup 2007/10/23
================================================================================== */

var liveSearchReq = false;
var trigger_hide = null;
var t = null;
var liveSearchLast = "";
	
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest)
	{
	liveSearchReq = new XMLHttpRequest();
	}

function liveSearchInit()
	{
	if (navigator.userAgent.indexOf("Safari") > 0)
		{
		document.getElementById('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
		//		document.getElementById('livesearch').addEventListener("blur",liveSearchHide,false);
		}
	else if (navigator.product == "Gecko")
		{
		document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
		document.getElementById('livesearch').addEventListener("blur",liveSearchHideDelayed,false);
		}
	else
		{
		document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
		//		document.getElementById('livesearch').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
		}
	
	document.getElementById('livesearch').setAttribute("autocomplete","off");
	}
	
function liveSearchHide()
	{
	document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight)
		{
		highlight.removeAttribute("id");
		}
	}

/*
function liveSearchHover(el) {   
        highlight = document.getElementById("LSHighlight");   
        if (highlight) {   
            highlight.removeAttribute("id");   
        }   
        el.setAttribute("id","LSHighlight");   
}   
  
function liveSearchClicked(el) {   
        highlight = document.getElementById("LSHighlight");   
        if (highlight) {   
            highlight.removeAttribute("id");   
        }   
        el.setAttribute("id","LSHighlight");   
        return liveSearchSubmit();   
}  */


function liveSearchKeyPress(event)
	{
	var i_down 	= 0;
	var i_up 		= 0;
	if (event.keyCode == 40 )
		//KEY DOWN
		{
		if (trigger_hide) { window.clearTimeout(trigger_hide); }
		trigger_hide = window.setTimeout("liveSearchHide()",4000);
			
		highlight 			= document.getElementById("LSHighlight");
		li_elemente			= (document.forms.searchform.getElementsByTagName("li").length)-1;
		if (!highlight) 
			{
			document.forms.searchform.getElementsByTagName("li")[0].setAttribute("id","LSHighlight");
			highlight 			= document.getElementById("LSHighlight");
			highlight_prev	=	'';
			document.forms.searchform.getElementsByTagName("li")[li_elemente].style.backgroundColor = "white";
			document.forms.searchform.getElementsByTagName("li")[li_elemente].style.border 					= "0px";
			} 
		else
			{
			highlight.removeAttribute("id");
			highlight_prev	=	highlight;
			highlight 			= highlight.nextSibling;
			}
		
		if (highlight_prev) 
			{
			highlight_prev.style.backgroundColor = "white";
			highlight_prev.style.border 				 = "0px";
			}
		if (highlight) 
			{
			highlight.setAttribute("id","LSHighlight");
			highlight.style.backgroundColor = "#EEEEEE";
			highlight.style.border = "1px solid #666666";	
			} 
		
		if (!isIE) { event.preventDefault(); }
		} 
	
	//KEY UP
	else if (event.keyCode == 38 )
		{
		if (trigger_hide) { window.clearTimeout(trigger_hide); }
		trigger_hide = window.setTimeout("liveSearchHide()",4000);
		
		li_elemente			= (document.forms.searchform.getElementsByTagName("li").length)-1;
		highlight = document.getElementById("LSHighlight");
		if (!highlight)
			{
			document.forms.searchform.getElementsByTagName("li")[li_elemente].setAttribute("id","LSHighlight");
			highlight = document.getElementById("LSHighlight");
			highlight_nex = '';
			document.forms.searchform.getElementsByTagName("li")[0].style.backgroundColor = "white";
			document.forms.searchform.getElementsByTagName("li")[0].style.border 					= "0px";
			} 
		else
			{
			highlight.removeAttribute("id");
			highlight_nex = highlight;
			highlight = highlight.previousSibling;
			}
			
		if (highlight_nex) 
			{
			highlight_nex.style.backgroundColor = "white";
			highlight_nex.style.border 				 	= "0px";
			}
		if (highlight)
			{
			highlight.setAttribute("id","LSHighlight");
			highlight.style.backgroundColor = "#EEEEEE";
			highlight.style.border = "1px solid #666666";
			}
		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";
		}
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE)
		{
		liveSearchStart();
		}
	
	//RETURN
	else if (event.keyCode == 13 )
		{
		return liveSearchSubmit();
		}
	}


function liveSearchStart()
	{
	if (t)  { window.clearTimeout(t); }
	if (trigger_hide) { window.clearTimeout(trigger_hide); }
	
	t = window.setTimeout("liveSearchDoSearch()",500);
	trigger_hide = window.setTimeout("liveSearchHide()",4000);
	}

function liveSearchDoSearch()
{
	if (typeof liveSearchRoot == "undefined")
		{
		liveSearchRoot = "";
		}
	if (typeof liveSearchRootSubDir == "undefined")
		{
		liveSearchRootSubDir = "";
		}
	if (typeof liveSearchParams == "undefined")
		{
		liveSearchParams2 = "";
		} 
	else
		{
		liveSearchParams2 = "&" + liveSearchParams;
		}
	if (liveSearchLast != document.forms.searchform.search_detail_value.value)
		{
		if (liveSearchReq && liveSearchReq.readyState < 4)
			{
			liveSearchReq.abort();
			}
		if ( document.forms.searchform.search_detail_value.value == "")
			{
			liveSearchHide();
			liveSearchLast = "";
			return false;
			}
		if (window.XMLHttpRequest)
			{
			// branch for IE/Windows ActiveX version
			} 
		else if (window.ActiveXObject)
			{
			liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
		liveSearchReq.open("GET", liveSearchRoot + "/livesearch.php?search_detail_value=" + document.forms.searchform.search_detail_value.value + liveSearchParams2);
		liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
		liveSearchReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		liveSearchReq.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		liveSearchReq.setRequestHeader('Expires', 'Mon, 26 Jul 1970, 00:00:00 GMT');
		liveSearchReq.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
		liveSearchReq.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0');
		liveSearchLast = document.forms.searchform.search_detail_value.value;
		liveSearchReq.send(null);
		}
}

function liveSearchProcessReqChange()
	{
	if (liveSearchReq.readyState == 4)
		{
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var  sh = document.getElementById("LSShadow");
		sh.innerHTML = liveSearchReq.responseText;
		
		var php_request = liveSearchReq.responseText.split("[|]");
		var php_request = php_request[1].split("=>");
		
		document.getElementById('LSResult').style.width  = php_request[1];		 
		//alert (php_request[1]);
		}
	}

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




/* preview comment code */

/*
function preview(request)
	{
	//
	var previewNode = $('previewComment');
	
	if (!previewNode)
		{
		var lastComment = $('bx_foo');
		var previewNode = document.createElement("div");
		previewNode.id = 'previewComment';
		previewNode.className = 'post_content';
		previewNode = lastComment.parentNode.insertBefore(previewNode,lastComment.nextSibling);
		
		}
	previewNode.innerHTML = request.responseText;
	}


function previewSubmit(test)
	{
	var name = document.getElementById('name').value;
	var mail = document.getElementById('email').value;
	var uri = document.getElementById('openid_url').value;
	var text = document.forms['bx_foo']['comments'].value;
	
	var f = document.forms['commentForm'];
	if (typeof liveSearchRoot == "undefined")
		{
		var liveSearchRoot = "/";
		}
		
	body = "mail=" + encodeURIComponent(mail) + "&uri="+ encodeURIComponent(uri) + "&text="+ encodeURIComponent(text) + "&name="+ encodeURIComponent(name);
	new ajax (liveSearchRoot + 'inc/bx/php/preview.php', {
			postBody: body,
							method: 'post',
							onComplete: preview
	});
	
	
	return false;
	}
	*/
