
function searchSuggest(sTextboxID) {
	this.SourceInput = getE(sTextboxID);
	this.Container = null;
  this.LoadingNotif = null;

	var me = this;
	var bInProcess = false;
	var sCustValue = "";
	var ajaxTimeout = null;
	var oCurLink = null;

	//
	// Inicializace
	//
	this.init = function() {
		if (!me.SourceInput) { /*alert("Source input not found.");*/ return;	}
		//var oBody = document.getElementsByTagName("body")[0];
    var oBody = getE("containerHeader");

		// Navazeme udalosti
		if (document.addEventListener) {
			me.SourceInput.addEventListener("keyup", me.onKeyUpHandler, false);
			me.SourceInput.addEventListener("blur", me.onBlurHandler, false);
		}
		else {
			me.SourceInput.attachEvent("onkeyup", me.onKeyUpHandler);
			me.SourceInput.attachEvent("onblur", me.onBlurHandler);
		}

		// Vytvorime DIV
		var oPos = getPosition(me.SourceInput);
		me.Container = document.createElement("div");
		me.Container.setAttribute("id", "searchSuggest");
    
    me.LoadingNotif = document.createElement("img");
		me.LoadingNotif.setAttribute("id", "searchSuggestLoading");
    me.LoadingNotif.setAttribute("src", "/img/loading.gif");

   
		oBody.appendChild(me.Container);
    oBody.appendChild(me.LoadingNotif);
           
	}

	//
	// OnKeyUp event handler
	//
	this.onKeyUpHandler = function(e) {

		if (me.SourceInput.value.length==0) {
			sCustValue = "";
			me.endProcess();
			return;
		}
		else if (!bInProcess) {
			me.startProcess();
		}

		switch (e.keyCode) {
			case 40: // Down
				me.moveDown();
				break;

			case 38: // Up
				me.moveUp();
				break;

			case 33: // PageUp 
			case 34: // PageDown
			case 35: // End
			case 36: // Home
			case 37: // Left
			case 39: // Right
				break;

			case 27: // Escape
				me.SourceInput.value = sCustValue;
				me.endProcess();
				break;

			default: // Ostatni
				if (ajaxTimeout) window.clearTimeout(ajaxTimeout);
				ajaxTimeout = window.setTimeout(me.callAjax, 500);
    	}

		//alert(e.keyCode);
		//inspectObject(me);

	}

	//
	// Vola ajax
	//
	this.callAjax = function() {
    me.LoadingNotif.style.display = "block";
		var ajax = new Ajax("/Back_search.asp?exps=" + encodeURIComponent(me.SourceInput.value) + "&l=" + gnLangID, "get", me.ajaxCallback);
		ajax.sendRQ("", false);
	}

	//
	// Ajax Callback handler
	//
	this.ajaxCallback = function(sText) {
		if (sText.length==0) {
			me.endProcess();
		}
		else {
			me.startProcess();
		}

		me.Container.innerHTML = sText;
    me.LoadingNotif.style.display = "none";
	}

	//
	// Pri stisknuti tlacitka dolu vybere a element
	//
	this.moveDown = function() {
		if (oCurLink) oCurLink.className = "";

		if (!oCurLink) {
			oCurLink = me.Container.getElementsByTagName("a")[0];
		}
		else {
			oCurLink = oCurLink.nextSibling;
			while (oCurLink!=null) {
				if (oCurLink.nodeType==1 && oCurLink.attributes["phrase"]!=undefined) break;
				oCurLink = oCurLink.nextSibling;
			}
		}

		if (oCurLink) {
			oCurLink.className = "sel";
		}

		if (oCurLink!=null && oCurLink.nodeType==1 && oCurLink.attributes["phrase"]!=undefined)
			me.SourceInput.value = oCurLink.getAttribute("phrase");
		else
			me.SourceInput.value = sCustValue;
	}

	//
	// Pri stisknuti tlacitka nahoru vybere a element
	//
	this.moveUp = function() {
		if (oCurLink) oCurLink.className = "";

		if (!oCurLink) {
			var items = me.Container.getElementsByTagName("a");
			oCurLink = items[items.length - 1];
		}
		else {
			oCurLink = oCurLink.previousSibling;
			while (oCurLink!=null) {
				if (oCurLink.nodeType==1 && oCurLink.attributes["phrase"]!=undefined) break;
				oCurLink = oCurLink.previousSibling;
			}
		}

		if (oCurLink) {
			oCurLink.className = "sel";
		}

		if (oCurLink!=null && oCurLink.nodeType==1 && oCurLink.attributes["phrase"]!=undefined)
			me.SourceInput.value = oCurLink.getAttribute("phrase");
		else
			me.SourceInput.value = sCustValue;
	}

	//
	// OnBlur event handler
	//
	this.onBlurHandler = function() {
		me.endProcess();
	}

	//
	// Zacatek zpracovani
	//
	this.startProcess = function() {
		sCustValue = me.SourceInput.value;
		me.Container.style.display = "block";
    me.LoadingNotif.style.display = "block";
		bInProcess = true;
	}

	//
	// Ukonceni zpracovani
	//
	this.endProcess = function() {
		if (ajaxTimeout) window.clearTimeout(ajaxTimeout);
//		if (sCustValue.length>0) me.SourceInput.value = sCustValue;
		sCustValue = "";
		window.setTimeout(function() { me.Container.style.display = "none"; me.LoadingNotif.style.display = "none"; }, 200);
		bInProcess = false;
	}

	return true;
}

function getPosition(e, toElement){
	var left = 0;
	var top  = 0; 
	while (e){
		left += e.offsetLeft; 
		top += e.offsetTop; 
		e = e.offsetParent;

		if (e && toElement && e == toElement) 
			break;
	}
  
   
	return {x:left, y:top}; 
} 

