//-----------------------------------------------------------------------------
//  Pop-up with dynamically changeable content.
//  Runs when hovering on a specified element. Hides after a small delay if the cursor goes out of the element or pop-up.
//-----------------------------------------------------------------------------

var TT = new Object();
TT.CurrPos = null;
TT.LastSender = null;
TT.X = 0;
TT.Y = 0;
TT.LastOpened = null;

//restores state after async postback
TT.Restore = function()
{
   TT.IsRequestExecuting = false;
   
   if (TT.LastOpened)
   {
       var id = TT.LastOpened.Id;
       var tooltip = $GetById(id);
       if (tooltip)
       {
           tooltip.style.left = TT.X;
           tooltip.style.top = TT.Y;
           tooltip.style.position = "absolute";
   
           tooltip.style.display = "block";
           $GetById(id+"h").style.display = "block";
           $GetById(id+"c").style.visibility = "visible";
           
           var fr = $GetById(id+"f");
           if (fr)
           {
               fr.style.left = TT.X;
               fr.style.top = TT.Y;
               fr.style.width = tooltip.offsetWidth;
               fr.style.height = tooltip.offsetHeight;
               fr.style.display = "block";
           }
       }
       else //broken reference
           TT.LastOpened = null;
   }  
}

//initiates pop-up showing when mouse hovers a specified element
TT.MouseMove = function(sender, id, func, key, e)
{
   e = e || window.event;
   TT.CurrPos = {X : (e.pageX || (e.clientX + document.body.scrollLeft)), Y : (e.pageY || (e.clientY + document.body.scrollTop)) };

   if (TT.LastSender != sender)
   {
       TT.LastSender = sender;
       if (TT.showTimeout) clearTimeout(TT.showTimeout);
       TT.showTimeout = setTimeout("TT.Show(\""+ id +"\",\""+ func +"\",\""+ key +"\")", 1000);
   }
}

//initiates pop-up hiding when mouse leave pop-up
TT.TooltipMouseOut = function(e)
{
   e = e || window.event;
   var elem = e.relatedTarget || e.toElement;
    
   if (TT.LastOpened)
   {
       while(elem)
       {
           if (elem.id == TT.LastOpened.Id)
           {
              if (TT.hideInterval) clearInterval(TT.hideInterval);
              return;
           }   
           else
              elem = elem.parentNode;   
       }
       
       if (TT.hideInterval) clearInterval(TT.hideInterval);
       TT.hideInterval = setInterval("TT.Hide(\""+TT.LastOpened.Id+"\",1)", 1000);
   }
}

//initiates pop-up hiding when mouse leave element
TT.MouseOut = function(e)
{
   if (br_ns && TT.IsRequestExecuting) //bug in FF
      return;
      
   TT.LastSender = null;
   if (TT.showTimeout) clearTimeout(TT.showTimeout);
   
   if (TT.LastOpened)
   {
        if (TT.hideInterval) clearInterval(TT.hideInterval);
        TT.hideInterval = setInterval("TT.Hide(\""+TT.LastOpened.Id+"\",2)", 1000);
   }
}

//shows pop-up
TT.Show = function(id, func, key)
{
   if (!TT.LastOpened || (TT.LastOpened.Id != id && TT.LastOpened.Key != key))
   {
      var tooltip = $GetById(id);
      if (tooltip)
      {
          if (TT.LastOpened)
              TT.Hide(TT.LastOpened.Id);

          TT.LastOpened = {Id:id, Key:key};
        
          var x = TT.CurrPos.X + 10;
          var y = TT.CurrPos.Y;
          if (x + parseInt(tooltip.style.width) > document.body.scrollWidth)
             x = x - parseInt(tooltip.style.width) - 50;
          if (y + 350 > document.body.scrollTop + document.body.clientHeight)
          {
               y = document.body.scrollTop + document.body.clientHeight - 350;
               if (y < 0) y = 0;
          }  
          TT.X = x + "px";
          TT.Y = y + "px";
          tooltip.style.left = TT.X;
          tooltip.style.top = TT.Y;
           
          //hide content before rereading
          $GetById(id+"c").style.visibility = "hidden";

          tooltip.style.display = "block";
          $GetById(id+"h").value = "block";

          TT.TransparentProgress = true;
          TT.IsRequestExecuting = true;
          
          eval(func);
       }
   }
}

//hides pop-up
TT.Hide = function(id,i)
{
   if (TT.hideInterval) clearInterval(TT.hideInterval);
   TT.LastOpened = null;
   TT.LastSender = null;

   var tooltip = $GetById(id);
   tooltip.style.display = "none";
   $GetById(id+"h").value = "none";

    var fr = $GetById(id+"f");
    if (fr)
       fr.style.display = "none";
}
