/*

version 1.0

$('input').autocomplete(obj,cls,url,f);

obj = id of new unordered list object to create
cls = class to apply to obj
url = url to load results from
f   = callback function

*/

$.replace = function(s,w) {
	
	var r = new RegExp( '(' + w + ')', 'gi' );
	return s.replace(r, "<b>$1</b>" );
}

$.fn.extend({
		
	autocomplete: function(obj,cls,url,f) {
		
		this.keypress( function(event) {
		
			var el      = $(this);
			var keycode = event.which;
			var query   = $.trim(el.val() + String.fromCharCode(keycode));
			
			//if(query.length > 2 && keycode != 8) {
                        if(query.length > 0 && keycode != 8) {
			
				$('body').append('<ul id="' + obj + '"></ul>');
				
				var autocomplete = $('#' + obj);
				
				autocomplete.css({
					top: (el.offset().top + el.height() + 13) + 'px',
					left: (el.offset().left) + 'px',
					width: (el.outerWidth() - 12) + 'px'
				});
				
				autocomplete.addClass(cls);
				
				$.get(url + query, function (data) {
			
					if(data.length > 0) {
						autocomplete.fadeIn(250);
						autocomplete.html($.replace(data,query));
						autocomplete.children('li').click(function() {
							
							el.val($(this).text());
							
							if(typeof f == "function") f.call($(this));
							
							autocomplete.remove();	
						});
					} else {
						
						autocomplete.fadeOut().html('');
					}
				});
			} else {
				$('#' + autocomplete).fadeOut();
			}
		
		});
		
		this.blur( function() {
			$('#' + obj).fadeOut();
		});
	}
	
});
