app_search = function() {
	pointer = this;

	this.oSearchField = $('#searchparam');

	this.oOptions = {
		minchars : 2,
		delay : 100
	};

	this.oTimer = null;

	this.sSearchparam = "";
	this.nInputChars = 0;
	this.aSuggestions = [];
	this.iHighlighted = 0;

	this.searchLive = function() {
		searchparam = this.oSearchField.val();

		// if input stays the same, do nothing
		if (searchparam == this.sSearchparam) {
			return false;
		}

		// input length is less than the min required to trigger a request
		// reset input string do nothing
		if (searchparam.length < this.oOptions.minchars) {
			this.sSearchparam = "";
			return false;
		}

		clearTimeout(this.oTimer);
		this.oTimer = setTimeout(function() {
			pointer.doAjaxRequest();
		}, this.oOptions.delay);

	};

	this.doAjaxRequest = function() {
		$('#searchResults').hide();
		$('#searchResults table tbody').html("");

		oRequest = $.ajax( {
			type : "GET",
			url : "/modules/mhwd_appsearch/mhwd_appsearch.php",
			dataType : "text",
			data : "searchparam=" + searchparam,
			success : function(data) {
				pointer.showResults(data);
			}
		});
	};

	this.showResults = function(data) {
		$('#searchResults table tbody').html(data);
		if(data=!"") {
			$('#searchResults').show();
			document.onclick = function() { $('#searchResults').hide(); };
		};
	};

	this.oSearchField.keypress(function(ev) {
		var key = (window.event) ? window.event.keyCode : ev.keyCode;

		var RETURN = 13;
		var TAB = 9;
		var ESC = 27;

		var bubble = true;

		switch (key) {
		case RETURN:
			// this.setHighlightedValue();
//			bubble = false;
			break;
		case ESC:
			// this.clearSuggestions();
			break;
		}

		return bubble;
	});

	this.oSearchField.keyup(function(ev) {
		var key = (window.event) ? window.event.keyCode : ev.keyCode;

		var ARRUP = 38;
		var ARRDN = 40;

		var bubble = true;

		switch (key) {
		case ARRUP:
			// this.changeHighlight(key);
			bubble = false;
			break;
		case ARRDN:
			// this.changeHighlight(key);
			bubble = false;
			break;
		default:
			pointer.searchLive();
		}

		return bubble;
	});
	
	this.handleClick = function(id) {
		window.location.href = $('#link_'+id).attr('href');
	};

};

$(document).ready(function() {
	oApp_search = new app_search();
});

