// Stuff for displaying tooltips
// Should work with most popular browsers

var lastX=-1;
var lastY=-1;
var posX;
var posY;
var hasCaptureMousePos=0;
var timer=0;
var lastMessage = "";
var tooltipActive = 0;
var popup = 0;

// increased every timer step
tipcnt=0;

// Displays tooltip after a while
function displayTooltip() {
	tipcnt++;
	if (tipcnt>20) {
		timer=0;
		popup=0;
		// Setup tooltip
		document.getElementById('tooltip').innerHTML = "";  // Get rid of IE/Mac bug, it but does introduce opera bug :)
		document.getElementById('tooltip').innerHTML = lastMessage; 
		if (tooltipActive) {
			posX = lastX;
			posY = lastY;
			winWidth = 0;
			// Works at least with NS
			if (typeof(window.innerWidth) != undefined) 
				winWidth = window.innerWidth;
			if (typeof(document.body.offsetWidth) != undefined)  // IE
				winWidth = document.body.offsetWidth;
			if (winWidth)
				if ((posX+280)>winWidth)
					lastX=winWidth-280;
			document.getElementById('tooltip').style.visibility = "visible";
			document.getElementById('tooltip').style.left = lastX+13;
			document.getElementById('tooltip').style.top = lastY+16;
		}
		tipcnt=0;
	}
	timer=setTimeout("displayTooltip()",50);
	return 0;
}

// request displaying tooltip (called in mouseover)
function showTooltip(message) {
	tipcnt=0;
	if (!hasCaptureMousePos) {
		// This browser has not called captureMousePos()
		// Let's hope that browser has mouse position in "event"
		if (typeof(event) != undefined) {
			lastX = event.clientX+document.body.scrollTop;
			lastY = event.clientY+document.body.scrollLeft;
		} else {
			// fail
			return 0;
		}
	}
	if (document.getElementById('tooltip').style.visibility != "visible" && lastX >-1 && lastY > -1) {
		lastMessage = message; // Do not set message directly.. Get rid of Safari bugs
		tooltipActive = 1;
		// Display tooltip after 1.5 secs
	}
	return 0;
}

// Display tooltip as popup message
function popupTooltip() {
	popup = 1;
	tooltipActive = 1;	
	tipcnt = 4096;
	
	return 0;
}

// Hide tooltip (called in mouseout)
function hideTooltip() {
	tipcnt = 0;
	document.getElementById('tooltip').style.visibility = "hidden";
	tooltipActive = 0;

	return 0;
}

// Allow mouse move a little before tooltip dissappears
function hide() {
 	if (lastX<(posX+4))
		if (lastX>(posX-4))
			if (lastY<(posY+4)) 
				if (lastY>(posY-4)) 
					return 0;

 	document.getElementById('tooltip').style.visibility = "hidden";
	// does not work??
 	//if (lastX>(posX+3) || lastX<(posX-3) | lastY>(posY+3) || lastY<(posY-3)) {
 	//}
	return 0;
}

// Update tool tip position if mouse moves before tooltip is displayed
function updateTooltipPos() {
	if (!popup)
		tipcnt=0;
	if (typeof(event) != "undefined" && event.clientX && event.clientY) {
		lastX = event.clientX+document.body.scrollLeft;
		lastY = event.clientY+document.body.scrollTop;
	}
	hide();
	return 0;
}

// Capture mouse coordinates
function captureMousePos(e) {
	// Some browsers do not send mouse position events to onmouseover handler
 	// so we capture last known mouse position here
	if (!popup)
		tipcnt=0;
	hasCaptureMousePos=1;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		lastX = e.pageX;
		lastY = e.pageY;
	} else {
		// if (e.clientX && e.clientY) 
		lastX = e.clientX+document.body.scrollLeft;
		lastY = e.clientY+document.body.scrollTop;
	}
	hide();
	return 0;
}

// Assign event handler for browsers that supports it
window.onmousemove = captureMousePos;
window.onkeydown = hideTooltip;
// Set up timer routine
displayTooltip();
