// Lots of content uses Modernizr.mq, polyfill with matchMedia
if (typeof window.Modernizr === 'undefined') {
  window.Modernizr = {};
}

if (typeof window.Modernizr.mq !== 'function') {
  var matchMedia = window.matchMedia || window.msMatchMedia || function () { return false; };
  window.Modernizr.mq = function (query) {
    var mql = matchMedia(query);
    return mql && mql.matches || false;
  };
}

window.jQuery(function($) {
  const $main = $('#main');

  // utility bar edit menu special behaviour
  var $editMenu = $('#sb-edit-link');
  var $editLink = $editMenu.children('.sb-edit-link');

  // capture a[href] and disable default link behaviour
  $editMenu.data('href', $editLink.attr('href'));
  $editLink.attr('href', '#').css('cursor', 'pointer'); // SBTWO-8879 Need a # href for accessibility!

  $editMenu.on('dblclick click', function(e) {
    // on double-click of top-level edit link, or single click *after* dropdown has opened, redirect to edit section landing page
    if (e.type === 'dblclick' || ($editMenu.hasClass('open') && !$(e.target).closest('ul').hasClass('dropdown-menu'))) {
      window.location.href = $editMenu.data('href');
      return false;
    } // (otherwise default to link handler in dropdown if appropriate)
  });

  var listener = function (e, screenConfig) {
    if (screenConfig.name === 'xs') {
      $('.id7-current-site-link.visible-xs-inline a').attr('accesskey', '1');
      $('.id7-current-site-link.hidden-xs a').removeAttr('accesskey');
    } else {
      $('.id7-current-site-link.hidden-xs a').attr('accesskey', '1');
      $('.id7-current-site-link.visible-xs-inline a').removeAttr('accesskey');
    }
  };
  $(window).on('id7:reflow', listener);

  // inject short-form site titles
  $('.id7-header-text h1 > span').each(function() {
    var $this = $(this);
    var xsTitle = $this.data('short-title');
    $this.removeData('shortTitle')
      .removeAttr('data-short-title')
      .clone()
      .addClass('visible-xs-inline')
      .insertBefore($this)
      .children('a, span').html(xsTitle);
    $this.addClass('hidden-xs');
  });

  // SBTWO-7434 Stop Prototype calling hide on utility bar anchors and list items
  $(".id7-utility-bar a").each(function() { this.hide = $.noop; } );
  $(".id7-utility-bar li").each(function() { this.hide = $.noop; } );

  // SBTWO-8211 If clicking anywhere other than a popover or its toggle, hide any open popovers
  var $body = $('body');
  $body
    .on('click', function (e) {
      var $t = $(e.target);
      if ($t.closest('[data-toggle="popover"]').length === 0
        && $t.closest('.popover').length === 0) {
        $('[data-toggle="popover"]').popover('hide');
      }
    })
    .on('show.bs.popover', function () {
      if (!$body.hasClass('popover-open')) {
        $body.addClass('popover-open');
      }
    })
    .on('hide.bs.popover', function () {
      $body.removeClass('popover-open');
    });

    // Formsbuilder pages include jquery-ui which nobbles Bootstrap tooltips
    if (typeof $.fn.tooltip.Constructor !== undefined) {
      var bootstrapSanitizerDefaultWhitelist = $.fn.tooltip.Constructor.DEFAULTS.whiteList;
      // SBTWO-8866 Add data attributes to the bootstrap sanitizer whitelist
      bootstrapSanitizerDefaultWhitelist.a.push(/^data-[\w-]+/);

      // SBTWO-8860 Add table tags to the bootstrap sanitizer whitelist
      bootstrapSanitizerDefaultWhitelist.table = ['border', 'role', 'align', 'style'];
      bootstrapSanitizerDefaultWhitelist.tr = [];
      bootstrapSanitizerDefaultWhitelist.td = ['colspan', 'rowspan', 'style', 'align', 'valign'];
      bootstrapSanitizerDefaultWhitelist.tbody = [];
      bootstrapSanitizerDefaultWhitelist.thead = [];

      //SBTWO-8871 Added classes to allow calendar popovers to work correctly
      bootstrapSanitizerDefaultWhitelist.span = ['class', 'category'];
      bootstrapSanitizerDefaultWhitelist.time = ['class', 'title'];
    }

    //SBTWO-9757 Handle un-re-published new window links
    $main.find('.new-window-link').each((i, newWindowLink) => {
      const $newWindowLink = $(newWindowLink);
      if ($newWindowLink.attr('aria-hidden') !== "true") {
        $(newWindowLink)
          .attr('aria-hidden', 'true')
          .after('<span class="sr-only">Link opens in a new window</span>');
      }
    });

});
