var ns6=document.getElementById && !document.all
var ie=document.all
var maxOpacity = 90;
var tipWidth = 320;
var tipDist = 200;
var lastToolTip = null;
var idCount = 0;
var list = new Array();
var tipMouseOver = new Array();
if (!ie) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0
var tempY = 0

closeHanged();

function getMouseXY(e) {
  if (ie) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true
}

function FadingTooltip()
{
    this.tmr = null;
    this.t = null;
    this.obj = null;
    this.id = idCount++;
    this.parentObject = null;
    this.addToDOM();
    this.isClosing = false;
    this.obj = document.getElementById("fa" + this.id);
    this.tipcontent = document.getElementById("tooltipcontent" + this.id);
    list[this.id] = this; 
}

function closeHanged()
{   
    try
    {
        var elem = null;
        var pop = null;
        for (var i = 0; i < idCount; i++)
        {
            log("Checking " + i);
            elem = document.getElementById("fa" + i);
            if (elem)
            {
                pop = list[i];
                var x = findPosX(pop.parentObject);
                var y = findPosY(pop.parentObject);
                var x1 = findPosX(pop.obj);
                var y1 = findPosY(pop.obj);
                
                if ((tempX >= x && tempX <= x + pop.parentObject.offsetWidth && tempY >= y && tempY <= y + pop.parentObject.offsetHeight)
                 || (tempX >= x1 && tempX <= x1 + pop.obj.offsetWidth && tempY >= y1 && tempY <= y1 + pop.obj.offsetHeight))
                {
                    setTimeout("closeHanged()", 3000);
                    return;
                }
                log("Hanged " + i + ". Closing");
                if (!pop.isClosing)
                    pop.Hide();
        	}
        	else
        	{
        	   log("Null element " + i);
        	}
        	
        }
    }
    catch (err)
    {
    }
    setTimeout("closeHanged()", 3000);
}

FadingTooltip.prototype.setText = function(text)
{
    this.tipcontent.innerHTML = text;
}

FadingTooltip.prototype.setParent = function(parent)
{
    this.parentObject = parent;
}

function sFa(e,text,align) {
	if (lastToolTip != null)
	   if (!hFa())
	     return;
	lastToolTip = new FadingTooltip(); 
	lastToolTip.setText(text);
	lastToolTip.setParent(e);
	lastToolTip.positiontip(e, align);
	lastToolTip.shw(true);
	lastToolTip.t = 0;
	lastToolTip.sTmr();
}

FadingTooltip.prototype.Hide = function()
{
    this.isClosing = true;
	if (this.tmr)
		clearInterval(this.tmr);
    this.t = 0;
}


function hFa() {
    log("hFa()");
	if (lastToolTip != null)
	{
	   var x = findPosX(lastToolTip.parentObject);
	   var y = findPosY(lastToolTip.parentObject);
	   
	   if (tempX >= x && tempX <= x + lastToolTip.parentObject.offsetWidth && tempY >= y && tempY <= y + lastToolTip.parentObject.offsetHeight)
	   {
	       log("Still over " + lastToolTip.id + ". Returning");
	       return false;
	   }
	   log("Closing " + lastToolTip.id);
	   list[lastToolTip.id].Hide();
	   lastToolTip = null;
	}
	return true;
}
 
function log(text)
{
    //document.getElementById("console").innerHTML = document.getElementById("console").innerHTML + "<br>" + text;
}


FadingTooltip.prototype.addToDOM = function()
{
    var div = document.createElement("div");
    div.setAttribute('id',"fa" + this.id);
    div.setAttribute('class', "fa");
    
    var table = document.createElement("table");
    var tr = null;
    var td = null;
    
    table.setAttribute("class", "tooltip-inner-table");
    table.setAttribute("cellpadding", "0");
    table.setAttribute("cellspacing", "0");
    table.setAttribute("onmouseout", "hFa()");
    
    tr = document.createElement("tr");
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-top-left");
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-top-middle");
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-top-right");
    tr.appendChild(td);
    table.appendChild(tr);
    
    tr = document.createElement("tr");
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-vertical-left");
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltipcontent");
    td.setAttribute("id", "tooltipcontent" + this.id);
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-vertical-right");
    tr.appendChild(td);
    table.appendChild(tr);
    
    tr = document.createElement("tr");
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-bottom-left");
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-bottom-middle");
    tr.appendChild(td);
    td = document.createElement("td");
    td.setAttribute("class", "tooltip-inner-table-bottom-right");
    tr.appendChild(td);
    table.appendChild(tr);
    
    div.appendChild(table);
    
    document.getElementsByTagName("body")[0].appendChild(div);
    div.className = "fa";
}

FadingTooltip.prototype.removeFromDOM = function()
{
    var div = document.getElementById('fa' + this.id);
    if (div)
		div.parentNode.removeChild(div);
}

FadingTooltip.prototype.Hide = function()
{
	this.t = -maxOpacity;
	this.sTmr();
	return false;
}

function execFD(idx)
{
    list[idx].fd();
}

FadingTooltip.prototype.sTmr = function() {
	this.tmr = setInterval("execFD(" + this.id + ")",15);
}

FadingTooltip.prototype.fd = function() {
	var amt = Math.abs(this.t+=2);
	if(amt == 0 || amt == maxOpacity) clearInterval(this.tmr);
	if (amt > maxOpacity)
		return;
	
	this.obj.style.filter = "alpha(opacity:"+amt+")";
	this.obj.style.KHTMLOpacity = amt/100;
	this.obj.style.MozOpacity = amt/100;
	this.obj.style.opacity = amt/100;
	
	if(amt == 0) this.shw(false);
}

FadingTooltip.prototype.gObj = function() {
	return document.getElementById("fa" + this.id);	
}

FadingTooltip.prototype.shw = function(b) {
	(b)? this.obj.className = 'fa show':this.obj.className = 'fa';	
	if (!b)
		this.removeFromDOM();
}

FadingTooltip.prototype.positiontip = function(e, align){
  var x = findPosX(e);
  var y = findPosY(e);
  var h = parseInt(e.offsetWidth);
  var xTra;
  if (align == 0)
    xTra = -tipWidth + tipDist;
  else
    xTra = parseInt(e.offsetWidth) - tipDist;
    
  this.obj.style.left = x + xTra + "px";
  this.obj.style.top = y + "px";
}

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

