(function($) {  
	var self = null;
 	
	jQuery.fn.liveUpdate = function(list, exclude) {	
		return this.each(function() {
			new jQuery.liveUpdate(this, list, exclude);
		});
	};
	
	jQuery.liveUpdate = function (e, list, exclude) {
		this.field = jQuery(e);
		this.list  = jQuery('#' + list);
		this.exclude = exclude;
		
		if (this.list.length > 0) {
			this.init();
		}
	};
	
	jQuery.liveUpdate.prototype = {
		init: function() {
			var self = this;
			this.setupCache();
			this.field.parents('form').submit(function() { return false; });
			this.field.keyup(function() { self.filter(); });
			self.filter();
		},
		
		filter: function(exclude) {
			if (jQuery.trim(this.field.val()) == '') { this.list.children('li').show(); return; }
			this.displayResults(this.getScores(this.field.val().toLowerCase()));
		},
		
		setupCache: function() {
			var self = this;
			this.cache = [];
			this.rows = [];
			this.list.children('li').each(function() {
				var label = jQuery("label", this).text();
				self.cache.push(label.toLowerCase());
				self.rows.push(jQuery(this));
			});
			this.cache_length = this.cache.length;
		},
		
		displayResults: function(scores) {
			var self = this;
			this.list.children('li').hide();
			jQuery.each(scores, function(i, score) { self.rows[score[1]].show(); });
			this.list.children('li'+self.exclude+'').show();
		},
		
		getScores: function(term) {
			var scores = [];
			for (var i=0; i < this.cache_length; i++) {
				var score = this.cache[i].score(term);
				if (score > 0) { scores.push([score, i]); }
			}
			return scores.sort(function(a, b) { return b[0] - a[0]; });
		}
	}
})(jQuery);