tooltips = {
  initTooltips: function()
  {
    var tips = jsHelper.getElementsByAttribute("class", "hastooltip");
    
    for (var i = 0; i < tips.length; i++)
    {
      jsHelper.attachEventListener(tips[i], "mouseover", tooltips.showTip, false);
      jsHelper.attachEventListener(tips[i], "mouseout", tooltips.hideTip, false);
    }
    
    return true;
  }, // end of initTooltips
  
  showTip: function(event)
  {
    if(typeof event == "undefined")
    {
      event = window.event;
    }
    
    var target = jsHelper.getEventTarget(event);
    
    while(target.className == null || !/(^| )hastooltip( |$)/.test(target.className))
    {
      target = target.parentNode;
    }
    
    var tip = document.createElement("div");
    var content = target.getAttribute("title");
    
    target.tooltip = tip;
    target.setAttribute("title", "");
    
    if(target.getAttribute("id") != "")
    {
      tip.setAttribute("id", target.getAttribute("id") + "tooltip");
    }
    
    tip.className = "tooltip";
    //tip.appendChild(document.createTextNode(content));
    tip.innerHTML = content;
    //alert(content);
    
    var scrollingPosition = jsHelper.getScrollingPosition();
    var cursorPosition = [0,0];
    
    if (typeof event.pageX != "undefined" &&
      typeof event.x != "undefined")
    {
      cursorPosition[0] = event.pageX;
      cursorPosition[1] = event.pageY;
    }
    else
    {
      cursorPosition[0] = event.clientX + scrollingPosition[0];
      cursorPosition[1] = event.clientY + scrollingPosition[1];
    }
    
    tip.style.position = "absolute";
    tip.style.left = cursorPosition[0] + 10 + "px";
    tip.style.top = cursorPosition[1] + 10 + "px";
    tip.style.visibility = "hidden";
    
    document.getElementsByTagName("body")[0].appendChild(tip);
    
    var viewportSize = jsHelper.getViewportSize();
    
    if(cursorPosition[0] - scrollingPosition[0] + 10 +
      tip.offsetWidth > viewportSize[0] - 25)
    {
      tip.style.left = scrollingPosition[0] + viewportSize[0] - 25 -
        tip.offsetWidth + "px";
    }
    else
    {
      tip.style.left = cursorPosition[0] + 10 + "px";
    }
    
    if(cursorPosition[1] - scrollingPosition[1] + 10 +
      tip.offsetHeight > viewportSize[1] - 25)
    {
      if (event.clientX > (viewportSize[0] - 25 - tip.offsetWidth))
      {
        tip.style.top = cursorPosition[1] - tip.offsetHeight - 10 +
          "px";
      }
      else
      {
        tip.style.top = scrollingPosition[1] + viewportSize[1] -
          25 - tip.offsetHeight + "px";
      }
    }
    else
    {
      tip.style.top = cursorPosition[1] + 10 + "px";
    }
    
    tip.style.visibility = "visible";
    
    return true;
  }, // end of showTip
  
  hideTip: function(event)
  {
    if (typeof event == "undefined")
    {
      event = window.event;
    }
    
    var target = jsHelper.getEventTarget(event);
    
    while (target.className == null ||
      !/(^| )hastooltip( |$)/.test(target.className))
    {
      target = target.parentNode;
    }
        
    if (target.tooltip != null)
    {
      target.setAttribute("title",
        target.tooltip.innerHTML); // target.tooltip.childNodes[0].nodeValue
      target.tooltip.parentNode.removeChild(target.tooltip);
    }
    
    return false;
  } // end of hideTip
};

jsHelper.addLoadListener(tooltips.initTooltips);