/**
 * JQuery Tooltip plugin
 * Version 1.2
 * Date 5.10.2007
**/
(function($) {

$.fn.Tooltip = function(options)
{
	if (!$.iTooltip.helper)	{
		$.iTooltip.helper = $('<div id="tooltipHelper"><div id="tooltipTitle"></div><div id="tooltipBody"></div>');
		$('body').append($.iTooltip.helper);
		$.iTooltip.helper.css({ position: 'absolute', zIndex: 3000, display: 'none'});
	}
	return this.each(function() {
		this.ttCFG = $.extend({bodyText: null, bodyUrl: null, className: null, showTitle: null, loadStatus: 0}, options || {});
		$(this).removeAttr('title').bind('mouseover', $.iTooltip.show);
	});
};

$.iTooltip =
{
	helper: 0, curEl: 0, x: 0, y: 0, w: 0, h: 0,

	show : function(e, el)
	{
		if (!el) el = this;
		$.iTooltip.curEl = el;

		var tTitle = null;
		var tBody = el.ttCFG.bodyText;

		if (el.ttCFG.showTitle && tBody && tBody.indexOf(el.ttCFG.showTitle) != -1) 
		{
			tTitle = tBody.substr(0, tBody.indexOf(el.ttCFG.showTitle));
			tBody = tBody.substr(tBody.indexOf(el.ttCFG.showTitle) + el.ttCFG.showTitle.length);
		}

		if (el.ttCFG.bodyUrl && el.ttCFG.loadStatus == 0) $.iTooltip.load(el);

		if (tBody || el.ttCFG.bodyUrl) 
		{
			$.iTooltip.helper.addClass(el.ttCFG.className);
			var lClass = el.ttCFG.className ? el.ttCFG.className + '-load' : null;
			if (el.ttCFG.loadStatus == 1)
				$.iTooltip.helper.addClass(lClass);
			else
				$.iTooltip.helper.removeClass(lClass);

			$('#tooltipBody', $.iTooltip.helper).html(tBody ? tBody : '');
			$('#tooltipTitle', $.iTooltip.helper).html(tTitle ? tTitle : '');

			$.iTooltip.helper.hide();

			var s = $.iTooltip.helper.css('display') == "none";
			if (s) $.iTooltip.helper.css({visibility: 'hidden', left: '0px', top: '0px'}).show();
			$.iTooltip.w = $.iTooltip.helper[0].offsetWidth;
			$.iTooltip.h = $.iTooltip.helper[0].offsetHeight;
			if (s) $.iTooltip.helper.css({visibility: ''}).hide();

			$.iTooltip.mousemove(e);
			$.iTooltip.helper.show();
			$(el).bind('mouseout', $.iTooltip.hide).bind('mousemove', $.iTooltip.mousemove);
		}
	},

	mousemove : function(e)
	{
		if (e) 
		{
			$.iTooltip.x = e.pageX;
			$.iTooltip.y = e.pageY;
		}
		var d = document.documentElement, b = document.body;
		var cs = {t: (d && d.scrollTop) || (b && b.scrollTop), l: (d && d.scrollLeft) || (b && b.scrollLeft),
				w: (b && b.clientWidth) || (d && d.clientWidth), h: (b && b.clientHeight) || (d && d.clientHeight)};

		var l = $.iTooltip.x + 15, t = $.iTooltip.y + 15;
		if (cs.w + cs.l - l <= $.iTooltip.w) l = cs.w + cs.l - $.iTooltip.w;
		if (cs.h + cs.t - t - 20 < $.iTooltip.h) t -= 20 + $.iTooltip.h;
		$.iTooltip.helper.css({top: t + 'px', left: l + 'px'});
	},

	hide : function(e, el)
	{
		if (!el) el = this;
		$.iTooltip.curEl = null;
		$.iTooltip.helper.hide();
		$(el).unbind('mouseout', $.iTooltip.hide).unbind('mousemove', $.iTooltip.mousemove);
	},

	load : function(el)
	{
		el.ttCFG.loadStatus = 1;
		$.ajax({
			type: "GET",
			url: el.ttCFG.bodyUrl,
			success: function(data) 
			{
				el.ttCFG.loadStatus = 2;
				el.ttCFG.bodyText = data;
				if ($.iTooltip.curEl == el) $.iTooltip.show(null, el);
			},
			error: function() 
			{
				el.ttCFG.loadStatus = 0;
			}
		});
	}
};

})(jQuery);