/*
 * RB Tooltip 1.0 (uncompressed)
 *
 * This javascript function was designed and coded by Ray Boyd.
 * Copyright (c) 2008 Ray Boyd
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

var Tooltip = function()
{

/** config **/

  var maxWidth  = 300;
  var offsetX   = 10;
  var offsetY   = 10;

/** public functions **/

  return {
    init:function(classID, hash) {
      var a = getElementsByClassName(classID);
      for (var i=0; i<a.length; i++) {
        if (a[i].id) {
          var ref = a[i].id;
          var tooltip = document.createElement('span');
              tooltip.className = "tt_contents";
              tooltip.innerHTML = hash[ref];
              tooltip.style.display = 'none';
              tooltip.style.width = (tooltip.offsetWidth >= maxWidth ? maxWidth + 'px' : 'auto');
          a[i].appendChild(tooltip);
        }
        a[i].onmouseover = show;
        a[i].onmouseout  = hide;
      }
    }
  };

/** private functions **/

  function show() {
    var tooltip = this.lastChild;
        tooltip.style.display = 'block';
        tooltip.style.width = (tooltip.offsetWidth >= maxWidth ? maxWidth + 'px' : 'auto');
    document.onmousemove = function(event) {
      var a = getMousePosition(event);
      position(tooltip, a);
    };
  }

  function hide(event) {
    var tooltip = this.lastChild;
    tooltip.style.display = 'none';
  }

  function position(target, windowInfo) {
    target.style.width = (target.offsetWidth >= maxWidth ? maxWidth + 'px' : 'auto');
    x = windowInfo[0];
    y = windowInfo[1];
    var ws = getWindowSize();
    var posX = (((x + target.offsetWidth) + offsetX) >= ws[0]
                  ? target.style.left = x - target.offsetWidth - offsetX/2 + 'px'
                  : target.style.left = x + offsetX + 'px');
    var poxY = (((y + target.offsetHeight) + offsetY)  >= ws[1]
                  ? target.style.top = y - target.offsetHeight - offsetY/2 + 'px'
                  : target.style.top = y + offsetY + 'px');
  }

  function getElementsByClassName(classID) {
    var a = [];
    var scan = new RegExp('\\b' + classID + '\\b');
    var target = document.getElementsByTagName("body")[0].getElementsByTagName("*");
    for (var i=0,j=target.length; i<j; i++)
        if (scan.test(target[i].className))
          a.push(target[i]);
    return a;
  }

  function getWindowSize() {
    var a = [];
    if (typeof(window.innerWidth) == 'number')
      a = [window.innerWidth, window.innerHeight];
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
      a = [document.documentElement.clientWidth, document.documentElement.clientHeight]; // IE 6+ standards compliant mode
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
      a = [document.body.clientWidth, document.body.clientHeight]; // IE 4 compatible
    return a;
  }

  function getMousePosition(event) {
    var a = [];
    if (document.all)
      a = [window.event.x + document.body.scrollLeft, window.event.y + document.body.scrollTop];
    else if (document.getElementById || document.layers)
      a = [event.pageX, event.pageY];
    return a;
  }

}(); // RB Tooltip 24/08/08 v1.0
