(function($){ 
	var compliterTagTimer;
	var forceClose = true;
	
	$.prototype.ajaxCompliter = function() {
		this.each(function() {
			var input	= $( this ),
				hash	= Math.floor( Math.random() * 9999999 );
			
			input.attr('autocomplete', 'off');	
			
			if( $('#compliter_' + hash).length )
				$('#compliter_' + hash).remove();
			
			var table		= $('<table id="compliter_' + hash + '" class="compliter" style="width:200px;display:none"><tbody></tbody></table>');
			
			$(document.body).append( table );
			
			input.blur(function(){
				if( $.browser.safari ) {
					window['setTimeout'](function() {
						table.html( '<tbody></tbody>' ).hide();
					}, 500);
					forceClose = true;
				}
			});
			
			input.keypress(function(e) {
				var offset_top	= input.get(0).offsetTop,
					offset_left	= input.get(0).offsetLeft,
					parent		= input.get(0).offsetParent;

				while( parent && parent.nodeType == 1 ) { 
					offset_top	+= parent.offsetTop;
					offset_left	+= parent.offsetLeft;

					parent = parent.offsetParent;
				}
				
				table.css({
					top: (offset_top + input.height() + 4) + 'px',
					left: offset_left + 'px',
					position: 'absolute'
				});	
			
				if(e.keyCode == 9) {
					table.html('<tbody></tbody>').hide();
					return;
				}
			
				if(e.keyCode == 13) {
					table.html('<tbody></tbody>').hide();
					return false;
				}

				if( e.keyCode == 40 && table.find('td').length > 0) {
					var selected = table.find('td.selected');
					if(selected.length == 0) {
						table.find('td:first').addClass('selected');
						input.val(table.find('td:first').addClass('selected').html());
					} else {
						if(selected.get(0).parentNode.nextSibling) {
							selected.removeClass('selected');
							selected.get(0).parentNode.nextSibling.firstChild.className = 'selected';
							input.val(selected.get(0).parentNode.nextSibling.firstChild.innerHTML);
						}
					}
					return false;
				}
				
				if( e.keyCode == 38 && table.find('td').length > 0) {
					var selected = table.find('td.selected');
					if(selected.length == 0) {
						input.val(table.find('td:last').addClass('selected').html());
					} else {
						if(selected.get(0).parentNode.previousSibling) {
							selected.removeClass('selected');
							selected.get(0).parentNode.previousSibling.firstChild.className = 'selected';
							input.val(selected.get(0).parentNode.previousSibling.firstChild.innerHTML);
						}
					}
					return false;
				}
			
				if(e.keyCode != 9 && e.keyCode != 13 && e.keyCode != 38) {
					if(compliterTagTimer) 
						window.clearTimeout(compliterTagTimer);
				
						compliterTagTimer = window.setTimeout(function() {
						
						if(input.val() == '' || forceClose ) {
							table.html('<tbody></tbody>').hide();
							forceClose = false;
							return false;
						}
				
						table.html('<tbody></tbody>').hide();

						$.getJSON('/search/ajax/tags/', {'query': input.val()}, function( data ) {
							if( !data )
								return;
								
							if( data.length == 1 ) {
								data[0].tag == input.val();
								return;
							}
							
							var html = '<tbody>';
							for(var i in data) {
								html += '<tr><td align="left" nowrap="nowrap" style="width: 200px; overflow: hidden; cursor:pointer">' + data[i].tag +'</td></tr>';
							}
							html += '</tbody>';

							table.html( html ).show().find('td').each(function() {
								var el = $(this);
								el.click(function() {
									input.val( $(this).text() );
									table.html('<tbody></tbody>').hide();
								});
							});
						});
					}, 300);
				}
			});
		});
		
		$(document.body).click(function( e ) {
			if( e.target.className != 'compliter' )
				$('table.compliter').html('<tbody></tbody>').hide();
		});
	}
	
})(jQuery);