/*
 * Glossary class.
 *
 * Allows loading of glossary content from another page via an AJAX call through Sitebuilder.
 */

(function($) {
  $(function() {
    // SBTWO-10916: Changed to listen events on 'html'. It will support the case when the '.glossaryDefinition'
    //  tags are added after the page load (DOM adding via javascript, after AJAX call, etc.).
    $('html').on('click', '.glossaryDefinition', function(e) {

      var el = $(this);

      e.preventDefault();
      e.stopPropagation();

      if(!el.attr("aria-describedby")) { // Check if the popover is already open (if it is, the link has this attribute)
        $.get('/sitebuilder2/api/ajax/glossary.html', {
          page: el.data('page'),
          term: el.data('term'),
          rn: Math.round(Math.random() * 10000)
        }).done(function (data) {
          el.popover({
            placement: 'bottom',
            trigger: "manual", // Trigger is manual because we call el.popover('show') ourselves after creating it
            content: data,
            html: true,
            template: '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
          });
          el.popover('show');
        })
      } else {
        el.popover('hide');
      }

    })
  });
})(jQuery);
