function autoCompleteDB()
{
	this.aNames=new Array();
}

autoCompleteDB.prototype.assignArray=function(aList)
{
	this.aNames=aList;
};

autoCompleteDB.prototype.getMatches=function(str,aList,maxSize)
{
	/* debug */ //alert(maxSize+"ok getmatches");
	var ctr=0;
	for(var i in this.aNames)
	{
		if(this.aNames[i].toLowerCase().indexOf(str.toLowerCase())==0 || 1) /*looking for case insensitive matches - bypassed */
		{
			aList.push(this.aNames[i]);
			ctr++;
		}
		if(ctr==(maxSize-1)) /* counter to limit no of matches to maxSize */
			break;
	}
};

function autoComplete(aNames,oText,oDiv,maxSize)
{

	this.oText=oText;
	this.oDiv=oDiv;
	this.maxSize=maxSize;
	this.cur=-1;

	
	/*debug here */
	//alert(oText+","+this.oDiv);
	
	this.db=new autoCompleteDB();
	this.db.assignArray(aNames);
	
	oText.onkeyup=this.keyUp;
	oText.onkeydown=this.keyDown;
	oText.autoComplete=this;
	oText.onblur=this.hideSuggest;
	
}

autoComplete.prototype.hideSuggest=function()
{
	//this.autoComplete.oDiv.style.visibility="hidden";
	window.autompletehandler.oDiv.style.visibility="hidden";
};

autoComplete.prototype.selectText=function(iStart,iEnd)
{
	if(this.oText.createTextRange) /* For IE */
	{
		var oRange=this.oText.createTextRange();
		oRange.moveStart("character",iStart);
		oRange.moveEnd("character",iEnd-this.oText.value.length);
		oRange.select();
	}
	else if(this.oText.setSelectionRange) /* For Mozilla */
	{
		this.oText.setSelectionRange(iStart,iEnd);
	}
	this.oText.focus();
};

autoComplete.prototype.textComplete=function(sFirstMatch) /* bypass */
{
/*	if(this.oText.createTextRange || this.oText.setSelectionRange)
	{
		var iStart=this.oText.value.length;
		this.oText.value=sFirstMatch;
		this.selectText(iStart,sFirstMatch.length);
	}*/
};

autoComplete.prototype.keyDown=function(oEvent)
{
	oEvent=window.event || oEvent;
	iKeyCode=oEvent.keyCode;

	switch(iKeyCode)
	{
		case 38: //up arrow
			this.autoComplete.moveUp();
			break;
		case 40: //down arrow
			this.autoComplete.moveDown();
			break;
		case 13: //return key
			this.oText.focus();
			break;
	}
};

autoComplete.prototype.moveDown=function()
{
	if(this.oDiv.childNodes.length>0 && this.cur<(this.oDiv.childNodes.length-1))
	{
		++this.cur;
		for(var i=0;i<this.oDiv.childNodes.length;i++)
		{
			if(i==this.cur)
			{
				this.oDiv.childNodes[i].className="over";
				this.oText.value=strip_tags(this.oDiv.childNodes[i].innerHTML);
			}
			else
			{
				this.oDiv.childNodes[i].className="";
			}
		}
	}
};

autoComplete.prototype.moveUp=function()
{
	if(this.oDiv.childNodes.length>0 && this.cur>0)
	{
		--this.cur;
		for(var i=0;i<this.oDiv.childNodes.length;i++)
		{
			if(i==this.cur)
			{
				this.oDiv.childNodes[i].className="over";
				this.oText.value=strip_tags(this.oDiv.childNodes[i].innerHTML);
			}
			else
			{
				this.oDiv.childNodes[i].className="";
			}
		}
	}
};

autoComplete.prototype.keyUp=function(oEvent)
{
	oEvent=oEvent || window.event;
	var iKeyCode=oEvent.keyCode;
	if(iKeyCode==8 || iKeyCode==46)
	{
		this.autoComplete.onTextChange(false); /* without autocomplete */
	}
	else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) 
	{
        //ignore
    }
	else 
	{
		this.autoComplete.onTextChange(true); /* with autocomplete */
	}
};

autoComplete.prototype.positionSuggest=function() /* to calculate the appropriate poistion of the dropdown */
{
/*	var oNode=this.oText;
	var x=0,y=oNode.offsetHeight;

	while(oNode.offsetParent && oNode.offsetParent.tagName.toUpperCase() != 'BODY')
	{
		x+=oNode.offsetLeft;
		y+=oNode.offsetTop;
		oNode=oNode.offsetParent;
	}

	x+=oNode.offsetLeft;
	y+=oNode.offsetTop;

	this.oDiv.style.top=y+"px";
	this.oDiv.style.left=x+"px";*/
}

autoComplete.prototype.onTextChange=function(bTextComplete)
{

	var lang = null;
	
	for( i = 0; i < document.getElementById('searchform').lang.length; i++ ){
		if( document.getElementById('searchform').lang[i].checked){
			lang = document.getElementById('searchform').lang[i].value;
		}
	}
	
	suggest_ajax('/.dictionary/.ajax/suggest.ajax.php?query='+Url.encode(this.oText.value)+'&lang='+lang+'&local='+window.local);


};

autoComplete.prototype.onTextChangeAsync=function(bTextComplete)
{

	var txt=this.oText.value;
	var oThis=this;
	this.cur=-1;
	
	if(txt.length>0)
	{
		while(this.oDiv.hasChildNodes())
			this.oDiv.removeChild(this.oDiv.firstChild);
		
		var aStr=new Array();
		this.db.getMatches(txt,aStr,this.maxSize);
		if(!aStr.length) {this.hideSuggest ;return}
		if(bTextComplete) this.textComplete(aStr[0]);
		this.positionSuggest();
		
		for(i in aStr)
		{
			var oNew=document.createElement('div');
			this.oDiv.appendChild(oNew);
			oNew.onmouseover=
			oNew.onmouseout=
			oNew.onmousedown=function(oEvent)
			{
				oEvent=window.event || oEvent;
				oSrcDiv=oEvent.target || oEvent.srcElement;

				//debug :window.status=oEvent.type;
				if(oEvent.type=="mousedown")
				{
					oThis.oText.value=strip_tags(this.innerHTML);
				}
				else if(oEvent.type=="mouseover")
				{
					this.className="over";
				}
				else if(oEvent.type=="mouseout")
				{
					this.className="";
				}
				else
				{
					this.oText.focus();
				}
			};
			oNew.innerHTML=aStr[i];
		}
		
		this.oDiv.style.visibility="visible";
	}
	else
	{
		this.oDiv.innerHTML="";
		this.oDiv.style.visibility="hidden";
	}
};

function createAutoComplete()
{
var aNames = [];

window.autompletehandler = new autoComplete(aNames,document.getElementById('queryinput'),document.getElementById('suggest'),10);
}

function suggest_ajax(stranka)
{

    var httpRequest;
    
    if(typeof window.ActiveXObject != 'undefined')
    {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {  
        httpRequest = new XMLHttpRequest();
    }
    httpRequest.open("GET", stranka, true);
    httpRequest.onreadystatechange= function () 
    {
        processRequest(httpRequest) 
    };
    httpRequest.send(null);
}


function processRequest(httpRequest) 
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
        {
    
        //alert(httpRequest.responseText);
        
        arr = httpRequest.responseText.split('|');
        window.autompletehandler.db.assignArray(arr);
        window.autompletehandler.onTextChangeAsync(1);
        
        }
    }
       
} 


/**
*
* URL encode
* http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },


    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

}

function strip_tags(text){
 return text.replace(/<\/?[^>]+>/gi, '');
}