var xmlHttp;
var aktSugIndex=-1;
var aktSugValue="";
var sugCount=0;
var sugLastQ="";
var divVisible=false;
var divItemHeight=0;

function ajaxFunction(get)
{
document.getElementById("ajaxq").innerHTML=get;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=readyChange;
  
	xmlHttp.open("GET",get,true);
	xmlHttp.send(null);  
}



function readyChange()
{
	//alert(lb.innerHTML);
	document.getElementById("ajaxstate").innerHTML=xmlHttp.readyState;
	if (xmlHttp.readyState!=4) 
		return;
	//alert(xmlHttp.responseText);
	document.getElementById("divSug").innerHTML=xmlHttp.responseText;
	aktSugIndex=-1;
	aktSugValue="";
	div = document.getElementById("divSug");
	setSugCount();
	if (sugCount>0)
	{
		input=document.getElementById("sug_input");
		pos=findPos(input);
		div.style.visibility="visible";
		div.style.left=(pos[0])+"px";
		div.style.top=(pos[1]+22)+"px";
		setSugActive(0);
		divVisible=true;
		/*div.style.height=(divItemHeight*sugCount+2)+"px";*/
		
	}
	else
	{
		div.style.visibility="hidden";
		divVisible=false;
	}
}

function sugKeyDown(input, key){
	if (key==38 && aktSugIndex>0){
		setSugActive(aktSugIndex-1);
		if (!divVisible) sugLastQ="";
	}
	if (key==40 && aktSugIndex<sugCount-1){
		setSugActive(aktSugIndex+1);
		if (!divVisible) sugLastQ="";
	}
}

function sugKeyUp(input, key){
	if (input.value=="" || input.value.length<3){
		closeDiv();
		sugLastQ="";
		return;
	}
	if (input.value!=sugLastQ){
		ajaxFunction("sug.php?q="+encodeURI(input.value));
		sugLastQ=input.value;
	}
	return true;
}

function sugKeyPress(input, key){
	if (key==13 && divVisible){
		input.value=aktSugValue;
		sugLastQ=input.value;
		closeDiv();
		return false;
	}
}


function sugExit(){
	setTimeout("closeDiv()", 100);
}

function closeDiv(){
	div = document.getElementById("divSug");
	div.style.visibility="hidden";
	divVisible=false;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
	do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function setSugActive(n)
{
	aktSugValue="";
	aktSugIndex=n;
	div = document.getElementById("divSug");
	var it = div.firstChild;
	while ( it != div.lastChild )
	{
		if (it.nodeType == 1){
			if (it.id=="sug_item_"+n)
			{
				it.style.backgroundColor="#cbd2b3";
				aktSugValue=it.innerHTML;
			}
			else
				it.style.backgroundColor="#eef1e8";
		}
		it = it.nextSibling;
	}
}

function setSugCount()
{
	sugCount=0;
	div = document.getElementById("divSug");
	var it = div.firstChild;
	while ( it != div.lastChild ){
		if (it.nodeType == 1)
		{
			sugCount++;
			divItemHeight=it.scrollHeight;
		}
		it = it.nextSibling;
	}
}

function sugClick(n){
	input = document.getElementById("sug_input");
	input.value=aktSugValue;
	sugLastQ=input.value;
}


