/*******************************************************************************
 * AutoComplete - Brian Wright - 9/2/2009
 * 
 * Requisites: position.js, ajax.js, lna.js
 * 
 * Usage: AutoComplete.setup(textElement, ajaxURL, onEnterPressedFunction,
 * maxResults[optional]);
 * 
 * textElement -->The object that you want to add autocomplete to ajaxURL -->The
 * url of the ajax query (minus the element value) such as:
 * "http://www.url.com/blah.jsp?elem=" onEnterPressedFunction -->The function
 * that gets called when a focused suggestion is triggered by an enter press.
 * Can be either the function itself, or a string representing the function's
 * name
 */
var AutoComplete = (new function() {
	var k = {};
	k.timeout = null;
	k.setup = function(f, g, h, max) {
		if (!max)
			max = 1000;
		f.hideHelper = function() {
			if (f.helper) {
				document.body.removeChild(f.helper);
				f.helper = null;
			}
		};
		var j = f.onkeyup, z = document.onmouseup;
		document.onmouseup = function() {
			if (z)
				z();
			f.hideHelper();
		};
		f.onkeyup = function(e) {
			var b = this;
			if (j)
				j(e);
			var c = getKeyCode(e);
			if (c == 40) {
				if (b.helper) {
					if (b.helper.childNodes.length > 0) {
						b.helper.childNodes[0].focus();
					}
				}
			} else if (c != 38 && c != 13) {
				var q = f.value.replace(/\\/g, "");
				if (q != f.value)
					f.value = q;
				if (b.value.length > 2) {
					var that = this;
					if (k.timeout)
						window.clearTimeout(k.timeout);
					k.timeout = window.setTimeout(function() {
						AJAX.load(g + that.value.replaceAll("%", "") + "&time=" + new Date().getTime(), function(x) {
							try {
								var a = eval(x.responseText.split("\n")[1]), l = a.length, i = 0;
								if (l > 0) {
									var d = document.createElement("div"), pos = Position.get(b), o = new StringBuffer(), pts;
									with (d.style) {
										position = "absolute";
										zIndex = "100";
										top = (pos.top + b.offsetHeight) + "px";
										left = pos.left + "px";
										width = "250px";
									}
									d.className = "acc";
									for (i; i < l && i < max; i = i + 1) {
										pts = a[i].split("::");
										o.append("<a href='javascript:;' id='" + pts[1] + "'>" + pts[0] + "</a>");
									}
									d.innerHTML = o.toString();
									b.hideHelper();
									document.body.appendChild(d);
									b.helper = d;
									a = d.childNodes;
									l = a.length;
									for (i = 0; i < l; i = i + 1) {
										a[i].index = i;
										a[i].size = l;
										a[i].onkeyup = function(e) {
											var c = getKeyCode(e);
											if (c == 40 && (this.index < this.size)) {
												if (this.parentNode.childNodes[this.index + 1])
													this.parentNode.childNodes[this.index + 1].focus();
											} else if (c == 38 && this.index > 0) {
												if (this.parentNode.childNodes[this.index - 1])
													this.parentNode.childNodes[this.index - 1].focus();
											} else if (c == 38 && this.index == 0) {
												b.focus();
												b.hideHelper();
											} else if (c == 13) {
												b.value = $eit(this);
												b.hideHelper();
												this.onmousedown();
												return false;
											}
										};
										a[i].onfocus = function() {
											this.style.backgroundColor = "#E3F2FC";
										};
										a[i].onblur = function() {
											this.style.backgroundColor = "#ffffff";
										};
										a[i].onmousedown = function() {
											b.focus();
											b.value = $eit(this);
											b.hideHelper();

											var id = this.id;
											var q = $eit(this);
											window.setTimeout(function() {
												document.location.href = "/lna/main.do?actionSource=documentClicked&selectedDocument=" + id + "&q=" + q;
												// document.location.href =
												// "/lnp/view.do?searchQuery=" +
												// q + "&pId=" + id;
											}, 250);
										};
									}
								} else {
									b.hideHelper();
								}
							} catch (e) {
								b.hideHelper();
							}
						});
					}, 200);
				} else {
					if (k.timeout)
						window.clearTimeout(k.timeout);
					b.hideHelper();
				}
			}
		};
	};
	return k;
});

