var IE = document.all ? true : false;
var NS6 = document.getElementById && !document.all;
if (!IE) document.captureEvents(Event.MOUSEMOVE);

var disappeardelay = 250;  //menu disappear speed onMouseout (in miliseconds)
//var hidetooltip_onclick = "yes"; //hide menu when user clicks within menu?

var mouseX = 0;
var mouseY = 0;

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX;
    mouseY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mouseX < 0) mouseX = 0;
  if (mouseY < 0) mouseY = 0;
  return true;
}

if (IE || NS6) {
	document.write('<div id="tooltips" style="visibility:hidden; z-index:65537" onMouseover="clear_timeout()" onMouseout="hide_tooltip(event)"></div>');
	document.write('<iframe id="mattress_layer" frameborder="0" scrolling="no" style="position:absolute; width:1px;height:1px; z-index:65536; filter:alpha(opacity=1) shadown();-moz-opacity:0.1;"></iframe>');
}
function show_tooltip(obj, e, content) {
	clear_timeout();
	tooltips_obj = document.getElementById ? document.getElementById("tooltips") : tooltips;
	mattress_obj = document.getElementById ? document.getElementById("mattress_layer") : mattress_layer;
	tooltips_obj.innerHTML = content;
	getMouseXY(e);
	
	if (document.body) {
		w_width = document.body.clientWidth;
		w_height = document.body.clientHeight;
	} else if (self.innerWidth) {
		w_width = self.innerWidth;
		w_height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		w_width = document.documentElement.clientWidth;
		w_height = document.documentElement.clientHeight;
	} else {
		w_width = 0;
		w_height = 0;
	}	
	if (IE || NS6) {
		top_dis = mouseY;
		bot_dis = w_height - mouseY;
		if (tooltips_obj.offsetHeight > top_dis && tooltips_obj.offsetHeight > bot_dis) tooltip_top = mouseY - tooltips_obj.offsetHeight + "px";
		else if (tooltips_obj.offsetHeight < top_dis) tooltip_top = mouseY - tooltips_obj.offsetHeight + "px";
		else tooltip_top = mouseY + "px";
		
		mattress_obj.style.left = mouseX - (tooltips_obj.offsetWidth/2) + "px";
		mattress_obj.style.top = tooltip_top;
		mattress_obj.style.width = tooltips_obj.offsetWidth;
		mattress_obj.style.height = tooltips_obj.offsetHeight;
		
		tooltips_obj.style.left = mouseX - (tooltips_obj.offsetWidth/2) + "px";
		tooltips_obj.style.top = tooltip_top;
		tooltips_obj.style.filter='alpha(opacity=90)'; // IE
		tooltips_obj.style.opacity='0.90'; // Firefox		
		tooltips_obj.style.visibility = "visible";
	}
	return clickreturnvalue();
}

function clickreturnvalue() {
	if (IE || NS6) return false;
	return true;
}

function contains_ns6(a, b) {
	while (b.parentNode)
		if ((b = b.parentNode) == a) return true;
	return false;
}

function hide_tooltip(e) {
	if (IE && !tooltips_obj.contains(e.toElement)) delayhide = setTimeout("hide()",disappeardelay);
	else if (NS6 && e.currentTarget != e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget)) delayhide = setTimeout("hide()",disappeardelay);
}

function hide(e) {
	if (typeof tooltips_obj != "undefined") {
		if (IE||NS6) tooltips_obj.style.visibility="hidden";
		mattress_obj.style.height = "1px";
		mattress_obj.style.height = "1px";
	}
}

function clear_timeout(){
	if (typeof delayhide != "undefined") clearTimeout(delayhide);
}
//if (hidetooltip_onclick == "yes") document.onclick = hide;
