
jQuery.fn.liveSearch = function(conf) {
	var config = jQuery.extend({
		ajaxURL: '/mod/search-results.php?q='
	}, conf);

	return this.each(function() {
		var input		= jQuery(this);
		var results = jQuery('#sresult').slideUp();

		input.focus(function() {
			this.value = "";
		});
		input.keyup(function() {
			if((this.value != this.lastValue) && this.value) {
				input.addClass('ajax-loading');

				var q = this.value;

				if(this.timer) {
					clearTimeout(this.timer);
				}

				this.timer = setTimeout(function() {
					jQuery.get(config.ajaxURL +q, function(data) {
						input.removeClass('ajax-loading');

						if(data.length) {
							results.html(data).slideDown(300);
						}
						else {
							results.slideUp(300);
						}
					});
				}, 200);

				this.lastValue = this.value;
			}
		});
	});
};
