﻿// Dominic Winsor, Design Haus, www.dhaus.com
// script to provide the dynamically positioned tooltip
var focusTooltip = '';
var focusElement = '';
var focusEnabled = false;
var focusDelay   = 1000;
var focusTimeout = 4000;

// put focus on an element on the page, optionally add a tooltip
function initFocus(element, message, delay, timeout )
{
    if (delay) focusDelay = delay;
    if (timeout) focusTimeout = timeout;
    if (message) focusTooltip = '<strong>Tip</strong>: '+message;
    focusElement = element;
    focusEnabled = true;
}

// focus on the element and present the tooltip
function doFocus()
{
    try 
	{
	    el = document.getElementById(focusElement);
	    el.focus();
	    if (focusTooltip.length>0)
	    {
	        setTimeout("doTooltip(focusElement,focusTooltip)",focusDelay);
	        setTimeout("document.getElementById('tooltip').style.display='none'",focusTimeout);
	    }
	}
	catch(exception) {	}
}

function doTooltip(el, message)
{
	var tooltip = document.getElementById('tooltip');
	position = getElementPosition(el);
	var x,y;
	x = position[0];
	y = position[1]+2;
	tooltip.innerHTML = message;
	tooltip.style.left = x + 'px';
	tooltip.style.top = y + 'px';
	tooltip.style.display = 'block';
}

/// <summary>
/// return the coordinates from the top left (0,0) of an element
/// </summary>
/// <returns>A 2-element array containing [x,y] coords</returns>
function getElementPosition(el)
{
    el = document.getElementById(el);
    
    // position the container div (by walking up the DOM and summing all offset positions.)
	var x = 0;
	var y = el.offsetHeight;
	while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
	{
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}
	x += el.offsetLeft;
	y += el.offsetTop;
	return new Array(x,y);
}

