(function($) {
	$.fn.tooltipx = function(options, callback) {
		if ( $.isFunction(options) ) {
			options = null;
		}

		/*$('body').live('click', function(e){
			if ( $(e.target).parents('.tooltipx').length == 0 && $('.tooltipx').is(':visible') ) {
				$('.tooltipx').hide();
			}
		});*/

		if ( $('.tooltipx').length == 0 ) {
			$('<div><div><a href="#"></a></div><div></div><div></div></div>')
				.addClass('tooltipx')
				.appendTo('body')
				.find(':first')
					.addClass('tlx-head')
					.next()
						.addClass('tlx-body')
						.next()
							.addClass('tlx-bott');
		}

		options = $.extend($.fn.tooltipx.defaults, options);
		
		return this.each(function() {
			var $th = $(this);
			
			if ( $th.find(':first').length == 0 || $th.find(':first').html() == '' ) {
				return;
			}
			
			$th
				.hover(function(e) {
					var pageX = $th.offset().left,
						pageY = $th.offset().top;

					e.preventDefault();

					setTimeout(function(){
						$('.tooltipx')
							.show(0, function(){
								setTimeout(function(){
									$('.tooltipx')
										.css({
											top : pageY - $('.tooltipx').outerHeight(),
											left : pageX - $('.tooltipx').outerWidth() + 14
										});
								}, 10);
							})
							.find('.tlx-body')
								.html($th.find(':first').html())
								.prev()
									.find('a')
										.click(function(e){
											e.preventDefault();
											$('.tooltipx').hide().data('visible', false);
										});
					}, 10);
				}, function() {
					$('.tooltipx').hide();	
				});
		});
	};
})(jQuery);
