var autoFillTimeoutFunction, 
		autoFillTimeout;

addLoadEvent(function () {
	window.autoFillList = document.createElement('div');
	window.autoFillList.id = 'autofill';
	document.body.appendChild(window.autoFillList);
});

function autoFill (af) {
	if (typeof autoFillList == 'undefined') {
		return;
	}
	
	for (varname in af) {
		this[varname] = af[varname]
	}
	
	if (typeof autoFillTimeout == 'number') {
		clearTimeout(autoFillTimeout);
		autoFillTimeoutFunction();
	}
	
	autoFillList.style.display = 'none';
	autoFillList.innerHTML = '';
	
	var object = this.object;
	var left = object.offsetLeft, 
			top = object.offsetTop + object.offsetHeight + 2;
	
	while (object.offsetParent) {
		var offsetParent = object.offsetParent;
		object = object.offsetParent;
		left += object.offsetLeft;
		top += object.offsetTop;
	}
	
	autoFillList.style.left = left + 'px';
	autoFillList.style.top = top + 'px';
	autoFillList.style.width = this.object.offsetWidth + 'px';
	
	this.object.setAttribute('autocomplete', 'off');
	
	var that = this;
	
	this.prevText = this.object.value;
	this.lastURL = '';

	this.object.onblur = function () {
		autoFillTimeoutFunction = function () {
			autoFillList.style.display = 'none';
			autoFillList.innerHTML = '';
			delete that;
		}
		
		autoFillTimeout = setTimeout(autoFillTimeoutFunction, 250);
	}
	
	this.object.onkeydown = function (event) {
		if (window.event) {
			event = window.event;
		}
		
		if (event.keyCode == 38 || event.keyCode == 40) {
			if (autoFillList.style.display == 'none') {
				return;
			}
			
			var index = that.hover();
			if (index == -1 && event.keyCode == 38) {
				return;
			} else if (index > -1) {
				autoFillList.childNodes[index].className = '';
			}
			
			if (event.keyCode == 38) {
				index --;
				if (index > -1) {
					autoFillList.childNodes[index].className = 'hover';
					that.lastURL = autoFillList.childNodes[index].href;
				}
			} else {
				index ++;
				if (index == autoFillList.childNodes.length) {
					index = -1;
				} else {
					autoFillList.childNodes[index].className = 'hover';
					that.lastURL = autoFillList.childNodes[index].href;
				}
			}
			
			if (index != -1 && autoFillList.childNodes[index].innerText) {
				that.object.value = autoFillList.childNodes[index].innerText;
			} else if (index != -1 && autoFillList.childNodes[index].textContent) {
				that.object.value = autoFillList.childNodes[index].textContent;
			} else if (index == -1) {
				that.object.value = that.prevText;
			} else {
				that.object.value = '';
			}
		}
	}
	
	this.object.onkeyup = function (event) {
		if (window.event) {
			event = window.event;
		}

		if (typeof that.onChange == 'function' && !that.onChange(this.value, this) || !this.value.length || event.keyCode == 27) {
			autoFillList.style.display = 'none';
			that.prevText = this.value;
			return false;
		} else if (event.keyCode == 13 || event.keyCode == 38 || event.keyCode == 40 || this.value == that.prevText) {
			return;
		}
				
		try {
			new Ajax.Request(that.url + escape(typeof that.onValue == 'function' ? that.onValue(this.value) : this.value), {
				method: 'get',
				onSuccess: function (transport) {
					autoFillList.innerHTML = '';
					
					if (transport.responseText == '' || transport.responseText == '[]') {
						autoFillList.style.display = 'none';
						return;
					}
					
					var json = eval('(' + transport.responseText + ')');
					for (label in json) {
						var item = document.createElement('a');
						var itemlabel = document.createTextNode(label);
						item.appendChild(itemlabel);
						item.href = json[label];
						
						item.onmouseover = function () {
							var index = that.hover();
							if (index > -1) {
								autoFillList.childNodes[index].className = '';
							}
							
							this.className = 'hover';
							that.lastURL = this.href;
						}
						
						item.onclick = function () {
							if (that.object.innerText) {
								that.object.value = this.innerText;
							} else if (that.object.textContent) {
								that.object.value = this.textContent;
							}
						}
						
						if (typeof that.onClick == 'function') {
							var onclick = item.onclick;
							item.onclick = function () {
								onclick();

								if (this.innerText) {
									var value = this.innerText;
								} else if (this.textContent) {
									var value = this.textContent;
								}
								return that.onClick(value, that.object);
							}
						}
						
						if (json[label] == that.lastURL) {
							item.className = 'hover';
						}
						autoFillList.appendChild(item);
					}
					
					if (!autoFillList.childNodes.length) {
						autoFillList.style.display = 'none';
						return;
					}
					
					autoFillList.style.display = 'block';
					if (typeof autoFillTimeout == 'number') {
						clearTimeout(autoFillTimeout);
					}
				}
			});
		} catch (e) { }
	}
	
	this.hover = function () {
		for (index in autoFillList.childNodes) {
			if (autoFillList.childNodes[index] != undefined && autoFillList.childNodes[index].className == 'hover') {
				return index;
			}
		}
		return -1;
	}
}