// Compatibility shim for WPopupBox, which has been removed in favor of bootstrap popovers

function WPopupBox() {

  this.anchor = null;

  this.options = {
    placement: 'bottom',
    html: true,
    content: '<i class="fa fa-spin fa-spinner" aria-hidden="true"> Loading...</i>',
    template: '<div class="popover role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>'
  };

  this.positionAbove = function(el) {
    this.options.placement = 'top';
    this.showPopover(el);
  };

  this.positionBelow = function(el) {
    this.options.placement = 'bottom';
    this.showPopover(el);
  };

  this.positionRight = function(el) {
    this.options.placement = 'right';
    this.showPopover(el);
  };

  this.show = function() {
    if(this.anchor) {
      this.anchor.popover('show');
    }
  };

  this.hide = function() {
    if(this.anchor) {
      this.anchor.popover('hide');
    }
  };

  this.setContent = function(content) {
    if(this.anchor) {
      this.options.content = content;
      this.anchor.data('bs.popover').options.content = content;
      this.anchor.popover('show');
    }
  };

  this.setSize = function(width, height) {
    if(this.anchor) {
      this.anchor.data('bs.popover').tip().find('.popover-content').css({
        'width': width + 'px',
        'height': height + 'px',
        'max-width': width + 'px',
        'max-height': height + 'px',
        'overflow-y': 'scroll'
      });
      this.anchor.popover('show')
    }
  };

  this.showPopover = function(el) {
    if(jQuery(el).length) {
      el.hide = jQuery.noop;
      this.anchor = jQuery(el);
      this.anchor = jQuery(el);
      this.anchor.popover(this.options);
      this.anchor.popover('show');
    }
  };

};

window.WPopupBox = WPopupBox;
