(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;
    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 = YAHOO.util.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');
    YAHOO.util.Dom.addClass(cont, 'ibeHoverContainer');
    cont.setAttribute('style', 'position:absolute');
    var h = YAHOO.util.Dom.getLastChild(document.body);
    YAHOO.util.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);
    YAHOO.util.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';
  };

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

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

  YAHOO.ibetooltip.insertText = function(txt, isie) {
    // Always add ibeHoverContainerTbl as surrounder.
    if (true || isie) {
      var 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\">';
      var fixend = '</td></tr></table>';
      var ietxt = fixstart + txt + fixend;
      YAHOO.ibetooltip.myModule.setBody(ietxt);
    } else {
      var fixstart = '<div class=\"ibeHoverContainerTbl\" id=\"ibeHoverContainerTbl\">';
      var fixend = '</div>';
      var newTxt = fixstart + txt + fixend;
      YAHOO.ibetooltip.myModule.setBody(newTxt);
    }
  };

})();
