//
// Bubblehelp infoboxes, (C) 2002 Klaus Knopper <infobox@knopper.net>
// You can copy/modify and distribute this code under the conditions
// of the GNU GENERAL PUBLIC LICENSE Version 2.
//
// Changes: Tomasz Lewandowski
// admin@webbe.de
// Date: 19.10.2004 11:47
// Changes: 19.10.2004 blackeagle@bluemail.ch - correct position by scroll window for IE
//          19.10.2004 admin@webbe.de - correct position by scroll window for Firefox, Opera, Mozilla

var IWIDTH = 250;   // Tip box width
var fox;            // Are we using Firefox?
var moz;            // Are we using Mozilla?
var ope;            // Are we using Opera?
var ns4;            // Are we using Netscape4?
var ie4;            // Are we using Internet Explorer Version 4?
var ie5;            // Are we using Internet Explorer Version 5 and up?
var kon;            // Are we using KDE Konqueror?
var x,y,winW,winH;  // Current help position and main window size
var idiv = null;    // Pointer to infodiv container
var px = "px";      // position suffix with "px" in some cases

function nsFix()
{
  setTimeout("window.onresize = rebrowse", 2000);
}

function rebrowse()
{
  window.location.reload();
}

function infoInit()
{
  fox = (navigator.userAgent.indexOf('Firefox') > -1) ? true : false;
  ns4 = (document.layers) ? true : false;
  ope = (navigator.userAgent.indexOf('Opera') > -1) ? true : false;
  ie4 = (document.all) ? true : false;
  ie4 = (ope || fox) ? false : true;
  ie5 = ((ie4) &&
         ((navigator.userAgent.indexOf('MSIE 5') > 0) ||
          (navigator.userAgent.indexOf('MSIE 6') > 0)
         )
        ) ? true : false;
  
  moz = ((navigator.userAgent.indexOf('Mozilla') > -1) && (ie5 == false) && (fox == false)) ? true : false;
  ie4 = (moz || ope) ? false : true;
  kon = (navigator.userAgent.indexOf('konqueror') > 0) ? true : false;
  x = 0;
  y = 0;
  winW = 800;
  winH = 600;
  idiv = null;
  document.onmousemove = mouseMove;
  if(ns4 && document.captureEvents)
  {
    document.captureEvents(Event.MOUSEMOVE);
  }
  // Workaround for just another netscape bug: Fix browser confusion on resize
  // obviously conqueror has a similar problem :-(
  if (ns4 || kon)
  {
    nsFix();
  }
  if (ns4)
  {
    px = "";
  }
}

function unTip()
{
  if (idiv)
  {
    idiv.visibility = ns4 ? "hide" : "hidden";
  }
  idiv = null;
}

function getTip(name)
{
  return ((document.layers && document.layers[name])
          ? document.layers[name]
          : (document.all && document.all[name] && document.all[name].style)
             ? document.all[name].style
             : document[name]
                ? document[name]
                : (document.getElementById(name)
                   ? document.getElementById(name).style
                   : 0
                  )
          );
}

// Prepare tip boxes, but don't show them yet
function makeTip(name, title, text)
{
  var infobox = '<div id="' + name + '" name="' + name + '" class="infodiv">\n'
              + '  <table class="infotable" cellspacing="0" width="' + IWIDTH + '">\n';

  // title is present
  if (title != '')
  {
    infobox += '    <tr>\n'
             + '      <td class="infohead">\n'
             + title + '\n'
             + '      </td>\n'
             + '    </tr>\n';
  }
  // text is present
  if (text != '')
  {
    infobox += '    <tr>\n'
             + '      <td class="infocell">\n'
             + text + '\n'
             + '      </td>\n'
             + '    </tr>\n';
  }

  infobox += '  </table>\n'
           + '</div>\n';

  document.write(infobox);

}

function setTip(name)
{
  if (idiv)
  {
    unTip();
  }
  idiv = getTip(name);
  if (idiv)
  {
    winW = (window.innerWidth)
           ? window.innerWidth  + (window.pageXOffset - 16)
           : (document.body.offsetWidth - 20);
    winH = (window.innerHeight)
           ? window.innerHeight + (window.pageYOffset)
           : (document.body.offsetHeight);
    if (x <= 0 || y <= 0) // konqueror can't get mouse position
    {
      // middle of window
      x = (winW - IWIDTH) / 2 + (window.pageXOffset ? window.pageXOffset : 0);
      y = (winH - 50    ) / 2 + (window.pageYOffset ? window.pageYOffset : 0);
    }
    showTip();
  }
}

function showTip(){
  idiv.left = (((x + 260) < winW) ? (x + 12) : (x - 255)) + px;
  if (ope || fox || moz)
  {
	  idiv.top  = (((y +  90) < winH) ? (y + 12) - window.pageYOffset : (y -  90) - window.pageYOffset) + px ;
  }
  else if (ie4 || ie5)
  {
	  idiv.top  = (((y +  90) < winH) ? document.body.scrollTop + (y + 12) : document.body.scrollTop + (y -  90)) + px;
  }
  else	
  {
	  idiv.top  = (((y +  90) < winH) ? (y + 12) : (y -  90)) + px ;
  }
  idiv.visibility = ns4 ? "show" : "visible";
}

function mouseMove(e){
  if (e)
  {
    x = e.pageX ? e.pageX : (e.clientX ? e.clientX : 0);
    y = e.pageY ? e.pageY : (e.clientY ? e.clientY : 0);
  }
  else if(event)
  {
    x = event.clientX;
    y = event.clientY;
  }
  else
  {
    x = 0;
    y = 0;
  }
  if (document.documentElement) // Workaround for scroll offset of IE
  {
    x += document.documentElement.scrollLeft;
    y += document.documentElement.scrollTop;
  }
  if (idiv)
  {
    showTip();
  }
}

// Initialize after loading the page
window.onload = infoInit;

// EOF infobox.js
