var isIE = false;
var checkcss = false;

function divausblenden()
{
	document.getElementById('search-results').style.display='none';
}

function diveinblenden()
{
	document.getElementById('search-results').style.display='block';
}

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;
	}
}


function liveSearchKey (event) 
{
	
	if (event.keyCode == 40 )
	{	
		cnt = document.getElementById("search-results").childNodes.length;
		
		if(cnt > 0)
		{
			highlight = document.getElementById("ls_highlight");
			
			if (!highlight) 
			{
				if (document.getElementById("lsie"))
				{
					highlight = document.getElementById("lsie").firstChild;
				}
				else
				{
					highlight = document.getElementById("lsff").firstChild;
				}
			} 
			else 
			{
				if(checkcss == false)
				{
					highlight.setAttribute("id","li_ls_22");
					checkcss = true;
				}
				else
				{
					highlight.setAttribute("id","li_ls_2");
					checkcss = false;
				}
				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) 
			{
				if (document.getElementById("lsie"))
				{
					highlight = document.getElementById("lsie").lastChild;
				}
				else
				{
					highlight = document.getElementById("lsff").lastChild;
				}
			} 
			else 
			{
				if(checkcss == false)
				{
					highlight.setAttribute("id","li_ls_22");
					checkcss = true;
				}
				else
				{
					highlight.setAttribute("id","li_ls_2");
					checkcss = false;
				}
				highlight = highlight.previousSibling;
			}
			
			if (highlight) 
			{
				highlight.setAttribute("id","ls_highlight");
			}
			
			if (!isIE) { event.preventDefault(); }
		}
	}
	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) 
	{
		window.location = highlight.firstChild.getAttribute("href");
		return false;
	} 
	else 
	{
		return true;
	}
}

function activateSearch() 
{
    if ($('searchform')) 
    {
    	//$('searchform').onsubmit = function() { doSearch();return false; };
		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)
	{ 
	    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: 'get',
				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"
}


Event.observe(window, 'load', activateSearch, false);