var topDivZIndex = 10000;
var divWidth = 250;

function buildGlossary(blnExternal, strServer){
	var ids = [];
  var imgs = document.getElementsByTagName("IMG");
  for (var i=0; i < imgs.length; i++){
		if (imgs[i].id != "" && imgs[i].className == "tooltip") {
				imgs[i].onmouseover = showTooltip;
				ids.push(imgs[i].id);
		}
  }

}

// showTooltip
function showTooltip(inEvent,  inElement, inPhrase){
  if (!document.getElementById) {return;}
	
  inEvent = inEvent || window.event; //eventhandler assigned as attribute or as property
  inElement = inElement || this;     //eventhandler assigned as attribute or as property
	if (objGlossary[inElement.id]=='') {return;}
  // give it a onmouseout handler to hide the tooltip
  inElement.onmouseout = hideTooltip;

  var tooltip = document.getElementById("divTooltip");
  var tooltipframe = document.getElementById("iframeTooltip");
  tooltip = tooltip || createTooltip();
  tooltipframe = tooltipframe || createIframe();
  // get the glossary id from inside the <em id=""> element
  var id = inElement.id;
  tooltip.innerHTML = objGlossary[id];
  getToolTipDivHeight();
  
  var tooltipX = inEvent.clientX +4;
  var tooltipY = viewportTop() + inEvent.clientY + 10;
  if (tooltipX<0) tooltipX=0;
	if ((tooltipX+divWidth)>document.body.clientWidth) tooltipX = tooltipX - divWidth;
  tooltipframe.style.left = tooltipX + "px";		
  tooltipframe.style.top = tooltipY + "px";
  tooltipframe.style.width = getToolTipDivWidth();
  tooltipframe.style.height = getToolTipDivHeight();

  tooltipframe.style.visibility = "visible";
  tooltip.style.left = tooltipX + "px";
  tooltip.style.top = tooltipY + "px";
  tooltip.style.visibility = "visible";
  tooltip.style.zIndex = topDivZIndex++;

}

// hideTooltip
function hideTooltip(){
  if (!document.getElementById) {return;}
  tooltip = document.getElementById("divTooltip");
  if (tooltip){tooltip.style.visibility = "hidden";}
  tooltipframe = document.getElementById("iframeTooltip");
  if (tooltipframe){
    tooltipframe.style.width="0px";
    tooltipframe.style.height="0px";
  }
}

// create tooltip
function createTooltip(){
  var div = document.createElement("div");
  div.id = "divTooltip";
  div.className = "postit";
  div.style.position = "absolute";
  div.style.width = divWidth + "px";
  document.body.appendChild(div);
  return div;
}

// create workaround iframe
function createIframe(){
  var iframe = document.createElement("iframe");
  iframe.id = "iframeTooltip";
  iframe.style.position = "absolute";
  iframe.style.width = 0;
  iframe.style.height = 0;
  iframe.style.border = 0;

  document.body.appendChild(iframe);
  return iframe;
}

function viewportTop(){
  if (document.documentElement && document.documentElement.scrollTop){
	  return document.documentElement.scrollTop;
	}
  else if (document.body && document.body.scrollTop){
	  return document.body.scrollTop;
	}
	else if (window.pageYOffset){
	  return window.pageYOffset;
	}
	return 0;
}

function viewportLeft(){
  if (document.documentElement && document.documentElement.scrollLeft){
	  return document.documentElement.scrollLeft;
	}
  else if (document.body && document.body.scrollLeft){
	  return document.body.scrollLeft;
	}
	else if (window.pageXOffset){
	  return window.pageXOffset;
	}
	return 0;
}

function getToolTipDivHeight(){
// We are checking the inner table because of a bug in NS/Mozilla with the DIV-->offsetHeight
var divHeight = document.getElementById("divTooltip").offsetHeight;
if (divHeight < 7 ) divHeight = 0;
return divHeight;
}

function getToolTipDivWidth(){
// We are checking the inner table because of a bug in NS/Mozilla with the DIV-->offsetWidth
var sdivWidth = document.getElementById("divTooltip").offsetWidth;
if (sdivWidth < 7 ) sdivWidth = 0;
return sdivWidth;
}
