jQuery.fn.autofill = function(_options) {
	var _options = jQuery.extend({
		result_url: 'http://blog.m-designz.nl/tag-search.php?keyword=',
		result_box_class: 'results-box'
	}, _options);
	
	return $(this).each(function(){
		$(this).keyup(function(event){
			var value = $(this).val();
			var names = new Array();
			var urls = new Array();
			var el = $(this);
			
			if (value != "" && event.keyCode != 40 && event.keyCode != 38)
			{
				$.getJSON(_options.result_url+value,
				function(json){
					$.each(json.results, function(i,item){
						names[names.length] = item.name;
						urls[urls.length] = item.url;
					});

					createBox(el,names,urls);
				});
			}
			else
			{	
				//handleKeys(event.keyCode);
				//$("."+ _options.result_box_class).remove();
			}
        });

		var createBox = function(el,names, urls){
			$("."+ _options.result_box_class).remove();
			
			if (names.length > 0)
			{
				var divinput = "<ul>";
				for (var i = 0; i < names.length; i++)
				{
					divinput += "<li><a href=\""+urls[i]+"\">"+names[i]+"</a></li>";
				}
				divinput += "</ul>";
				var inputwidth = el.width();
				var inputheight = el.outerHeight();
				var inputoffset = el.position();
				var topoffset = el.offset().top + inputheight;
				$("body").append("<div id=\"results-box-id\" style=\"position: absolute; z-index: 9999; left: "+el.offset().left+"px; top: "+topoffset+"px; width: "+inputwidth+"\" class=\""+_options.result_box_class+"\">"+divinput+"</div>");
			}
			else
				$("."+ _options.result_box_class).remove();
		};
		
		var handleKeys = function(keycode){
			switch(keycode)
			{
				case 38: moveSelectionUp(); break;
				case 40: moveSelectionDown(); break;
			}
		};
		
		var moveSelectionDown = function(){
			var activeEl = false;
			var counter = 1;
			$("#results-box-id ul li").each(function()
			{
				if ($(this).children("a").hasClass("activeSelection"))
					activeEl = counter;
					
				counter++;
			});
			
			if (activeEl == $("#results-box-id ul li").length)
			{
				activeEl = 0;
				counter = 0;
			}
			if (!activeEl)
			{
				$("#results-box-id ul li a").removeClass("activeSelection");
				$("#results-box-id ul li:first a").addClass("activeSelection");
			}
			else
			{
				$("#results-box-id ul li a").removeClass("activeSelection");
				$("#results-box-id ul li:eq("+activeEl+") a").addClass("activeSelection");
			}
		}
		
		var moveSelectionUp = function(){
			var activeEl = false;
			var counter = 1;
			$("#results-box-id ul li").each(function()
			{
				if ($(this).children("a").hasClass("activeSelection"))
					activeEl = counter - 2;
					
				counter++;
			});	
			if (activeEl < 0)
			{
				activeEl = $("#results-box-id ul li").length - 1;
			}
			if (!activeEl)
			{
				$("#results-box-id ul li a").removeClass("activeSelection");
				$("#results-box-id ul li:first a").addClass("activeSelection");
			}
			else
			{
			
				$("#results-box-id ul li a").removeClass("activeSelection");
				$("#results-box-id ul li:eq("+activeEl+") a").addClass("activeSelection");
			}
		}
	});
}

function clearResults() {
	$("#results-box-id").remove(); 
}
