
function searchSuggest(sTextboxID) {
	this.SourceInput = getE(sTextboxID);
	this.Container = 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];

		// 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.Container.style.left = oPos.x + "px";
		me.Container.style.top = (me.SourceInput.clientHeight + oPos.y + 3) + "px";
		oBody.appendChild(me.Container);
	}

	//
	// OnKeyUp event handler
	//
	this.onKeyUpHandler = function(e) {

		if (me.SourceInput.value.length==0) {
			sCustValue = "";
			me.endProcess();
			return;
		}
		else {
		      sCustValue = me.SourceInput.value;
              bInProcess = true;
		}

		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); 
				me.callAjax();
		}

		//alert(e.keyCode);
		//inspectObject(me);

	}

	//
	// Vola ajax
	//
	this.callAjax = function() {
		
		$.ajax({
            type: "GET",
            url: "/searchsuggest_back.asp?exps=" + encodeURIComponent(me.SourceInput.value) + "&l=" + gnLangID,
            success: function (msg) {
                if (msg != "[object XMLDocument]")
                {
                	//alert(msg);
                    me.Container.style.display = "block";
                    bInProcess = true;
                    me.Container.innerHTML = msg;
                }
                else
                    me.Container.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();
	}


	//
	// 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"; }, 200);
		bInProcess = false;
	}

	return true;
}

