/*
 * interface sortables - http://www.eyecon.ro/interface/
 *
 * Copyright (c) 2006 Stefan Petre
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

$.tooltipHelper = null;
$.tooltipCur = null;
$.tooltipShowF = function(e)
{
	this.ttFocused = true;
	$.tooltipShow(e, this);
};
$.tooltipShow = function(e, el)
{
	if (!el) {
		el = this;
	}
	if ($.tooltipCur != null)
		return ;
	
	$.tooltipCur = el;
	pos = $.getPos(el);
	tt = $.attr(el,'title');
	ah = $.attr(el,'href');
	if (tt && !el.tE.onT) {
		el.tE.tt = tt;
		$(el).set('title','');
		$.tooltipHelper.empty()
			.append('<strong>' + tt + '</strong>' + (ah ? '<br />' + ah : ''))
			.css(
				{
					top: 	pos.y + pos.h + 10 + 'px',
					left:	pos.x + 'px',
					display: 'block'
				}
			);
		if(el.tE.ttClass){
			$.tooltipHelper.addClass(el.tE.ttClass);
		}
	}
};
$.tooltipHideF = function(e)
{
	if ($.tooltipCur != this)
		return ;
	this.ttFocused = false;
	$.tooltipHide(e, this);
};
$.tooltipHide = function(e, el)
{
	if (!el) {
		el = this;
	}
	if (el.ttFocused != true && $.tooltipCur == el) {
		$.tooltipCur = null;
		$.tooltipHelper.css('display','none');
		if(el.tE.ttClass){
			$.tooltipHelper.removeClass(el.tE.ttClass);
		}
		$(el).set('title',el.tE.tt);
	}
};
$.fn.ToolTip = function(c, o)
{
	if (!$.tooltipHelper)
	{
		$('body').append('<div id="tooltipHelper"></div>');
		$.tooltipHelper = $('#tooltipHelper');
		$.tooltipHelper.css(
			{
				position:	'absolute',
				zIndex:		3000,
				display: 	'none'
			}
		);
	}
	return this.each(
		function(){
			if($.attr(this,'title')) {
				var el = $(this);
				if (c && c.constructor == String) {
					this.ttClass = c;
				}
				el.bind(
					'mouseover',
					$.tooltipShow
				);
				el.bind(
					'focus',
					$.tooltipShowF
				);
				el.bind(
					'mouseout',
					$.tooltipHide
				);
				el.bind(
					'blur',
					$.tooltipHideF
				);
				this.ttFocused = false;
				el.get(0).tE = this;
			}
		}
	);
};