var isIE = false;

function liveSearchInit() 
{
	if (navigator.userAgent.indexOf("Safari") > 0) 
	{
		document.getElementById('s').addEventListener("keydown",liveSearchKey,false);
	} 
	else if (navigator.product == "Gecko") 
	{
		document.getElementById('s').addEventListener("keypress",liveSearchKey,false);
	} 
	else 
	{
		document.getElementById('s').attachEvent('onkeydown',liveSearchKey);
		isIE = true;
	}
	
	//document.searchform.livesearch.focus()
}


function liveSearchKey (event) 
{
	
	if (event.keyCode == 40 )
	{	
		cnt = document.getElementById("search-results").childNodes.length;
		
		if(cnt > 0)
		{
			highlight = document.getElementById("ls_highlight");
			
			if (!highlight) 
			{
				highlight = document.getElementById("ls").firstChild;
			} 
			else 
			{
				highlight.setAttribute("id","li_ls_2");;
				highlight = highlight.nextSibling;
			}
	
			if (highlight) 
			{
				highlight.setAttribute("id","ls_highlight");
			}
			
			if (!isIE)
			{ 
				event.preventDefault(); 
			}
		}
			
	}
	else if (event.keyCode == 38 ) 
	{
			
		cnt = document.getElementById("search-results").childNodes.length;
		
		if(cnt > 0)
		{
			highlight = document.getElementById("ls_highlight");
			
			if (!highlight) 
			{
				highlight = document.getElementById("ls").lastChild;
			} 
			else 
			{
				highlight.setAttribute("id","li_ls_2");;
				highlight = highlight.previousSibling;
			}
			
			if (highlight) 
			{
				highlight.setAttribute("id","ls_highlight");
			}
			
			if (!isIE) 
			{ 
				event.preventDefault(); 
			}
		}
	}
	else if (event.keyCode == 27) 
	{
		anim.hide();
	}
	else if (event.keyCode == 13 ) 
	{
		return liveSearchSubmit();
	}
	else if (event.keyCode == 8) 
	{
		anim.hide();
	}
	

}

function liveSearchSubmit() 
{
	
	var highlight = document.getElementById("ls_highlight");
	
	if (highlight && highlight.firstChild) 
	{
		
		document.location.href = highlight.firstChild.getAttribute("href");
		
		return false;
	} 
	else 
	{
		return true;
	}
}

function activateSearch() 
{
    if ($('searchform')) 
    {
    	//$('searchform').onsubmit = function() { anim.hide();return true; };
		anim = new fx.Height('search-results', {duration: 400});
		anim.hide();
		new Form.Element.Observer('s', 1, doSearch);
        is_searching = false;
    }
}

function doSearch() 
{
	if(document.forms.searchform.livesearch.value.length >= 3 &&
		!IsNumeric(document.forms.searchform.livesearch.value))
	{ 
		
	    if (is_searching) 
	    	return false;
	    	
	    s = $F('s');
		anim.hide();
	
	    if (s == '') 
	    	return false; 
	    	
	    is_searching = true;
		Element.show($('wait'));
	
		pars = Form.serialize('searchform');
	    var myAjax = new Ajax.Request(
			'livesuche.php',
			{
				method: 'post',
				parameters: pars,
				onComplete:doSearchResponse
			}
		);
	}
}

function doSearchResponse(response) 
{
    $('search-results').innerHTML = response.responseText;
	anim.toggle();
	Element.hide($('wait'));
	is_searching = false;
}

function liveSearchHide() 
{
	document.getElementById("search-results").style.display = "none";
}

function liveSearchShow()
{
	document.getElementById("search-results").style.display = "block";
	doSearch();
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
 }

Event.observe(window, 'load', activateSearch, false);
