(function($) {

var methods = {
	init: function(options) {
		var settings = {}
		
		return this.each(function() {
			var $this = $(this);

			if (typeof options == 'string') {
				$.extend(settings, {text: options});
			} else {
				if (options) {
					$.extend(settings, options);
				}
			}

			var text = typeof(settings.text) != 'undefined'? settings.text: $this.attr('title');
			var html = settings.html;
			
			var $table = $(
				'<table class="jquery-tooltip" border="0" cellpadding="0" cellspacing="0">\n' +
				'	<tr><td class="tl">&nbsp;</td><td class="tc">&nbsp;</td><td class="tr">&nbsp;</td></tr>\n' +
				'	<tr><td class="ml">&nbsp;</td><td class="mc">&nbsp;</td><td class="mr">&nbsp;</td></tr>\n' +
				'	<tr><td class="bl">&nbsp;</td><td class="bc">&nbsp;</td><td class="br">&nbsp;</td></tr>\n' +
				'</table>');

			$table.css({position: 'absolute', top: 0, left: 0});
			$('.tl', $table).css({background: 'transparent url(images/jquery.tooltip/tl.png)', width: '19px', height: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.tc', $table).css({background: 'transparent url(images/jquery.tooltip/tc.png) repeat-x top', height: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.tr', $table).css({background: 'transparent url(images/jquery.tooltip/tr.png)', width: '19px', height: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.ml', $table).css({background: 'transparent url(images/jquery.tooltip/ml.png) repeat-y', width: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.mc', $table).css({background: 'transparent url(images/jquery.tooltip/mc.png)', font: "100% Georgia, Times, 'Times New Roman', Serif", 'font-size': '12px', color: '#FFF'});
			$('.mr', $table).css({background: 'transparent url(images/jquery.tooltip/mr.png) repeat-y', width: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.bl', $table).css({background: 'transparent url(images/jquery.tooltip/bl.png)', width: '19px', height: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.bc', $table).css({background: 'transparent url(images/jquery.tooltip/bc.png) repeat-x', height: '19px', 'font-size': '1px', 'line-height': '1px'});
			$('.br', $table).css({background: 'transparent url(images/jquery.tooltip/br.png)', width: '19px', height: '19px', 'font-size': '1px', 'line-height': '1px'});

			if (typeof html != 'undefined') {
				$('.mc', $table).html(html);
			} else {
				$('.mc', $table).text(text);
			}
			
			$('body').append($table);
			$table.hide();

			var targetWidth = $this.width();
			var targetHeight = $this.height();
			$(document).mousemove(function(e) {
				var offset = $this.offset();
				if ((e.pageY < offset.top) || (offset.top + targetHeight < e.pageY) || (e.pageX < offset.left) || (offset.left + targetWidth < e.pageX)) {
					$table.hide();
				} else {
					$table.offset({left: Math.max(offset.left, e.pageX - $table.width() + 9), top: Math.max(offset.top, e.pageY - $table.height() + 9)});
					$table.show();
				}
			});
			
			$this.attr('title', '');
		});
	}
}

$.fn.tooltip = function(method) {
	// Method calling logic
	if (methods[method]) {
		return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
	} else if (typeof method === 'object' || !method) {
		return methods.init.apply(this, arguments);
	} else {
		$.error('Method ' +  method + ' does not exist on jQuery.tooltip');
	}
}

})(jQuery);
