/**
 * JQuery Tooltip plugin
 * Version 1.1
 * Date 25.07.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: null,
	curPos: null,
	current: null,

	show : function(e, el)
	{
		if (!el) el = this;
		$.iTooltip.current = 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();
			$.iTooltip.mousemove(e);
			$.iTooltip.helper.show();

			$(el).bind('mouseout', $.iTooltip.hide).bind('mousemove', $.iTooltip.mousemove);
		}
	},

	mousemove : function(e)
	{
		var de = document.documentElement, db = document.body;
		if (e) $.iTooltip.curPos =
				{
					x: e.pageX || (e.clientX + (de.scrollLeft || db.scrollLeft)),
					y: e.pageY || (e.clientY + (de.scrollTop || db.scrollTop))
				};
		var clientSize =
			{
				t: (de && de.scrollTop) || (db && db.scrollTop),
				l: (de && de.scrollLeft) || (db && db.scrollLeft),
				iw: (db && db.clientWidth) || (de && de.clientWidth),
				ih: (db && db.clientHeight) || (de && de.clientHeight)
			};

		var dstyle = $.iTooltip.helper.css('display');
		if (dstyle == 'none') $.iTooltip.helper.css({visibility: 'hidden', left: '0px', top: '0px'}).show();
		var helperSize = {wb: $.iTooltip.helper[0].offsetWidth, hb: $.iTooltip.helper[0].offsetHeight};
		if (dstyle == 'none') $.iTooltip.helper.css({visibility: ''}).hide();

		var pleft = $.iTooltip.curPos.x + 15;
		if (clientSize.iw + clientSize.l - pleft <= helperSize.wb) pleft = clientSize.iw + clientSize.l - helperSize.wb;

		var ptop = $.iTooltip.curPos.y + 15;
		if (clientSize.ih + clientSize.t - ptop - 20 < helperSize.hb) ptop -= 20 + helperSize.hb;

		$.iTooltip.helper.css({top: ptop + 'px', left: pleft + 'px'});
	},

	hide : function(e, el)
	{
		if (!el) el = this;
		$.iTooltip.current = 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.current == el) $.iTooltip.show(null, el);
			},
			error: function() {
				el.ttCFG.loadStatus = 0;
			}
		});
	}
};

})(jQuery);