/* Copyright (c) Matej Svejda 2006

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

/* If you use showTooltip('text', 'This is the tooltiptext') you need to have
<div id="tooltip" style="position:absolute;visibility:hidden;z-index:100">&nbsp;</div> somewhere in your (X)HTML code. */

// ----- constants you might want to edit ---------
var offsetX = 15;
var offsetY = 15;
var defaultTolltipId = "tooltip";
// ------------------------------------------------


var tooltip = null; // pointer to tooltip
var tooltipVisible = false; // indicates whater tooltip is visible
var tooltipWidth = 0, tooltipHeight = 0; // dimensions of the tooltip box

var normalBrowser = false;
var ie = false;
var ieStrict = false;

var windowInnerWidth, windowInnerHeight; // inner dimensions of the browser window

// Init
document.onmousemove = moveTooltip;

if(document.all || navigator.userAgent.indexOf('MSIE 5') > 0 || navigator.userAgent.indexOf('MSIE 6') > 0) { // Some kind of Intenet Explorer
	if(document.documentElement && document.documentElement.clientHeight) { // Internet Explorer 6 Strict Mode
		ieStrict = true;
	} else {
		ie = true;
	}
} else {
	normalBrowser = true;
}

function getInnerWindowDimensions()
{
	if(normalBrowser) { // all browsers but Internet Explorer
		windowInnerWidth = self.innerWidth + window.pageXOffset;
		windowInnerHeight = self.innerHeight + window.pageYOffset;
	} else if(ieStrict) { // Internet Explorer 6 Strict Mode
		windowInnerWidth  = document.documentElement.clientWidth + document.documentElement.scrollLeft;
		windowInnerHeight = document.documentElement.clientHeight + document.documentElement.scrollTop;
	} else if(ie) { // all other Internet Explorers versions
		windowInnerWidth  = document.body.clientWidth + document.body.scrollLeft;
		windowInnerHeight = document.body.clientHeight + document.body.scrollTop;
	}
}

function showTooltip(mode, arg)
{
	if(mode == 'id') { // a costum element is used as tooltip with the id = arg
		tooltip == document.getElementById(arg);
	} else if(mode = 'text') { // the default element is used as tooltip and is filled with text = arg
		tooltip = document.getElementById(defaultTolltipId);
		tooltip.firstChild.nodeValue = arg;
	}
	
	if(tooltip) {
		tooltipWidth = tooltip.offsetWidth;
		tooltipHeight = tooltip.offsetHeight;
	}
}

function hideTooltip()
{
	if(tooltip) {
		_setVisibility(tooltip, false);
	}
	tooltip = null;
	tooltipVisible = false;
}

function moveTooltip(costumEvent)
{
	if(!tooltip) {
		return;
	}
	
	getInnerWindowDimensions();
	
	if(costumEvent && !window.opera) {
		x = costumEvent.pageX ? costumEvent.pageX : costumEvent.clientX ? costumEvent.clientX : 0;
		y = costumEvent.pageY ? costumEvent.pageY : costumEvent.clientY ? costumEvent.clientY : 0;
	} else if(event) {
		x = event.clientX;
		y = event.clientY;
	}
	
	if(ieStrict) {
		x += document.documentElement.scrollLeft;
		y += document.documentElement.scrollTop;
	} else if(ie) {
		x += document.body.scrollLeft;
		y += document.body.scrollTop;
	}
	
	x += offsetX;
	y += offsetY;
	
	if(x + tooltipWidth > windowInnerWidth) {
		x -= tooltipWidth + 2 * offsetX;
	}
	if(y + tooltipHeight > windowInnerHeight) {
		y -= tooltipHeight + offsetY;
	}
	
	tooltip.style.left = x + 'px';
	tooltip.style.top  = y + 'px';
	if(!tooltipVisible) {
		_setVisibility(tooltip, true);
		tooltipVisible = true;
	}
}

function _setVisibility(object, visible)
{
	if(document.layers) {
		object.visibility  = (visible == true) ? 'show' : 'hide';
	} else {
		object.style.visibility = (visible == true) ? 'visible' : 'hidden';
	}
}

