/*******************************************************
ONFOCUS TOOLTIPS
(c) 2003, Brothercake
http://www.brothercake.com
CSS Modifications by Ryan Parman, www.skyzyx.com
*******************************************************/
// Global variables
var i, pos, obj, tempObj, tempEle, winSize, extent, scrollHeight;

// Tooltip Object
var toolTip = null;
var toolTipParent = null;

// Find object position
function getRealPos(ele, dir)
{
	(dir=="x") ? pos = ele.offsetLeft : pos = ele.offsetTop;
	tempEle = ele.offsetParent;
	while (tempEle != null)
	{
		pos += (dir=="x") ? tempEle.offsetLeft : tempEle.offsetTop;
		tempEle = tempEle.offsetParent;
	}
	return pos;
}

// Delay Timer
var goTip = false;
var goTimer = null;

function focusTimer(e)
{
	if (goTimer != null) // Second loop
	{
		// Clear timer
		clearInterval(goTimer);
		goTimer = null;

		// Pass object to create tooltip
		focusTip(e);
	}
	else // First loop
	{
		// Get focused object
		(e) ? obj = e.target : obj = event.srcElement;

		// Pass object back through timer
		tempObj = obj;

		// Set interval
		goTimer = setInterval('focusTimer(tempObj)',400);
	}
}

// Create tooltip
function focusTip(obj)
{
	// Remove any existing tooltip
	blurTip();

	if (toolTip == null) // If tooltip is null
	{
		// Get window dimensions
		if (typeof window.innerWidth!="undefined")
		{
			winSize = {
				x : window.innerWidth,
				y : window.innerHeight
			};
		}
		else if (typeof document.documentElement.offsetWidth!="undefined")
		{
			winSize = {
				x : document.documentElement.offsetWidth,
				y : document.documentElement.offsetHeight
			};
		}
		else 
		{
			winSize = {
				x : document.body.offsetWidth,
				y : document.body.offsetHeight
			};
		}

		// Create toolTip
		toolTip = document.createElement('div');

		// Add classname
		toolTip.setAttribute('class', '');
		toolTip.className = (obj.className == 'youAreHere') ? 'heretooltip' : 'tooltip';

		// Get focused object coordinates
		if (toolTipParent == null)
		{
			toolTipParent = {
				x : getRealPos(obj, 'x') - 3,
				y : getRealPos(obj, 'y') + 2
			};
		}

		// Offset tooltip from object
		toolTipParent.y += obj.offsetHeight;

		// Apply tooltip position
		toolTip.style.left = toolTipParent.x + 'px';
		toolTip.style.top = toolTipParent.y + 'px';

		// Apply CSS Styles from Brothercake's demo with JavaScript instead of native CSS to ensure minimal necessary external configuration.
		// (Added by Ryan Parman, www.skyzyx.com)
		toolTip.style.fontSize='8pt';
		toolTip.style.fontFamily='tahoma, verdana, helvetica, arial, sans-serif';
		toolTip.style.border='1px solid #000';
		toolTip.style.backgroundColor='#FFFFE1';
		toolTip.style.color='#000000';
		toolTip.style.paddingLeft='4px';
		toolTip.style.paddingRight='4px';
		toolTip.style.paddingTop='2px';
		toolTip.style.paddingBottom='2px';
		toolTip.style.textAlign='left';
		toolTip.style.position='absolute';
		toolTip.style.width='auto';
		toolTip.style.height='auto';

		// Write in title attribute (with 'you are here' string)
		toolTip.innerHTML = (obj.className == 'youAreHere') ? obj.title + ' <b>[You Are Here]</b>' : obj.title;

		// Add to document
		document.body.appendChild(toolTip);

		// Restrict width
		if (toolTip.offsetWidth > 300) toolTip.style.width = '300px';

		// Get tooltip extent
		extent = {
			x : toolTip.offsetWidth,
			y : toolTip.offsetHeight
		};

		// If tooltip exceeds window width...
		if ((toolTipParent.x + extent.x) >= winSize.x)
		{
			//shift tooltip left
			toolTipParent.x -= extent.x;
			toolTip.style.left = toolTipParent.x + 'px';
		}
		
		// Get scroll height
		if (typeof window.pageYOffset!="undefined")
		{
			scrollHeight = window.pageYOffset;
		}
		else if (typeof document.documentElement.scrollTop!="undefined")
		{
			scrollHeight = document.documentElement.scrollTop;
		}
		else 
		{
			scrollHeight = document.body.scrollTop;
		}

		// If tooltip exceeds window height
		if ((toolTipParent.y + extent.y) >= (winSize.y + scrollHeight))
		{
			//shift tooltip up
			toolTipParent.y -= (extent.y+obj.offsetHeight+4);
			toolTip.style.top = toolTipParent.y + 'px';
		}
	}
}

function blurTip()
{
	// If tooltip exists
	if (toolTip != null)
	{
		// Remove and nullify tooltip
		document.body.removeChild(toolTip);
		toolTip = null;
		toolTipParent = null;
	}

	// Cancel timer
	clearInterval(goTimer);
	goTimer = null;
}

window.onload = function()
{
	if (typeof document.getElementsByTagName!="undefined")
	{
		// Get tags collection
		var allTags = document.getElementsByTagName('*');
		var allTagsLen = allTags.length;
	
		for (var i=0;i<allTagsLen;i++)
		{
			// If tag has title attribute
			if (allTags[i].title)
			{
				//attach event
				allTags[i].onfocus = focusTimer;
				allTags[i].onblur = blurTip;
				allTags[i].onmouseover = blurTip;
			}
		}


	}
}
