(function ($) {
  $(document).ready(function () {
//Main nav related scripts - Begin
$(".usa-nav__primary button.usa-nav__link").find('a').addClass("usa-nav__link");
$(".usa-nav__primary button.usa-nav__link").click(function () {
  $(".usa-nav__primary button.usa-nav__link").css("pointer-events", "none");
  $(".usa-nav__primary button.usa-nav__link a").css("pointer-events", "auto");
});

$(".usa-nav__primary .usa-nav__primary-item").hover(function (){
  var isHovered = $(this).is(":hover");
  if (isHovered) {
    $(this).find('.usa-nav__link').addClass("hover");
    $(this).find('button.usa-nav__link').attr("aria-expanded", "true");
    $(this).find(".usa-nav__submenu").stop().slideDown(300);
  } else {
    $(this).find(".usa-nav__submenu").stop().slideUp(300);
    $(this).find('button.usa-nav__link').attr("aria-expanded", "false");
    $(this).find('.usa-nav__link').removeClass("hover");
  }
});
$(".usa-nav__primary .usa-nav__primary-item").focusin(function () {
  $(this).find('.usa-nav__link').addClass("hover");
  $(this).find('button.usa-nav__link').attr("aria-expanded", "true");
  $(this).find(".usa-nav__submenu").stop().slideDown(300);
});
$(".usa-nav__primary .usa-nav__primary-item").focusout(function () {
  $(this).find(".usa-nav__submenu").stop().slideUp(300);
  $(this).find('button.usa-nav__link').attr("aria-expanded", "false");
  $(this).find('.usa-nav__link').removeClass("hover");
});

$(".usa-nav__submenu").hover(
  function () {
    $(this).parent('.usa-nav__primary-item').find('.usa-nav__link').addClass("hover");
  }, function () {
    $(this).parent('.usa-nav__primary-item').find('.usa-nav__link').removeClass("hover");
  }
);
$(".usa-nav__primary button.usa-nav__link").click(function () {
  window.location.href = $(this).find('a.usa-nav__link').attr('href');
});
//Main nav related scripts - End
// Mobile menu - sub menu scripts - Begin
$(window).on('lessThan-md', function () {
  // Code for accordions
  $hdr = '.open-menu'; // set heading level to look out for
  $acc_id = 1; // global id counter
  /* The setup function that runs when the page has loaded */
  // Firstly, look for all headers of the right level within an accordion container
  $('.usa-nav__primary.usa-accordion').find($hdr).each(function () {
    // Store current ID value
    $this_id = $acc_id;
    // Get the current contents of the header
    $this_text = $(this).text();
    // empty the header
    $(this).empty();
    // Construct the button string with the attributes we need
    $this_button = '<button data-id="' + $this_id + '" id="accordion-button-' + $this_id + '" '
      + ' aria-controls="accordion-panel-' + $this_id + '" aria-expanded="false">'
      + $this_text + '</button>';
    // Add the button string into the heading
    $(this).append($this_button);
    // Get the set of elements between this header and the next header, or the end of
    // the accordion container
    var $set = $(this).nextUntil('.usa-nav__primary-item');
    // Set up the panel string
    $this_panel = '<div class="accordion-panel hidden" id="accordion-panel-' + $this_id +
      '" aria-labelledby="accordion-button-' + $this_id + '"></div>';
    // Wrap everything in a <div> with the necessary elements included.
    // This will be the accordion panel that is hidden by default and shown on demand.
    $set.wrapAll($this_panel);
    // Now increment the $acc_id value ready for the next one
    $acc_id++;
  });
  // Now set up the event listeners
  // Look for all buttons that are inside a correct level heading inside the accordion container
  $('.usa-nav__primary.usa-accordion').find($hdr).find('button').on('click', function (e) {
    var $thisId = $(this).attr('data-id');
    if ($(this).attr('aria-expanded') == 'true') {
      hideAccordion($(this));
      $(this).focus();
    } else {
      showAccordion($(this));
    }
  });

  function showAccordion(obj) {
    // Being passed a button
    var $thisId = $(obj).attr('data-id');
    var $panel = $('#accordion-panel-' + $thisId);
    //.log("In showAccordion - " + $thisId);
    $($panel).removeClass('hidden');
    $(obj).attr('aria-expanded', 'true');
    $(obj).focus();
  }

  function hideAccordion(obj) {
    // Being passed a button
    var $thisId = $(obj).attr('data-id');
    var $panel = $('#accordion-panel-' + $thisId);
    //.log("In hideAccordion - " + $thisId);
    $($panel).addClass('hidden');
    $(obj).attr('aria-expanded', 'false');
    $(obj).focus();
  }
});
// Mobile menu - sub menu scripts - End
  });
})(jQuery);