Autocomplete = Class.create();
Autocomplete.prototype = {
	initialize: function (element, container, ajaxurl, form, submitform) {
		this.element = $(element);
		this.container = $(container);
		this.element.setAttribute("autocomplete", "off");

		this.more_results_selected = false;
		this.last_value = "";
		this.list_nodes = [];
		this.current_selection_index = -1;
		this.previous_selection_index = this.current_selection_index;
		this.parent_form = document.forms[form];
		this.ajaxurl = ajaxurl;
		this.submitform = submitform;

		Event.observe(this.element, 'keydown', this.keyDown.bindAsEventListener(this), false);
		Event.observe(this.element, 'keyup', this.keyPressed.bindAsEventListener(this), false);
		Event.observe(this.element, 'blur', this.inputLostFocus.bindAsEventListener(this), false);
		
		Event.observe(this.element, "focus", this.onFocus.bindAsEventListener(this), false);
		//Event.observe(this.parent_form, 'submit', this.processSubmit.bindAsEventListener(this), false);
	},

	sanitizeQuery: function (query) {
		var sanitized_query = query;
		sanitized_query = sanitized_query.replace(/[\&]/g,'');
		return escape(sanitized_query);
	},

	processSubmit: function () {

	},

	closeList: function () {
		this.list_nodes = [];
		this.current_selection_index = -1;
		this.previous_selection_index = this.current_selection_index;
		this.container.style.display = "none";
		
		//var IfrRef = $('DivShim');
		//IfrRef.style.display = "none";
	},

	listNodeClicked: function (e) {
		var node = Event.element(e);
		var prepop = node.getAttribute('prepop');
		if (!prepop) {
			node = node.parentNode;
		}
		prepop = node.getAttribute('prepop');
		var saved_last_value = this.last_value;
		this.last_value = node.innerHTML.replace(/<.*?>/g, '');
		
		var aryPrePop = prepop.split("|")
		
		for( var i=0; i < aryPrePop.length; i+=2 )
		{
			this.parent_form.elements[aryPrePop[i]].value = aryPrePop[i+1];
		}
		
		if( this.submitform == true )
		{
			this.parent_form.submit();
		}
		
	},

	listNodeFocusedWithMouse: function (e) {
		var node = Event.element(e);
		var url = node.getAttribute('prepop');
		if (!url) {
			node = node.parentNode;
		}
		var node_index = this.list_nodes.indexOf(node);

		this.previous_selection_index = this.current_selection_index;
		if (this.previous_selection_index >= 0 && this.previous_selection_index < this.list_nodes.length) {
			this.list_nodes[this.previous_selection_index].className = "normal";
		}
		this.current_selection_index = node_index;
		this.list_nodes[this.current_selection_index].className = "focused";
	},
	
	onFocus: function(e) {
		this.watchKeyStrokes();
	},

	inputLostFocus: function (e) {
		this.last_value = "";
		this.closeList();
	},

	keyPressed: function (e) {
		switch (e.keyCode) {
		case Event.KEY_ESC:
			this.element.value = this.last_value;
			this.last_value = "";
			this.closeList();
			return;
			break;
		case Event.KEY_RETURN:
			this.closeList();
			return;
			break;
		case Event.KEY_LEFT:
		case Event.KEY_RIGHT:
			return;
			break;
		case Event.KEY_UP:
		case Event.KEY_DOWN:
			if (this.container.style.display == "none") {
				return;
			}
			this.previous_selection_index = this.current_selection_index;
			if (this.previous_selection_index >= 0 && this.previous_selection_index < this.list_nodes.length) {
				this.list_nodes[this.previous_selection_index].className = "normal";
			}

			switch (e.keyCode) {
				case Event.KEY_UP:
				this.current_selection_index--;
				if (this.current_selection_index < 0) {
					this.current_selection_index = -1;
					this.element.value = this.last_value;
				}
				break;
				case Event.KEY_DOWN:
				this.current_selection_index++;
				if (this.current_selection_index >= this.list_nodes.length) {
					this.current_selection_index = this.list_nodes.length - 1;
				}
				break;
			}

			if (this.current_selection_index >= 0 && this.current_selection_index < this.list_nodes.length) {
				var selected_node = this.list_nodes[this.current_selection_index];
				selected_node.className = "focused";
			}
			return;
			break;
		}
		this.watchKeyStrokes();
	},
	
	keyDown: function (e) {
		switch (e.keyCode) {
		case Event.KEY_RETURN:
			
			var selected_node = this.list_nodes[this.current_selection_index];
			
			if( selected_node )
			{
				var prepop = selected_node.getAttribute('prepop');
				
				var aryPrePop = prepop.split("|");
				
				for( var i=0; i < aryPrePop.length; i+=2 )
				{
					this.parent_form.elements[aryPrePop[i]].value = aryPrePop[i+1];
				}
				
				Event.stop(e);
				
				this.closeList();
				
				if( this.submitform == true )
				{
					this.parent_form.submit();
				}
			}
			
			return;
			break;
		}
	},

	ajaxRequestCompleted: function (originating_request) {
		this.container.innerHTML = originating_request.responseText;
		this.list_nodes = $A(this.container.getElementsByTagName('div'));
		if (this.list_nodes.length) {
			for (var i = 0; i < this.list_nodes.length; i++) {
				var node = this.list_nodes[i];
				node.onmousedown = this.listNodeClicked.bindAsEventListener(this);
				node.onmouseover = this.listNodeFocusedWithMouse.bindAsEventListener(this);
			}
			this.container.style.display = "block";
			
			var DivRef = $(this.container);
			//var IfrRef = $('DivShim');
			
			//IfrRef.style.width = DivRef.offsetWidth + 'px';
			//IfrRef.style.height = DivRef.offsetHeight + 'px';
			//IfrRef.style.top = DivRef.style.top;
			//IfrRef.style.left = DivRef.style.left;
			//IfrRef.style.display = "block";
		}
		else {
			this.closeList();
		}

		this.loading = false;
		if (this.element.value != this.last_value) {
			this.watchKeyStrokes();
		}
	},

	watchKeyStrokes: function () {
		if (this.loading) {
			return;
		}
		if (this.element.value.length < 0) {
			this.closeList();
			return;
		}

		this.loading = true;
		this.last_value = this.element.value;

		var aryUrl = this.ajaxurl.split("?");

		var url = aryUrl[0];
		var params = aryUrl[1] + '&q=' + this.sanitizeQuery(this.element.value);
		var ajax_request = new Ajax.Request(url, {
				method: 'get',
				parameters: params,
				onComplete: this.ajaxRequestCompleted.bind(this)
			}
		);
	}
}