/*
 * To get tooltips, include this file and:
 * 1. Give your trigger element a class 'tip'
 * 2. Give your trigger element a data-tip attribute containing the id of the tooltip
 * 3. Make sure the tooltip with the id mentioned above already exists on the page and
 *    is hidden (display: none)
**/
App.register_component('YAHOO.util.App.Tips', {
  marker: 'app-tips-bound',

  initialize: function () {
    Dom.getElementsByClassName('tip', 'img', 'frame', function (trigger) {
      if (Dom.hasClass(trigger, YAHOO.util.App.Tips.marker)) { return; }

      Evt.on(trigger, 'mouseenter', function (e) {
        var tid = App.get_data(this, 'tip')
        var tool = Dom.getNextSiblingBy(trigger, function (el) {
          return el.id === tid;
        });
        App.show(tool);
        Dom.setXY(tool, Evt.getXY(e));
      });

      Evt.on(trigger, 'mouseleave', function (e) {
        var tid = App.get_data(this, 'tip')
        var tool = Dom.getNextSiblingBy(trigger, function (el) {
          return el.id === tid;
        });
        App.hide(tool);
      });
      Dom.addClass(trigger, YAHOO.util.App.Tips.marker);
    });

    Dom.getElementsByClassName('tip_question_button', 'a', 'frame', function (trigger) {
      if (Dom.hasClass(trigger, YAHOO.util.App.Tips.marker)) { return; }
      Evt.on(trigger, 'mouseenter', function (e) {
        var tid = App.get_data(this, 'tip')
        var tool = Dom.getNextSiblingBy(trigger, function (el) {
          return el.id === tid;
        });
        App.show(tool);
        Dom.setXY(tool, Evt.getXY(e));
      });

      Evt.on(trigger, 'mouseleave', function (e) {
        var tid = App.get_data(this, 'tip')
        var tool = Dom.getNextSiblingBy(trigger, function (el) {
          return el.id === tid;
        });
        App.hide(tool);
      });
      Dom.addClass(trigger, YAHOO.util.App.Tips.marker);
    });
  }
});
