mad_tooltip = function ()
{
	this.self = this;
	this.text = '';

	this.tooltip = document.createElement('div');
	this.tooltip.setAttribute('id','tooltip');
	this.tooltip.style.position = 'absolute';
	this.tooltip.style.visibility = 'hidden';
	this.tooltip.style.backgroundColor = 'lightyellow';
	this.tooltip.style.border = '1px solid black';
	this.tooltip.style.width  = '200px';
	this.tooltip.style.padding = '3px';

	this.gliph = document.createElement('img');
	this.gliph.setAttribute('src','../../cms/gui_wrapper.php?s=img&l=tooltip_gliph.gif');
	this.gliph.style.position = 'absolute';
	this.gliph.style.marginTop = '19px';
	this.gliph.style.left = '185px';
}

mad_tooltip.prototype.shoot = function()
{
	obj = this.self;

	if(!document.getElementById || !document.getElementsByTagName) return;

	this.text = this.trigger.getAttribute("title");

	if(this.text == null || this.text.length == 0) return;

	this.trigger.removeAttribute('title');
	this.trigger.setAttribute('title', '');
	this.trigger.onmouseout  = function () { obj.hideTooltip(); };

	var origin = this.getPointOffset(this.trigger);

	document.body.appendChild(this.tooltip);

	this.tooltip.innerHTML = this.text;
	this.tooltip.style.top = (origin.y - this.tooltip.offsetHeight-16)+'px';
	this.tooltip.style.left = (origin.x-190)+'px';
	this.tooltip.appendChild(this.gliph);
	this.showHideSelects ('hidden');

	this.tooltip.style.visibility = 'visible';
}

showtooltip = function(trigger)
{
	tooltip.trigger = trigger;
	tooltip.shoot ();
}

mad_tooltip.prototype.showHideSelects = function(state)
{
	if(!document.all) return;

	var selects = document.getElementsByTagName('SELECT');
	var tOrigin = this.getPointOffset(this.tooltip);
	var absH, sOrigin, sBtm;

	for ( a in selects)
	{
		sOrigin = this.getPointOffset(selects[a]);

		sBtm = sOrigin.y + selects[a].offsetHeight;

		absH = Math.abs(sBtm - tOrigin.y)

		if ( ( tOrigin.y - 16 ) < sOrigin.y && absH < (selects[a].offsetHeight + this.tooltip.offsetHeight) )
		{
			selects[a].style.visibility = state;
		}
	}
}

mad_tooltip.prototype.hideTooltip = function  ()
{
	this.tooltip.style.visibility = 'hidden';
	this.showHideSelects ('visible');
	this.trigger.setAttribute('title', this.text);

	document.getElementsByTagName("body")[0].removeChild(this.tooltip);
}

mad_tooltip.prototype.getPointOffset = function (obj)
{
	var point = { x: 0, y: 0 };
	while (obj)
	{
		point.x += obj.offsetLeft;
		point.y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return point;
}

var tooltip = new mad_tooltip;