
/**
 *	tooltip.js
 *
 *	by Aleksandar Ruzicic (admin@krcko.net)
 */



// setup
var tt_speed = 20;			// interval (in miliseconds)
var tt_hold = 2;			// wait time (in seconds)


// don't change anything below!

var tt_container = null;
var tt_holder = null;
var tt_width = 200;
var tt_height = 20;
var tt_timer = null;
var tt_wait = null
var tt_delta = 10;

tooltip_init = function(ttc, tth, h)
{
	tt_container = document.getElementById(ttc);
	tt_holder = document.getElementById(tth);

	tt_container.style.position = 'absolute';
	tt_container.style.overflow = 'hidden';
	tt_container.style.textAlign = 'left';
	tt_holder.style.textAlign = 'center';
	tt_height = h;
};

tooltip_set = function()
{
	argc = arguments.length;
	argv = arguments;

	for (var i = 0; i < argc; i += 2)
	{
		var element = document.getElementById(argv[i]);

		if (element)
		{
			element.tooltiptext = argv[i + 1];

			element.onmouseover = function()
			{
				tooltip_reset();

				var pos = getElementPosition(this);

				var left = pos.x;
				var top = pos.y + tt_height;

				
				tt_container.style.top = top + 'px';
				tt_container.style.left = '-500px';
				
				tt_holder.style.width = '';
				tt_container.style.width = '';

				tt_holder.innerHTML = this.tooltiptext;
				tt_holder.style.display = 'block';

				tt_width = tt_holder.offsetWidth + 2;

				tt_holder.style.width = tt_width + 'px';

				if (left + tt_width >= tt_container.offsetParent.offsetWidth)
				{
					left = tt_container.offsetParent.offsetWidth - tt_width - 32;
				}

				tt_width = tt_container.offsetParent.offsetWidth - left - 30;
				
				tt_container.style.width = tt_width + 'px';
				tt_container.style.left = left + 'px';

				tt_holder.style.marginLeft = tt_width + 'px';

				tt_container.style.display = 'block';

				tt_timer = setInterval('tooltip_animate()', tt_speed);
			};
		}
	}
};

tooltip_reset = function()
{
	tt_holder.innerHTML = '';
	tt_holder.style.display = 'none';

	tt_container.style.marginLeft = '0';
	tt_container.style.marginRight = '0';

	if (tt_timer != null)
	{
		clearInterval(tt_timer);
		tt_timer = null;
	}

	if (tt_wait != null)
	{
		clearInterval(tt_wait);
		tt_wait = null;
	}

	tt_delta = 10;
};

tooltip_animate = function()
{
	var newx = parseInt(tt_holder.style.marginLeft) - tt_delta;

	if (newx <= 0) 
	{
		newx = 0;
		clearInterval(tt_timer);
		tt_timer = null;

		tt_wait = setInterval('tooltip_reset()', tt_hold * 1000);
	}

	tt_holder.style.marginLeft = newx + 'px';

	tt_delta += 15;
};


Number.prototype.Val = function()
{
	if (isNaN(this))
	{
		return 0;
	}

	return this;
};

getElementPosition = function(element)
{
	var left = 0;
	var top  = 0;

	do
	{
		left += element.offsetLeft;
		top  += element.offsetTop;

		if (element.currentStyle)
		{
			left += parseInt(element.currentStyle.borderLeftWidth).Val();
			top += parseInt(element.currentStyle.borderTopWidth).Val();
		}

		element = element.offsetParent;
	}
	while (element.offsetParent);

	return	{
				x : left,
				y : top
			};
};