// Copyright: Carsten Stadler - source-worx.de
// use, copying, etc. is not permitted

// create the search popup div onload
document.write('<div id="search_popup" style="display: none;" class="standard" onmouseover="setCloseState(false);" onmouseout="setCloseState(true);"></div>');

var close_popup = true;

function getRes(strURL,query_string,div_id)
{	
	var xmlHttpReq = false;
	var self = this;
	
	if (window.XMLHttpRequest) // Mozilla/Safari
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) // IE
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	self.xmlHttpReq.onreadystatechange = function()
	{
		if (self.xmlHttpReq.readyState == 4)
		{
			//alert(self.xmlHttpReq.responseText);
			div_id.innerHTML = self.xmlHttpReq.responseText;
		}
	}
	
	self.xmlHttpReq.send(query_string);
}


function showRes(val,sed)
{
	// the url of the search script
	var request_url = "http://" + window.location.hostname + "/search_popup.php";
	
	// the query string for the search script
	var query_string = "sed=" + sed + "&search_text=";
	
	// the height of the search field
	var search_field_height = 17;
	
	// the padding/distance from the div where the search field is in
	var search_field_padding = 5;
	
	//document.getElementById('search_popup').style.display= "none";
	
	if (val.length > 2)
	{	
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) // for ie 5.x,6.x,etc. and firefox >= 3.x
		{
			var pos_open_left = document.getElementById('navi_search').getBoundingClientRect().left + search_field_padding;
			var pos_top = document.getElementById('box_navi').getBoundingClientRect().top + search_field_height - 2; // get the sum of the top positions of all divs where the search field is inside
		}
		else // for firefox 2.x, 3.x
		{
			var pos_open_left = document.getElementById('navi_search').offsetLeft + search_field_padding;
			var pos_top = document.getElementById('navi_search').offsetTop + search_field_height; // get the sum of the top positions of all divs where the search field is inside
		}
		
		var pos_open_top = pos_top + search_field_height;
		
		// set the search field visible, if thr user scrolledd the page down
		if (document.body.scrollTop > pos_top) document.body.scrollTop = pos_top - search_field_height / 2;
		
		// open the popup div
		document.getElementById('search_popup').style.top = pos_open_top;
		document.getElementById('search_popup').style.left = pos_open_left;
		document.getElementById('search_popup').style.display= "block";
		
		// get the search result
		query_string = query_string + val;
		getRes(request_url,query_string,document.getElementById('search_popup'));
	}
	else 
	{
		document.getElementById('search_popup').innerHTML = "";
		document.getElementById('search_popup').style.display = "none";
	}
}

function closeRes()
{
	if (close_popup == true) document.getElementById('search_popup').style.display= "none";
}

function setCloseState(state)
{
	close_popup = state;
	document.getElementById('search_field').focus();
}
	
	