var maxWidth = 600;

var ttdiv;
var inTooltip = false;
var tipWidth = 0;
var widthKludge = 0;
var mouse_x;
var mouse_y;
var mouse_win_x;
var windowWidth;
var leftPos;
var origTitle = '';
var currentEl;

function tip(el)
{
	currentEl = el;
	ttdiv.innerHTML = origTitle = el.title;
	el.title = '' ;
	inTooltip = true;
	
	if (document.all) killChildAlts(el); // damn you, IE
}

function untip()
{
	ttdiv.style.width = 'auto';
	ttdiv.style.display = 'none';
	ttdiv.style.left = 0; // needed for recalc of width
	inTooltip = false;
	tipWidth = 0;
	widthKludge = 0;
	currentEl.title = origTitle;
}

function doTip(e)
{
	if (!inTooltip) return;
	if (!e) var e = window.event;

	if (window.innerWidth)
	{
		windowWidth = window.innerWidth;
		widthKludge = 10; //damn you again, IE
	}
	else if ((document.documentElement.clientHeight) && (document.documentElement.clientHeight) != 0)
	{
		windowWidth = document.documentElement.clientWidth;
	}
	else
	{
		windowWidth = document.body.clientWidth;
	}

	ttdiv.style.display = 'block';

	getMousePos(e);

	if (!tipWidth)
	{
		tipWidth = ttdiv.offsetWidth;

		if (tipWidth > maxWidth)
		{
			tipWidth = ttdiv.style.width = maxWidth;
		}
		else
		{
			ttdiv.style.width = tipWidth;
		}
	}

	if ((mouse_win_x + tipWidth + widthKludge + 25) > windowWidth)
	{
		leftPos = windowWidth - tipWidth - widthKludge - 25;
	}
	else
	{
		leftPos = mouse_x;
	}

	ttdiv.style.left = (leftPos + 5) + 'px';
	ttdiv.style.top = (mouse_y + 20) + 'px';
}

function getMousePos(e)
{
	if (e.pageX)
	{
		mouse_x = e.pageX;
		mouse_y = e.pageY;
	}
	else if (e.clientX)
	{
		mouse_x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		mouse_y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}

	mouse_win_x = e.clientX;
}

if (window.addEventListener)
{
  window.addEventListener('mousemove', doTip, false);
}
else if (document.attachEvent)
{
  document.attachEvent('onmousemove', doTip);
}

function killChildAlts(el)
{
	if (el.childNodes.length == 0) return;
	
	for( var i = 0; i < el.childNodes.length; i++ )
	{
		if (el.childNodes[i].alt) el.childNodes[i].alt = '';
	}
}