(function($) {
	$.fn.extend({
		
		aTOOLTIP: function(options) {
		
			var defaults = {
				follow: true,
				fadeDuration: 100,
				top: 6,
				left: 6,
				tip: false
			}
			
			var options = $.extend(defaults, options);
			
			var $container = $("<div class=\"aTOOLTIP-container\" style=\"position:absolute;z-index:9999;display:none;\">\
								</div>");
			var $tipholder = $("<div class=\"aTOOLTIP-tip\">\
								</div>");
			$container.append($tipholder);
			$("body").append($container);
			
			return this.each(function(){
				
				var $this = $(this);
				
//				if(typeof($this.data('tooltip')) !== 'undefined') {
					$this.bind('mouseenter',function(){
						$container.fadeIn(options.fadeDuration);
						if(options.live) {
							var interval = setInterval(render($this),50);
							$this.data('interval', interval);
						} else {
							render($this);
						}					
					}).bind('mouseleave mousedown',function(){
						clearInterval($this.data('interval'));
						$container.fadeOut(0);
					});
//				}
				
			});
			
			function render(obj) {
				var $this = obj;
				var offset = $this.offset();
				var tip = (options.tip == false) ? $this.data('tooltip') : options.tip;
				$tipholder.html(tip);
				y = offset.top - $container.height() - options.top;
				x = offset.left + options.left;
				$container.css({
					top:y,
					left:x
				});
			}
			
		}
		
	});
	$("[data-tooltip]").aTOOLTIP({top:-10, left:-24});
})(jQuery);
