(function () {
  YAHOO.namespace("ibetooltip");
  /**
   * <p>Requires YUI >= 2.6.0
   * <p>Usage: &lt;div onmouseover=&quot;YAHOO.ibetooltip.hover(event, this, '&lt;span&gt;yes&lt;/span&gt;')&quot;&gt;Mouse me&lt;/div&gt;
   * @param e event
   * @param context the element invoking this function
   * @param txt html where single and double-quotes have been escaped
   * @param xoffset Number/pixels can be null any chars which can be converted to a Number
   * @param yoffset Number/pixels can be null any chars which can be converted to a Number
   */
  YAHOO.ibetooltip = {
    hover : function(e, context, txt, xoffset, yoffset, oneline) {
      /* Thins function makes calls to objets in a specific order for different reasons (ie browsers are sensitive to call sequence)
       * if you change call sequence ordering or which functions are called, you need to test on all browsers espceially IE 6 AND 7 */
      if (YAHOO.ibetooltip.myModule && YAHOO.ibetooltip.myModule.context && YAHOO.ibetooltip.myModule.context == context) {
        return; // for handling mouse event which should not even be sent.
      }
      var tracker, DOM = YAHOO.util.Dom;
      var isie = YAHOO.env.ua.ie == 6 || YAHOO.env.ua.ie == 7 || YAHOO.env.ua.ie == 8;//waiting for higher IEs
      if (isie) {
        tracker = document.body;
      } else {
        tracker = window;
      }

      var xy = YAHOO.util.Event.getXY(e);
      var cont = DOM.get('ibeHoverContainer');
      if (cont) {// Especially needed for IE
        var t = cont.parentNode.removeChild(cont);
        t = null;
      }
      if (oneline && oneline === "true") {
        txt = '<div class="oneLineText">' + txt + '</div>';
      }
      cont = document.createElement("div");
      cont.setAttribute('id', 'ibeHoverContainer');
      DOM.addClass(cont, 'ibeHoverContainer');
      cont.setAttribute('style', 'position:absolute');
      var h = DOM.getLastChild(document.body);
      DOM.insertAfter(cont, h);// ordering affects IE
      var config = {'constraintoviewport':false};
      YAHOO.ibetooltip.myModule = new YAHOO.widget.Overlay("ibeHover", config);
      YAHOO.ibetooltip.insertText(txt, isie);
      YAHOO.ibetooltip.stopTrack(tracker, YAHOO.ibetooltip.myModule);
      YAHOO.ibetooltip.myModule.cfg.queueProperty("visible", true);
      DOM.setXY(cont, [xy[0] + 15 + (xoffset ? (+xoffset) : 0),xy[1] + 10 + (yoffset ? (+yoffset) : 0)]);
      YAHOO.ibetooltip.myModule.render(cont);
      YAHOO.ibetooltip.myModule.context = context;
      YAHOO.util.Event.addListener(tracker, "mousemove", YAHOO.ibetooltip.mouseTrack, {myModule:YAHOO.ibetooltip.myModule,'xoffset':xoffset, 'yoffset':yoffset});
      context.style.cursor = 'pointer';
    },

    mouseTrack : function(e, o) {
      var DOM = YAHOO.util.Dom;
      var xy = YAHOO.util.Event.getXY(e);
      var cr = DOM.getRegion(o.myModule.context);
      var mouse = new YAHOO.util.Point(xy[0], xy[1]), container;
      var inside = cr.contains(mouse);
      if (inside) { // keep updating
        container = DOM.get('ibeHoverContainer');
        container.style['position'] = 'absolute';
        DOM.setXY(container, [xy[0] + 15 + (o.xoffset ? (+o.xoffset) : 0),xy[1] + 10 + (o.yoffset ? (+o.yoffset) : 0)]);
      } else {
        YAHOO.ibetooltip.stopTrack(this, o.myModule);
        container = DOM.get('ibeHoverContainer');
        if (container) {
          var t = container.parentNode.removeChild(container);
          t = null;
        }
      }
    },

    stopTrack : function(obj, myModule) {
      YAHOO.util.Event.removeListener(obj, "mousemove", YAHOO.ibetooltip.mouseTrack);
      myModule.hide();
      myModule.context = undefined;
    },

    insertText : function(txt, isie) {
      // Always add ibeHoverContainerTbl as surrounder.
      var fixstart, fixend;
      if (true || isie) {
        fixstart = '<table class=\"ibeHoverContainerTbl\" id=\"ibeHoverContainerTbl\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td style=\"vertical-align:top !important;padding:0 !important;margin:0 !important\">';
        fixend = '</td></tr></table>';
        var ietxt = fixstart + txt + fixend;
        YAHOO.ibetooltip.myModule.setBody(ietxt);
      } else {
        fixstart = '<div class=\"ibeHoverContainerTbl\" id=\"ibeHoverContainerTbl\">';
        fixend = '</div>';
        var newTxt = fixstart + txt + fixend;
        YAHOO.ibetooltip.myModule.setBody(newTxt);
      }
    }
  }

})();

