(function ($, Drupal) {

  // When screen width changes, some functions need run.
  $(window).on('resize load', function () {
    // Do stuff if window is now mobile-sized.
    if (!window.matchMedia("(min-width: 64em)").matches) {
      mobileSideNav();
    }
    else {
      desktopSideNav();
    }

    // Do stuff if window is now mobile-sized.
    if (!window.matchMedia("(min-width: 40em)").matches) {
      mobileFooter();
    }
    else {
      tabletFooter();
    }
  });

  // Mobile nav open close.
  $(".mobile-nav-open").click(function () {
    $('.mobile-nav').show();
  });
  $(".mobile-nav-close").click(function () {
    $('.mobile-nav').hide();
  });

  // Main menu submenu hide/show.
  $('.mobile-nav li.usa-nav__primary-item button.usa-nav__link').on('click', function(e) {
    let $button = $(this);
    // Close open submenus and reset carats.
    $button.closest('ul').find('.usa-nav__primary-item').each(function() {
      let $menuItem = $(this);
      $menuItem.find('.usa-nav__submenu').slideUp();
      $menuItem.find('i.fa').removeClass('fa-angle-up').addClass('fa-angle-down');
    });
    // Close clicked button's submenu.
    if ($button.next().is(':visible')) {
      $(this).next().slideUp();
      $button.prev().addClass('fa-angle-down').removeClass('fa-angle-up');
    }
    else {
      $(this).next().slideDown();
      $button.prev().addClass('fa-angle-up').removeClass('fa-angle-down');
    }
  });
  // Open the submenu if a child link is the current path.
  // For some reason, usa-current is put on a main menu item when the current path
  // doesn't seem to warrant it, so not doing this for now.
  //$('.usa-current').trigger('click');

  // Show/hide share buttons.
  $(".mobile-share").on('click', function () {
    $('.block-simple-blockshare-buttons').toggle();
  });


  // Footer menu.
  let mobileFooter = function () {
    $('.usa-footer__primary-link').each(function() {
      let $link = $(this);
      $link.off('click');
      $link.removeClass('is-active').next().hide();
      $link.on('click', function (e) {
        e.preventDefault();
        if ($link.hasClass('is-active')) {
          // Clicking an already-open submenu. Just toggle the class.
          if ($link.next().is(':visible')) {
            $link.removeClass('is-active');
            $link.next().slideUp();
            return;
          }
          $link.removeClass('is-active');
          $link.next().slideUp();
        }
        else {
          // Close anything open. One at a time only.
          $link.closest('.usa-footer__nav').find('ul.usa-list').slideUp().prev().removeClass('is-active');
          $link.addClass('is-active');
          $link.next().slideDown();
        }
      });
    });
  }

  // Unbind click events on footer primary links.
  let tabletFooter = function() {
    $('.usa-footer__primary-link').each(function() {
      let $link = $(this);
      // Turn off the click event and show any hidden submenus.
      $link.off('click');
      $link.next().show();
    });
  }

  // Move group menu to above main menu on mobile.
  let mobileSideNav = function() {
    /* If it's already here, abort! */
    let $mobileGroupNav = $('.mobile-nav .group-menu');
    if ($mobileGroupNav.length !== 0) return;
    /* Show site name */
    $('.mobile-nav .site-name').removeClass('visually-hidden');
    let $sideNav = $('.c-usgs-sidebarnav');
    let $groupMenu = $('.group-menu');
    if ($sideNav.length > 0) {
      $sideNav.find('.microsite-title').insertAfter('.mobile-nav-top');
      $('.mobile-nav').find('.microsite-title').removeClass('visually-hidden');
      $groupMenu.insertAfter('.mobile-nav .microsite-title').addClass('usa-nav__primary');
    }
  }

  // Move it back to sidebar if it had been moved to mobile.
  let desktopSideNav = function() {
    let $mobileNav = $('.mobile-nav');
    let $mobileGroupMenu = $mobileNav.find('.group-menu');
    if ($mobileGroupMenu.length !== 0) {
      let $sideNav = $('.c-usgs-sidebarnav');
      $mobileNav.hide().find('.site-name').addClass('visually-hidden');
      $mobileGroupMenu.removeClass('usa-nav__primary');
      let $mobileMicrositeTitle = $mobileNav.find('.microsite-title');
      $mobileMicrositeTitle.addClass('visually-hidden');
      $sideNav.append($mobileMicrositeTitle).append($mobileGroupMenu);
    }
  }

})(jQuery, Drupal);
