var rkSuggest = function(strId) {
	this.bufferText = false;
	this.bufferTime = 0;
	this.strId = strId;
	this.cur = -1;
	this.layer = 0;
	this.init();
};

rkSuggest.prototype = {
	makeRequest : function() {
		oThis = this;
		xajax.call("suggest",{parameters:[oThis.strId.value],context:this});
	},
	compareBuffer : function(strText) {
		if (strText == this.strId.value && strText != this.bufferText) {
			this.bufferText = strText;
			this.makeRequest();
		}
	},
	modified : function() {
		oThis = this;
		setTimeout('oThis.compareBuffer("'+this.strId.value+'")', this.bufferTime);
	},
	showSuggestions : function() {
		this.layer.style.visibility = "visible";
	},
	hideSuggestions : function() {
		xajax.$('suggest').innerHTML = '';
		this.layer.style.visibility = "hidden";
	},
	init : function() {
		oThis = this;

		this.strId.onkeyup = function (oEvent) {
			if (!oEvent) {
				oEvent = window.event;
			}
			oThis.handleKeyUp(oEvent);
		};

		this.strId.onkeydown = function (oEvent) {
			if (!oEvent) {
				oEvent = window.event;
			}
			oThis.handleKeyDown(oEvent);
		};

		this.strId.onblur = function () {
			oThis.hideSuggestions();
		};

		this.createDropDown();
	},
	handleKeyUp : function(oEvent) {
		iKeyCode = oEvent.keyCode;

    //for backspace (8) and delete (46), shows suggestions without typeahead
		if (iKeyCode == 8 || iKeyCode == 46) {
			this.modified();

    //make sure not to interfere with non-character keys
		} else if (iKeyCode >0 && iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
		} else {
        //request suggestions from the suggestion provider with typeahead
			this.modified();
		}
	},
	handleKeyDown : function(oEvent) {
		switch(oEvent.keyCode) {
			case 38: //up arrow
					this.previousSuggestion();
			break;
			case 40: //down arrow
					this.nextSuggestion();
			break;
			case 13: //enter
					this.hideSuggestions();
			break;
		}
	},
	previousSuggestion : function() {
		var cSuggestionNodes = this.layer.childNodes;

		if (cSuggestionNodes.length > 0 && this.cur > 0) {
			var oNode = cSuggestionNodes[--this.cur];
			this.highlightSuggestion(oNode);
			this.strId.value = oNode.firstChild.nodeValue;
		}
	},
	nextSuggestion :function() {
		var cSuggestionNodes = this.layer.childNodes;

		if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1) {
			var oNode = cSuggestionNodes[++this.cur];
			this.highlightSuggestion(oNode);
			this.strId.value = oNode.firstChild.nodeValue;
		}
	},
	highlightSuggestion : function(oSuggestionNode) {
		for (var i=0; i < this.layer.childNodes.length; i++) {
			var oNode = this.layer.childNodes[i];
			if (oNode == oSuggestionNode) {
				oNode.className = "current"
			} else if (oNode.className == "current") {
				oNode.className = "";
			}
		}
	},
	createDropDown : function() {
		oThis = this;

		this.layer = document.getElementById("suggest");
		this.layer.style.width = this.strId.offsetWidth;

    //when the user clicks on the a suggestion, get the text (innerHTML)
    //and place it into a textbox
		this.layer.onmousedown =
		this.layer.onmouseup =
		this.layer.onmouseover = function (oEvent) {
			oEvent = oEvent || window.event;
			oTarget = oEvent.target || oEvent.srcElement;

			if (oEvent.type == "mousedown") {
				oThis.strId.value = oTarget.firstChild.nodeValue;
				oThis.hideSuggestions();
			} else if (oEvent.type == "mouseover") {
				oThis.highlightSuggestion(oTarget);
			} else {
				oThis.strId.focus();
			}
		};

	}
};
