/**
 * Accordion expand all links
 */

(function ($, Drupal) {

    Drupal.behaviors.DhsSubThemeAccordionExpandall = {
        attach: function(context, settings) {

            $('.field--name-field-paragraphs .usa-accordion').parent().parent().not('.dhssubtheme-accordion-expand-all-processed')
                .addClass('dhssubtheme-accordion-expand-all-processed')
                .prepend('<div class="usa-accordion__controls" style="max-width: 64rem; margin: 0 auto;" aria-label="' + Drupal.t('Control all accordion items') + '">\n' +
                    '       <button class="usa-accordion__control-all accordion-close" aria-expanded="false" data-label--expanded="' + Drupal.t('Open all') + '"><svg class="svg-inline--fa fa-compress-alt fa-w-14" role="presentation" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="compress-alt" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg=""><path fill="currentColor" d="M4.686 427.314L104 328l-32.922-31.029C55.958 281.851 66.666 256 88.048 256h112C213.303 256 224 266.745 224 280v112c0 21.382-25.803 32.09-40.922 16.971L152 376l-99.314 99.314c-6.248 6.248-16.379 6.248-22.627 0L4.686 449.941c-6.248-6.248-6.248-16.379 0-22.627zM443.314 84.686L344 184l32.922 31.029c15.12 15.12 4.412 40.971-16.97 40.971h-112C234.697 256 224 245.255 224 232V120c0-21.382 25.803-32.09 40.922-16.971L296 136l99.314-99.314c6.248-6.248 16.379-6.248 22.627 0l25.373 25.373c6.248 6.248 6.248 16.379 0 22.627z"></path></svg>' + Drupal.t('Close all') + '</button>' +
                    '                 <button class="usa-accordion__control-all accordion-open" aria-expanded="false" data-label--collapsed="' + Drupal.t('Close all') + '"><svg class="svg-inline--fa fa-expand-alt fa-w-14" role="presentation" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="expand-alt" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" data-fa-i2svg=""><path fill="currentColor" d="M212.686 315.314L120 408l32.922 31.029c15.12 15.12 4.412 40.971-16.97 40.971h-112C10.697 480 0 469.255 0 456V344c0-21.382 25.803-32.09 40.922-16.971L72 360l92.686-92.686c6.248-6.248 16.379-6.248 22.627 0l25.373 25.373c6.249 6.248 6.249 16.378 0 22.627zm22.628-118.628L328 104l-32.922-31.029C279.958 57.851 290.666 32 312.048 32h112C437.303 32 448 42.745 448 56v112c0 21.382-25.803 32.09-40.922 16.971L376 152l-92.686 92.686c-6.248 6.248-16.379 6.248-22.627 0l-25.373-25.373c-6.249-6.248-6.249-16.378 0-22.627z"></path></svg>' + Drupal.t('Open all') + '</button>' +
                    '     </div>');
            $('.field--name-field-paragraphs button.usa-accordion__control-all:not(.dhssubtheme-expand-all-processed)')
              .addClass('dhssubtheme-expand-all-processed')
              .click(function(event) {
                    if($(this).hasClass('accordion-close')) {
                        $(this).parent().parent().find('.usa-accordion__button[aria-expanded="true"]').click();
                    } else {
                      // lock scroll position, but retain settings for later
                      var scrollPosition = [
                        self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
                        self.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
                      ];
                      if(scrollPosition[0] == 0 && scrollPosition[1] == 0) {
                        scrollPosition[1] = 1;
                        window.scrollTo(0, 2);
                      }
                      var html = jQuery('html'); // it would make more sense to apply this to body, but IE7 won't have that
                      html.data('scroll-position', scrollPosition);
                      html.data('previous-overflow', html.css('overflow'));
                      html.css('overflow', 'hidden');
                      window.scrollTo(scrollPosition[0], scrollPosition[1]);

                      $(this).parent().parent().find('.usa-accordion__button[aria-expanded="false"]').click();

                      // un-lock scroll position
                      var html = jQuery('html');
                      var scrollPosition = html.data('scroll-position');
                      html.css('overflow', html.data('previous-overflow'));
                      window.scrollTo(scrollPosition[0], scrollPosition[1])
                    }

                    return false;
                });
            setTimeout(function() {
                $('h1:first-of-type').click();
            }, 1500);
       }
    }

    $(document).ready(function(){
      // Use event delegation to attach the event handler
      $(document).on('keydown', '.form-item-combine > input', function(event) {
        console.log("Inside search box keydown function");
        // 13 is the key code for the 'Enter' key
        if (event.keyCode === 13) {
          event.preventDefault();
          event.stopPropagation();
          // Find the closest '.view' up the DOM tree and then find 'input.form-submit' inside it
          let submit=$(this).closest('.view').find('input.form-submit[name!="op"]');
          submit.click();
          return false;
        }
      });
    });

}(jQuery, Drupal, drupalSettings));