/*
  FLOATING MESSAGE - TWO OPTIONS:
  HIDE THE FLOATING MESSAGE (OUT OF ELEMENT):
  ~~~~~~~~~~~~~~~~~~~~~~~~~
  1) DEFAULT:   IMMEDIATELY 
  2) FM_STICKY: INTERVAL OF ONE MINUTE  
  
  FM_STICKY: INSERT INTO THE FLOATING MESSAGE WITH OUT HIDE IT

*/

var TP_LEFT       = 0x00000001;  
var TP_BOTTOM     = 0x00000002;  
var TP_RIGHT      = 0x00000004;  
var TP_TOP        = 0x00000008;  
var TP_TOPLEFT    = 0x00000010;  
var TP_BOTTOMLEFT = 0x00000020;
var TP_BOTTOMRIGHT= 0x00000040;
var TP_RTL        = 0x00000080;
var TP_NOSHADOW   = 0x00000100;
var TP_EVAL       = 0x00000200;
var TP_FIXEDPOS   = 0x00000400;
var TP_STICKY     = 0x00000800;

// When no style was declared
var TP_sDEFAULTSTYLE = "TP_BOTTOMLEFT | TP_RTL"

var TP_OFFSET   = 13
var TP_MAXWIDTH = 220  

var TP_nStyle = 0
  
var TP_bTipShown = false
var TP_bDisable= false
var TP_nFixedPosX = 0;
var TP_nFixedPosY = 0;
var TP_bDrag = false;
var TP_nDragStartX;
var TP_nDragStartY;
  
var TP_oLastObj = null
// Tip Counter pointer
var TP_oTipCounter = null;
// Hide Tip Counter
var TP_nHideCounter = 0;
var TP_bOverTP  = false;
var TP_bOverTip = false;


//---------------------------------    
var TP_fOldMouseMove = document.onmousemove;      
document.onmousemove = TP_OnMouseMove;

TP_oTipCounter = window.setTimeout("TP_OnTimeOut()", 10)        
//---------------------------------

function TP_OnTimeOut ()
//       ~~~~~~~~~~~~~     
{       
  // NOT sticky Style  
  if ((TP_nStyle & TP_STICKY) == 0)
  {       
    if (!TP_bOverTP && TP_bTipShown)   
      TP_Hide();
    window.setTimeout("TP_OnTimeOut()", 10)
  }    
  else
  {   
    if (TP_bOverTP || TP_bOverTip)
    {    
      TP_nHideCounter = 0;
      window.setTimeout("TP_OnTimeOut()", 10);
    }  
    else
    { 
      if (TP_bTipShown)
      {                                  
        TP_nHideCounter++;         
        if (TP_nHideCounter > 50) 
        {
          TP_Hide();            
        }          
      }
      window.setTimeout("TP_OnTimeOut()", 10)         
    }
  }     
}
//---------------------------------

function TP_nGetStyle (oElement)
//       ~~~~~~~~~~~~
{
  if ((oElement.TPStyle).indexOf("#") == -1)            
  {     
    return eval(oElement.TPStyle);            
  }  
  else
  {               
    var sElTPStyle = oElement.TPStyle;
    var sDic = sElTPStyle.substr(sElTPStyle.indexOf("#")+1,sElTPStyle.length-1);                    
    return eval(TP_nGetDictionryStyle(sDic))
  } 
}
//-----------------------------------------------------------------

function TP_nGetDefaultStyle ()
//       ~~~~~~~~~~~~~~~~~~~
{
  if (typeof(TP_sDefaultDicStyle) != 'undefined')
  {     
    return TP_nGetDictionryStyle(TP_sDefaultDicStyle)        
  }                     
  else
    return eval(TP_sDEFAULTSTYLE) 

}
//-----------------------------------------------------------------

function TP_nGetDictionryStyle (sDic)
//       ~~~~~~~~~~~~~~~~~~~~~~
{
  var oTPDic = document.getElementById("TPDictionary_" + sDic);                    
  return eval(oTPDic.TPStyle);     
}
//-----------------------------------------------------------------

function TP_sGetTPTextContent (oElement)
//       ~~~~~~~~~~~~~~~~~~~~
{
  var sDictionary = (oElement.TP).substring((oElement.TP).indexOf("#")+1,(oElement.TP).indexOf("."))                        
  var sTP = oElement.TP;  
  sDictionary =  "TPDictionary_" +  sDictionary
  var sTip = sTP.substr((oElement.TP).indexOf(".")+1,(oElement.TP).length-1)                        
  sTip = "TP_" + sTip;                
  var aTips = document.all[sTip];                                                

  if (aTips.length > 1)
  {
    for (var i=0; i<aTips.length; i++)
    { 
      if (aTips[i].parentElement.id == sDictionary)
      {
        return aTips[i].innerHTML        
        break;                
      }
    }
  }
  else
  { 
    return eval(aTips).innerHTML;              
  }              
}
//-----------------------------------------------------------------

function TP_getElementStyle (oElement)
//       ~~~~~~~~~~~~~~~~~~
{
   // Override default style with inline style if exists          
  if (typeof(oElement.TPStyle) == "string")         
  {          
    return TP_nGetStyle(oElement);            
  }
  else
  {           
    var sTP = oElement.TP;                   
    if ((oElement.TP).indexOf("#") == 0)
    {
      var sDictionary = (oElement.TP).substring((oElement.TP).indexOf("#")+1,(oElement.TP).indexOf("."))
      return TP_nGetDictionryStyle(sDictionary);              
    }            
    else
      return TP_nGetDefaultStyle();              
  }
}
//-----------------------------------------------------------------

function TP_OnMouseMove ()
//       ~~~~~~~~~~~
{
  if (TP_fOldMouseMove != null)
    TP_fOldMouseMove();
    
  if (TP_bDisable)
    return;
  var oElement = event.srcElement;    
  while (oElement != null && typeof(oElement.TP) != "string" && (!TP_bTipShown || (oElement != divTP && oElement != divShadow)))
    oElement = oElement.parentElement;

  TP_bOverTP  = (oElement != null && typeof(oElement.TP) == "string");
  TP_bOverTip = TP_bTipShown && (oElement == divTP || oElement == divShadow)
  
  if (!TP_bTipShown || !TP_bOverTip)
  {     
    if (TP_bOverTP)
    {
      TP_nHideCounter = 0;      
      var nStyle = TP_getElementStyle(oElement)      
      TP_nStyle = nStyle;
      if (oElement != TP_oLastObj)
      {                               
        TP_Hide();
        var sTP = oElement.TP;            
        if (sTP.indexOf("#") == -1)
        {
          TP(oElement.TP, nStyle)
        }
        else
        { 
          var sTPText = TP_sGetTPTextContent(oElement);            
          TP(sTPText, nStyle)
        }    
      }
      else
      {      
         //STICKY STYLE     
        if ((nStyle & TP_STICKY) != 0)
          return;                      
      }  
    }
    else
    
    if (TP_bTipShown)      
    {
      if ((TP_nStyle & TP_STICKY) != 0)
       return;
      else
        TP_Hide();
    }  
    TP_oLastObj = oElement
  }  
  else
  {
    if (TP_bOverTip)
    {
      //document.getElementById("debug").value = TP_bOverTip
      if (TP_nStyle & TP_STICKY)
      {
        TP_nHideCounter = 0;  
        return;
      }                
    }          
  }
  if (!TP_bTipShown)
    return;

  var nMouseX = event.x;
  var nMouseY = event.y; 
  var nNewPosX;
  var nNewPosY;
  var nHeight;
  var nWidth;

  if (document.body.currentStyle.direction == "rtl")
  {
    sOverflow = document.body.style.overflow

    if (sOverflow == "visible" || sOverflow == "hidden")
      bScrollBar = false
    else  
    if (sOverflow == "" || sOverflow == "scroll")
      bScrollBar = true
    else
    if (document.body.scrollHeight > document.body.clientHeight)
      bScrollBar = true
    else  
      bScrollBar = false
    if (bScrollBar)
      nMouseX -= 18;

    nMouseX += (document.body.scrollLeft - (document.body.scrollWidth - document.body.clientWidth))  
  }

  if ((TP_nStyle & TP_RTL) == TP_RTL)
  {
    TheText.align = "right"
    TheText.dir = "rtl"
  }  
  else  
  {
    TheText.align = "left"
    TheText.dir = "ltr"
  }  

  nHeight = divTP.offsetHeight;
  nWidth  = divTP.offsetWidth;

  var oElRect = TP_GetElementPos(oElement);  
  var nElWidth = oElement.offsetWidth;
  var nElHeight = oElement.offsetHeight;  
  
  switch (TP_nStyle & (TP_LEFT | TP_RIGHT | TP_TOP | TP_BOTTOM | TP_BOTTOMLEFT | TP_BOTTOMRIGHT | TP_TOPLEFT ))
  {
    case TP_TOP :                                               
      if (TP_nStyle & TP_STICKY)
      {                                                           
        nNewPosX = oElRect.top + nElWidth/2 + nWidth/2; 
        nNewPosY = oElRect.top - TP_OFFSET - nHeight;
      }
      else
      {
        nNewPosX = nMouseX - nWidth/2;
        nNewPosY = nMouseY - TP_OFFSET - nHeight;
      }                                                           
      break;
    case TP_RIGHT : 
      if (TP_nStyle & TP_STICKY)
      {         
        nNewPosX = oElRect.left + nWidth/2 + 3;        
        nNewPosY = oElRect.top - nHeight;    
      }
      else
      {    
        nNewPosX = nMouseX + 3;
        nNewPosY = nMouseY - nHeight - TP_OFFSET;
      }  
      break;
    case TP_BOTTOMRIGHT :
      if (TP_nStyle & TP_STICKY)
      {      
        nNewPosX = oElRect.left + nWidth/2 + 3;
        nNewPosY = oElRect.bottom + 5;      
      }
      else
      {
        nNewPosX = oElRect.left + 3;
        nNewPosY = oElRect.top + TP_OFFSET;
      }
      break;
    case TP_LEFT :
      if (TP_nStyle & TP_STICKY)
      {      
        nNewPosX = oElRect.left - nWidth + nElWidth/2 ;        
        nNewPosY = oElRect.top - nHeight -5;      
      }
      else
      {    
        nNewPosX = oElRect.left - 10 - nWidth;
        nNewPosY = oElRect.top - nHeight - TP_OFFSET;
      }  
      break;
    case TP_TOPLEFT :
      if (TP_nStyle & TP_STICKY)
      {      
        nNewPosX = oElRect.left - nWidth + nElWidth/2 ;
        nNewPosY = oElRect.top - nHeight - 5;      
      }
      else
      {        
        nNewPosX = nMouseX - 10 - nWidth;
        nNewPosY = oElRect.top - TP_OFFSET - nHeight;
      }
      break;      
    case TP_BOTTOMLEFT :
      if (TP_nStyle & TP_STICKY)
      {      
        nNewPosX = oElRect.left - nWidth + nElWidth/2 ;
        nNewPosY = oElRect.bottom + TP_OFFSET;      
      }
      else
      {            
        nNewPosX = oElRect.left - 10 - nWidth;
        nNewPosY = oElRect.bottom + TP_OFFSET;
      }
      break;
    default :
      if (TP_nStyle & TP_STICKY)
      {                                                           
        nNewPosX = nMouseX - nWidth/2;
        nNewPosY = nMouseY + nElHeight + TP_OFFSET
      }
      else
      {
        nNewPosX = nMouseX - nWidth/2;
        nNewPosY = nMouseY + TP_OFFSET;
        break;
      }  
  }

  // make sure the message is not running out of the BOTTOM of the screen
  if (nNewPosY + nHeight - 5> document.body.clientHeight)
    nNewPosY = document.body.clientHeight - nHeight + 5
  // make sure the message is not running out of the RIGHT of the screen
  if (nNewPosX + nWidth + 7 > document.body.clientWidth)
    nNewPosX = document.body.clientWidth - (nWidth + 7)
  // make sure the message is not running out of the LEFT of the screen
  if (document.body.currentStyle.direction == "rtl")
  {
    if (nNewPosX < (document.body.scrollLeft - (document.body.scrollWidth - document.body.clientWidth)))
      nNewPosX = (document.body.scrollLeft - (document.body.scrollWidth - document.body.clientWidth))
  }    
  else
  {
    if (nNewPosX < 0)
      nNewPosX = 0
  }
  // make sure the message is not running out of the TOP of the screen
  if (nNewPosY <-4)
    nNewPosY = -4
  
  // make sure the cursor is not in the message area
  if ((nNewPosY <= nMouseY &&   (nNewPosY + nHeight) >= nMouseY) && (nNewPosX <= nMouseX &&   (nNewPosX + nWidth) >= nMouseX))
    nNewPosY = nMouseY - nHeight - 30

  if (document.body.currentStyle.direction == "rtl")
    divTP.style.left  = nNewPosX 
  else        
    divTP.style.left       = nNewPosX + document.body.scrollLeft;
  divTP.style.top        = nNewPosY + document.body.scrollTop;
  divTP.style.visibility = "visible";    
  if ((TP_nStyle & TP_NOSHADOW) == 0)
  {  
    TP_AdjustShadow();
    divShadow.style.visibility = "visible";      
  }  
}
//---------------------------------

function TP_GetElementPos (oElement)
//      ~~~~~~~~~~~~~~~~~~    
{
  return oElement.getBoundingClientRect()
}
//---------------------------------

function TP_AdjustShadow ()
//       ~~~~~~~~~~~~~~~
{
  divShadow.style.width  = divTP.offsetWidth-5
  divShadow.style.height = divTP.offsetHeight-5
  divShadow.style.left   = divTP.offsetLeft+4
  divShadow.style.top    = divTP.offsetTop+6
}
//---------------------------------

function TP (sHtml, nStyle)
//       ~~
{  
  if (TP_bDisable)
    return;
  if (typeof(divTP) != "object")
  {
    var sCode = "<TABLE id=divTP BORDER=0 style='bborder: 1 solid red;position:absolute; z-index:100; visibility:hidden' cellpadding=0 cellspacing=0>" +                   
      "  <TR>"       
      if (typeof(TP_BLOCKSTYLE) != 'undefined') 
        sCode  += "    <TD id=TheText style=' " + TP_BLOCKSTYLE + "'></TD>" 
      else  
        sCode  += "    <TD id=TheText style=' cursor: default; font-size : 13;font-family :  Arial, Helvetica, sans-serif; color : Black; background-color:#fbfcdc; border:1px solid; border-color:#A08000;padding:10px;'></TD>" 
            
      sCode += 
      "  </TR>" +
      "</TABLE> " + 
      "<TABLE id=divShadow bgcolor=black cellpadding=0 cellspacing=0 style='position:absolute; z-index:99; visibility:hidden; filter:progid:DXImageTransform.Microsoft.Blur(PixelRadius=4,MakeShadow=true,ShadowOpacity=0.3);'>" +
      "  <TR>" +
      "    <TD>&nbsp</TD>" + 
      "  </TR>" +
      "</TABLE> "
    
    var oTable = document.createElement("TABLE")
    oTable     = document.body.insertBefore(oTable)
    oTable.outerHTML = sCode
  }  
  divTP.style.width = ""
  divTP.style.height = ""  
  
                           
  TheText.innerHTML = sHtml;  
  if (divTP.offsetWidth > TP_MAXWIDTH)
    divTP.style.width = TP_MAXWIDTH
    
  if ((TP_nStyle & TP_RTL) == TP_RTL)
  {
    TheText.align = "right"
    TheText.dir = "rtl"
  }  
  else  
  {
    TheText.align = "left"
    TheText.dir = "ltr"
  }            
  TP_nStyle     = nStyle
  TP_bTipShown          = true  
}
//---------------------------------
  
function TP_Force (sHtml, nStyle)
//       ~~~~~~~~
{
  if (TP_bDisable)
    return;
  TP (sHtml, nStyle)
}
//---------------------------------

function TP_Disable (bDisable)
//       ~~~~~~~~~~
{
  TP_bDisable= bDisable
}
//---------------------------------

function TP_HideShownTip ()
//       ~~~~~~~~~~~~~~~
{  
  TheText.innerHTML = "out"  
  divShadow.style.visibility = "hidden";
  divTP.style.visibility = "hidden";
  TP_bTipShown = false
}
//---------------------------------

function TP_Hide ()
//       ~~~~~~~
{  
  if (!TP_bTipShown)
    return;
  TP_HideShownTip();   
}
//---------------------------------

