//____________________________________________________________________
//
// bmejs_tooltip.php - BME JavaScript Tooltip Display Utility
// Copyright (C) 2002-2011 by Manfred Baumeister, Dublin, Ireland
//____________________________________________________________________
//
// requires bmejs.php
//
// Usage:
//
//	onmouseover="bme_TooltipShow('tooltipid');"
//	onmouseout="bme_TooltipHide();"
//
//____________________________________________________________________

//____________________________________________________________________
// Globals

// Tooltip customization
var currenttooltip	= null;		// Current tooltip
var offsetxpoint	= -20;		// Customize tooltip x offset
var offsetypoint	= 12;		// Customize tooltip y offset

// Browser sensing
var ie				= document.all;
var ns6				= document.getElementById && !document.all;

// Debugging
var _doDebug		= false;

//____________________________________________________________________
// Utility functions

function _getIeBody()
{
	return (document.compatMode && document.compatMode != 'BackCompat')
		? document.documentElement
		: document.body
		;
}

//____________________________________________________________________
// Tooltip functions

function bme_TooltipShow(
	ttid,		// Tooltip id
	ttbgcolor,	// Tooltip background color
	ttwidth		// Tooltip width
	)
{
	if (_doDebug)
	{
		alert('bme_TooltipShow: "' + ttid + '"');
	}
	if (!currenttooltip)
	{
		currenttooltip = bmeDOM_getObject(ttid);
	}
	if (!currenttooltip)
	{
		if (_doDebug)
		{
//			alert('bme_TooltipShow: object "' + ttid + '" not found!');
		}
		return false;
	}
	if (1)	//ns6 || ie)	//##??
	{
		if (_doDebug)
		{
//			alert('bme_TooltipShow: "' + ttid + '" mods...');
		}
/*
		// Set tooltip width
		if (typeof ttwidth != 'undefined')
		{
//			currenttooltip.style.width = ttwidth + 'px';
		}
		// Set tooltip background color
		if (typeof ttbgcolor != 'undefined' && ttbgcolor != '')
		{
//			currenttooltip.style.backgroundColor = ttbgcolor;
		}
		// Set tooltip content
//		currenttooltip.innerHTML		= ttid;
*/
		// Show tooltip
		currenttooltip.style.position	= 'absolute';
		currenttooltip.style.visibility	= 'visible';
		currenttooltip.style.display	= 'block';
		// Activate tooltip mouse interaction
		document.onmousemove = bme_TooltipMove;
		// Done
		return false;
	}
}

function bme_TooltipMove(
	e	// Event
	)
{
	if (_doDebug)
	{
//		alert('bme_TooltipMove');
	}
	if (currenttooltip)
	{
		// Get mouse position
		var curX	= ns6 ? e.pageX : event.clientX + _getIeBody().scrollLeft;
		var curY	= ns6 ? e.pageY : event.clientY + _getIeBody().scrollTop;
		// Get distance mouse to window edges
		var rightedge	= ie && !window.opera
			? _getIeBody().clientWidth - event.clientX - offsetxpoint
			: window.innerWidth - e.clientX - offsetxpoint - 10
			;
		var bottomedge	= ie && !window.opera
			? _getIeBody().clientHeight - event.clientY - offsetypoint
			: window.innerHeight - e.clientY - offsetypoint - 10
			;
		var leftedge	= (offsetxpoint < 0) ? offsetxpoint * (-1) : -1000;
		// Set horizontal tooltip position
		if (rightedge < currenttooltip.offsetWidth)
		{
			// Move tooltip to left by its width
			currenttooltip.style.left = ie
				? _getIeBody().scrollLeft + event.clientX - currenttooltip.offsetWidth + 'px'
				: window.pageXOffset + e.clientX - currenttooltip.offsetWidth + 'px'
				;
		}
		else if (curX < leftedge)
		{
			currenttooltip.style.left = '5px';
		}
		else
		{
			// Set horizontal tooltip position to mouse position
			currenttooltip.style.left = curX + offsetxpoint + 'px';
		}
		// Set vertical tooltip position
		if (bottomedge < currenttooltip.offsetHeight)
		{
//			currenttooltip.style.top = ie
//				? _getIeBody().scrollTop + event.clientY - currenttooltip.offsetHeight - offsetypoint + 'px'
//				: window.pageYOffset + e.clientY - currenttooltip.offsetHeight - offsetypoint + 'px'
//				;
			// Move tooltip upwards by its height
			var tt_top;
			var doflip = false;
			if (ie)
			{
				tt_top = _getIeBody().scrollTop + event.clientY - currenttooltip.offsetHeight - offsetypoint;
				if (tt_top < _getIeBody().scrollTop)
				{
					tt_top = _getIeBody().scrollTop;
				}
				else
				{
					doflip = true;
				}
			}
			else
			{
				tt_top = window.pageYOffset + e.clientY - currenttooltip.offsetHeight - offsetypoint;
				if (tt_top < window.pageYOffset)
				{
					tt_top = window.pageYOffset;
				}
				else
				{
					doflip = true;
				}
			}
			if (doflip)
			{
				currenttooltip.style.top = tt_top + 'px';
			}
		}
		else
		{
			// Set vertical tooltip position to mouse position
			currenttooltip.style.top = curY + offsetypoint + "px";
		}
		// Make tooltip visible
		currenttooltip.style.position	= 'absolute';
		currenttooltip.style.visibility = 'visible';
		currenttooltip.style.display	= 'block';
	}
}

function bme_TooltipHide()
{
	if (currenttooltip)
	{
		// Hide tooltip
		currenttooltip.style.visibility	= 'hidden';
		currenttooltip.style.display 	= 'none';
//		currenttooltip.style.position	= 'relative';
		currenttooltip.style.left		= '-1000px';
		// Reset current tooltip		
		currenttooltip = null;
		// Reset mouse event handler		
		document.onmousemove = '';
	}
}

//____________________________________________________________________
// JavaScript code end

