// SAMHSA - Main Drupal behaviors file
(function ($, Drupal) {
  Drupal.behaviors.mainBehavior = {
    attach(context) {
      $(once("mainBehavior", "html", context).forEach((element) => {
          $(document).ready(() => {
            // OCWT-2498: SET H1 WIDTH TO GRANDPARENT IF NO RIGHT RAIL
            const mlowidth = $(
              ".main-layout.with--sidebar-first.without--sidebar-second"
            ).width();
            $(
              ".main-layout.with--sidebar-first.without--sidebar-second article h1.program-title"
            ).width(mlowidth);

            // ADD "measure-none" CLASS TO EXCLUDE MEASURE BY PAGE
            switch (window.location.pathname) {
              case "/practitioner-training":
                $("body").addClass("measure-none");
                break;
              default:
              // Do nothing
            }

            if ($(".measure-none").length > 0) {
              $("body").addClass("measure-none");
            }

            // OCWT-2817
            $("#h5p-iframe-24").on("load", () => {
              $("#h5p-iframe-24")
                .contents()
                .find("head")
                .append(
                  $(
                    "<link href='https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700;1,900&display=swap' rel='stylesheet'> <style type='text/css'>  body {font-family:'Source Sans Pro', sans-serif !important;} .h5p-joubelui-button, .h5p-feedback-text, .h5p-question-content {font-size:1.125rem;}  </style>"
                  )
                );
            });

            // LEGO CONTACT US MULTIFIELD OCWT-7235
            $('.field--name-field-phone-number').prepend('<i class="fas fa-phone-alt"></i>');

            $('.field--name-field-email').prepend('<i class="fas fa-envelope"></i>');

            $('.field--name-field-phone-number, .field--name-field-email').hover(
              function() {
                  $(this).find('svg').css('color', '#a82d2b'); 
                  $(this).find('a').css('color', '#a82d2b');   
              }, function() {
                  $(this).find('svg').css('color', '#1f419a'); 
                  $(this).find('a').css('color', '#1f419a');   
              }
            );

            // REPLACE SVG.EXT ICON WITH FONT AWESOME SVG ICON
            $("svg.ext").removeAttr("aria-label").find("title").remove();
            $(
              "a.ext.btn, a.ext.button, .block-mobile-menu a.ext, .first-sidebar a.ext, .block-superfish-main-menu a.ext, .menu-item a.ext"
            ).each((i, el) => {
              $(el)
                .append(
                  "<span class='exitDisclaimer'><a href='https://www.samhsa.gov/disclaimer' aria-label='Link to SAMHSA Exit Disclaimer'><i class='fas fa-external-link-alt' ></i></a></span>"
                )
                .attr("aria-label", `${$(el).text()}, external`);
            });
            $("a.ext:not(.btn, .button, .btn2)").each((i, el) => {
              if (
                !$(el)
                  .parents()
                  .is(".navigation, .slide_link, .block-superfish-main-menu")
              ) {
                $(el)
                  .after(
                    "<span class='exitDisclaimer'><a href='https://www.samhsa.gov/disclaimer' aria-label='Link to SAMHSA Exit Disclaimer'><i class='fas fa-external-link-alt' ></i></a></span>"
                  )
                  .attr("aria-label", `${$(el).text()}, external`);
              }
            });
            $(
              ".usa-card-group.samhsa .usa-card__footer a.ext .exitDisclaimer"
            ).remove();

            $(
              ".usa-card-group.samhsa .usa-card__footer a.ext button, .usa-card__container .usa-card__footer a.ext button, .slide_link a.ext, .icons-block a.ext .icon .textbox"
            ).append(
              "<span class='exitDisclaimer'><a href='https://www.samhsa.gov/disclaimer' aria-label='Link to SAMHSA Exit Disclaimer'><i class='fas fa-external-link-alt' ></i></a></span>"
            );
            $(".exitDisclaimer a").on("focus", (e) => {
              $(e.target).attr("aria-label", "SAMHSA Exit Disclaimer");
            });

            // Layout builder buttons
            /* Commenting out this section as it causes duplicate icons.
            $(".usa-card__footer a.ext button.samhsa").append(
              "<span class='exitDisclaimer'><a href='https://www.samhsa.gov/disclaimer' aria-label='Link to SAMHSA Exit Disclaimer'><i class='fas fa-external-link-alt' ></i></a></span>"
            );
            */
            $("button.samhsa").siblings("span.exitDisclaimer").remove();

            // [OCWT-2741] Redo the email FA icon for buttons to place it inside the button.
            // Add the inside icon
            $(".usa-card-group.samhsa .usa-card__footer a.mailto button").append(
              " <i class='fa fa-envelope-o'></i>"
            );
            // Remove the outside icon
            $(".layout_builder_card .usa-card__container .usa-card__footer a.mailto svg").remove();

            $(
              "article.node--type-resource .resource-link .field__item a.ext .exitDisclaimer"
            ).attr("target", "_blank");

            // ADD MASONRY LAYOUT TO RIGHT RAIL
            if ($(window).width() > 639) {
              $(".region-sidebar-second").addClass("masonry");
              $(".region-sidebar-second .block-block-content").addClass("masonry-item");
              $(".region-sidebar-second .block-content").addClass(
                "masonry-item-content"
              );
            }

            if ($(window).width() > 1025) {
              $(".region-sidebar-second").removeClass("masonry");
              $(".region-sidebar-second .block-block-content").removeClass(
                "masonry-item"
              );
              $(".region-sidebar-second .block-content").removeClass(
                "masonry-item-content"
              );
            }

            // MASONRY LAYOUT FUNCTIONS
            function resizeGridItem(item) {
              const grid = document.getElementsByClassName("masonry")[0];
              const rowHeight = parseInt(
                window.getComputedStyle(grid).getPropertyValue("grid-auto-rows"),
                10
              );
              const rowGap = parseInt(
                window.getComputedStyle(grid).getPropertyValue("grid-row-gap"),
                10
              );
              const rowSpan = Math.ceil(
                (item.querySelector(".masonry-item-content").getBoundingClientRect()
                  .height +
                  rowGap) /
                  (rowHeight + rowGap)
              );
              item.style.gridRowEnd = `span ${rowSpan}`;
            }

            function resizeAllGridItems() {
              const allItems = document.getElementsByClassName("masonry-item");
              for (let x = 0; x < allItems.length; x++) {
                resizeGridItem(allItems[x]);
              }
            }

            function resizeInstance(instance) {
              const item = instance.elements[0];
              resizeGridItem(item);
            }

            window.onload = resizeAllGridItems();
            window.addEventListener("resize", resizeAllGridItems);

            const allItems = document.getElementsByClassName("masonry-item");
            for (let x = 0; x < allItems.length; x++) {
              imagesLoaded(allItems[x], resizeInstance);
            }

            // Apply some 508 to Marijuana Quiz Iframe.
            if (window.location.href.includes("/marijuana-quiz") === true) {
              $("iframe.h5p-iframe").attr({
                title: "Take the quiz to test your marijuana IQ",
              });
            }

            // Featured resources card.
            $(".ebrc-featured-resources").mouseup(() => {
              window.location = $(this).find("a").attr("href");
              return false;
            });

            // Removing text links from /sitemap.
            $("div.sitemap ul.sitemap-menu ul.sitemap-menu li")
              .has("span.no-link")
              .css("display", "none");

            // [OCWT-1979] - Add label to chosen-search-input fields for 508 compliance
            $(".chosen-search-input").each((i, el) => {
              const chosenInput = $(el)
                .parents(".js-form-item.form-item.js-form-type-select.form-type-select")
                .children("label")
                .text();
              $(el)
                .attr("id", `chosen-search-input-${i}`)
                .before(
                  `<label style="position:absolute;left:-100000px;" for="chosen-search-input-${i}">${chosenInput} selected</label>`
                );
            });

            // Click the "Apply" button to fix data values on MAT page.
            $(() => {
              if (
                $(
                  "div.block-views-blockbup-physician-count-national-table--block-1 div.view-content"
                ).children().length > 1
              ) {
                $('input[value="Apply"]').click();
              }
            });

            // 508 fix for Constant Contract widget
            $(() => {
              setTimeout(() => {
                $(".ctct-form-footer-link .ctct-form-footer-img").attr(
                  "alt",
                  "Constant Contact"
                );
                $(".ctct-form-errorMessage").each((i, el) => {
                  $(el).attr("style", "color: #A82D2B !important");
                });
                $(".ctct-form-custom").on("submit", () => {
                  if ($(".ctct-form-errorMessage").is(":not(:empty)")) {
                    $("input#email_address_1").focus();
                    setTimeout(() => {
                      $(".ctct-form-errorMessage").css("display", "block");
                    }, 200);
                    $(".ctct-form-errorMessage").attr("id", "ctct-form-errorMessage");
                    $("input#email_address_1").attr(
                      "aria-describedby",
                      "ctct-form-errorMessage"
                    );
                  }
                });
              }, 1000);
            });

            if (window.location.href.includes("/blog/year") === true) {
              //  [OCWT-3126] Do nothing if no results are found.
              if ($("#views-no-results-found").length === 0) {
                // having to call this out separately as the years are not taxo terms, but are populated from a module
                const yearurl = window.location.href.split("?");
                const year = yearurl[0];
                $(".breadcrumb ol li:last-child").text(
                  ` ${year.substring(year.lastIndexOf("/") + 1)}`
                );
              }
            }

            // Sample URL: /grants-awards-by-state/CA/2020
            //  [OCWT-3126] Do nothing if no results are found.
            if (window.location.href.includes("/grants-awards-by-state/") === true) {
              if ($("#views-no-results-found").length === 0) {
                const count = (window.location.href.match(/\//g) || []).length;

                if (count === 5) {
                  const tmpYear = window.location.href.split("/")[5];
                  const tmpState = window.location.href.split("/")[4];
                  $("body.path-grants-awards-by-state .breadcrumb ol li:eq(3)").html(
                    ` ${tmpYear}`
                  );
                  $("body.path-grants-awards-by-state .breadcrumb ol li:eq(3)").after(
                    `<li> ${tmpState}</li>`
                  );
                } else if (count === 7) {
                  const tmpYear = window.location.href.split("/")[6];
                  const tmpState = window.location.href.split("/")[4];

                  $("body.path-grants-awards-by-state .breadcrumb ol li:eq(3)").html(
                    ` ${tmpYear}`
                  );
                  $("body.path-grants-awards-by-state .breadcrumb ol li:eq(3)").after(
                    ` <li> ${tmpState}</li>`
                  );

                  let lastcrumb = "";
                  if (window.location.href.split("/")[5] === "discretionary") {
                    lastcrumb = " Discretionary Details";
                  } else {
                    lastcrumb = " Non-Discretionary Details";
                  }
                  $("body.path-grants-awards-by-state .breadcrumb ol li:eq(4)").after(
                    `<li>${lastcrumb}</li>`
                  );
                }
              }
            }

            // Takes the page title from the H1 and puts that string in the breadcrumbs
            // for URLs such as /grants/awards/2022/TI-22-001. Not sure why this can't be done
            // in the Drupal breadcrumbs module.
            if (window.location.href.includes("/grants/awards/") === true) {
              const grantsAwardsTitle = $(
                ".individual-grant-awards .view-header h1"
              ).text();
              $("body.path-grants .breadcrumb ol li:eq(3)").text(
                ` ${grantsAwardsTitle}`
              );
            }

            // OCWT-3119
            $(".main-layout").each((i, el) => {
              if (
                $(el).is(".with--sidebar-first.without--sidebar-second") &&
                $(".with--sidebar-first.without--sidebar-second").find(
                  ".views-element-container"
                ).length > 0
              ) {
                $(el).removeClass("measure-6");
              }
            });

            // OCWT-4126
            $(".field--name-field-cpnt-image-caption").each((i, el) => {
              if (!$(el).is(":empty")) {
                $(el)
                  .prev(".field--name-field-cpnt-media.field--type-image")
                  .find("img")
                  .attr("alt", "");
              }
            });

            // OCWT-4363
            // If they came from the dashboard with filters, they go back to it.
            // If they came to the RFA page directly, they go to the clean dashboard
            if (
              window.location.href.includes("/grants/grant-announcements/") === true
            ) {
              $("#back-to-grants").click((event) => {
                event.preventDefault();
                if (window.history.length > 1) {
                  window.history.back(1);
                } else {
                  window.location.href = "/grants/grants-dashboard";
                }
              });
            }

            // OCWT-6104
            $("li.usa-sidenav__item a.not-in-left-nav").parent().remove();
            $("li.sf-depth-2 a.not-in-top-nav").parent().css({"display": "none" });
            $("li.sf-depth-1 a.not-in-top-nav").parent().css({"display": "none" });
            $("li.menu-item a.not-in-mobile").parent().remove();

            function activeTrail (text, parenturl) {
              let myID = $("li.menu-item a")
                .filter(function( index ) {
                  return $(this).text() === text;
                })
                .addClass("is-active")
                .parents("li")
                .addClass("menu-item--active-trail")
                .parents("div.mm-panel")
                .attr("data-mm-parent");

              $("#" + myID).addClass("menu-item--active-trail");

              $("li.usa-sidenav__item a")
              .filter(function( index ) {
                return $(this).text() === text;
              })
              .addClass("is-active");

              $("ul.sf-menu, ul.sf-menu ul")
                .find("a[href='"+parenturl+"']")
                .addClass("active")
                .parent()
                .addClass("active-trail");
            }

            if (window.location.href.includes("/resource-search/ebp?") === true) {
              activeTrail("Evidence-Based Practices Resource Center","/programs");
            }

            if (window.location.href.includes("/resource-search/988?") === true) {
              activeTrail("988 Partner Toolkit","/find-help");
            }

            if (window.location.href.includes("/resource-search/sptac?") === true) {
              activeTrail("Resource Library","/practitioner-training");
            }

            if (window.location.href.includes("/grants/grants-dashboard?") === true) {
              activeTrail("Grants Dashboard","/grants");
            }

            if (window.location.href.includes("/about-us/advisory-councils/meetings?") === true) {
              activeTrail("Meetings","/about-us");
            }

            if (window.location.href.includes("/about-us/advisory-councils/meetings?") === true) {
              activeTrail("Meetings","/about-us");
            }

            if (window.location.href.includes("/resource/ebp/") === true) {
              activeTrail("Evidence-Based Practices Resource Center","/programs");
            }

            if (window.location.href.includes("/resource/988/") === true) {
              activeTrail("988 Partner Toolkit","/find-help");
            }

            if (window.location.href.includes("/resource/sptac/") === true) {
              activeTrail("Resource Library","/practitioner-training");
            }

            if (window.location.href.includes("/resource/dbhis/") === true) {
              activeTrail("DBHIS Resource Center","/practitioner-training");
            }

            if (window.location.href.includes("/grants/grant-announcements/") === true) {
              activeTrail("Grants Dashboard","/grants");
            }

            if (window.location.href.includes("/grants-awards-by-state/") === true || window.location.href.includes("/grants-awards-by-state?") === true) {
              activeTrail("Grant Awards By State","/grants");
            }

            if (window.location.href.includes("/blog/") === true) {
              activeTrail("SAMHSA Blog","/newsroom");
            }

            if (window.location.href.includes("/newsroom/press-announcements/") === true && window.location.href.includes("/ddh-") === false) {
              activeTrail("Press Announcements","/newsroom");
            }

            if (window.location.href.includes("/newsroom/press-announcements/") === true && window.location.href.includes("/ddh-") === true) {
              activeTrail("Disaster Distress Helpline Press Announcements","/newsroom");
            }

            if (window.location.href.includes("/newsroom/statements/") === true) {
              activeTrail("Statements","/newsroom");
            }

            if (window.location.href.includes("/medication-assisted-treatment/find-treatment/treatment-practitioner-locator?") === true) {
              activeTrail("Buprenorphine Practitioner Locator","/programs");
            }

            if (window.location.href.includes("/meetings/") === true) {
              activeTrail("Meetings","/about-us");

              // Need to do this because the meetings are two levels down. and meetings link doesn't get rendered
              $("li.usa-sidenav__item a")
                .filter(function( index ) {
                  return $(this).text() === "Advisory Councils";
                })
                .addClass("is-active")
                .parent()
                .addClass("menu-item--active-trail");
            }

            if (window.location.href.match(/\/(encuentra-apoyo|semana-de-prevencion|salud-mental)\b/)) {
              $("html, .side-menu a").attr("lang", "es");
            }

            if (window.location.href.match(/\/(linea-nacional-ayuda|entendamos-el-estres-traumatico-infantil-y-como-ayudar)\b/)) {
              $("html").attr("lang", "es");
              $(".side-menu a[href='/child-trauma/entendamos-el-estres-traumatico-infantil-y-como-ayudar']").attr("lang", "es");
            }

            $('.tab-btn').click(function() {
              $('.tab-title').removeClass('active');
              $(this).find('.tab-title').addClass('active');
            });

            //OCWT-8716  
            $("#views-exposed-form-fep-program-block-1 input[id^='edit-submit-fep-program']").click(()=> { 
              $(location).attr("href", `/esmi-treatment-locator?field_fepstate_value=${$("#edit-field-fepstate-value").find(":selected").val()}&field_fep_program_name_value=${$("#edit-field-fep-program-name-value").val()}&items_per_page=${$("#edit-items-per-page").find(":selected").val()}&page=0#block-samhsa-uswds-base-views-block-fep-program-block-1`);
            });
            $("#views-exposed-form-news-page-1 input[id^='edit-submit-news']").click(()=> { 
              $(location).attr("href", `/newsroom/press-announcements?body_value=${$("#edit-body-value").val()}&field_release_date_value%5Bmin%5D=${$("#edit-field-release-date-value-min").val()}&field_release_date_value%5Bmax%5D=${$("#edit-field-release-date-value-max").val()}&items_per_page=${$("#edit-items-per-page").find(":selected").val()}&page=0`);
            });
            $("#views-exposed-form-news-page-1 input[id^='edit-reset']").click(()=> { 
              $(location).attr("href", "/newsroom/press-announcements?body_value=&field_release_date_value%5Bmin%5D=&field_release_date_value%5Bmax%5D=&items_per_page=10&page=0");  
            });
            if(window.location.href.includes("/newsroom/press-announcements?body_value=&field_release_date_value%5Bmin%5D=&field_release_date_value%5Bmax%5D=&items_per_page=10&page=0") === true) {
              $("#views-exposed-form-news-page-1 input[id^='edit-reset']").css("display", "none"); 
            }
         });

          // 508 - Add alt attribute to Font Awesome SVGs for screen readers
          $(window).bind("load", () => {
            $("svg.svg-inline--fa.fa-external-link-alt").attr(
              "alt",
              "SAMHSA Exit Disclaimer"
            );
          });

          // Mobile Search: making search visible at small sizes
          $(once("opensearch", "#search-toggle")).mouseup((e) => {
            e.preventDefault();
            $(".search-form-wrapper")
              .slideToggle()
              .promise()
              .done(() => {
                $(".search-form-wrapper").removeAttr("style").toggleClass("open");
              });
          });
          // execute mobile menu
        $(document).ready(() => {
            // execute menu active path
            let path = window.location.pathname.split("/");
            path = path[path.length - 1];
            if (path !== undefined) {
              $("ul.sf-menu, ul.sf-menu ul")
                .find(`a[href$='${path}']`) // gets all links that match the href
                .parents("li") // gets all list items that are ancestors of the link
                .children("a") // walks down one level from all selected li's
                .addClass("active");
            }
            // More Like this section responsive tabs

            // OCWT-4186
            if ($(window).width() < 1024) {
              $(window).scroll(() => {
                if ($(window).scrollTop() > 50) {
                  $("#block-headercallout").addClass("headercallout-sticky");
                } else {
                  $("#block-headercallout").removeClass("headercallout-sticky");
                }
              });
        
               //for the 988 callout box height
              $(function() {
                $('header:has(#block-headercallout)').css('height', '110px');
              });
            }

            // Add optional class to lego card footer
            $("button.btnSnap").parent().parent().parent().addClass("btnSnapCon");
            $("button.btnSnap").parent().parent().addClass("btnSnapFoot");
          });
      }));
    }
  }
})(jQuery, Drupal);

(function ($, Drupal) {
  Drupal.behaviors.behaviorsMain = {
    attach(context) {
      $(once("behaviorsMain", "html", context).forEach((element) => {
        (($) => {
          //  ---------------- RESOURCE CENTER ----------------
          //  -----RESOURCE PAGE RESOURCE LINK DISPLAY
          // BUG: the following selector does not exist in USWDS
          const arialabel = $("div#block-pagetitle h1 div").text();

          $("article.node--type-resource .resource-link .field__item > a")
            .attr("aria-label", `Link to ${arialabel}`)
            .text("View Resource");
          $("article.node--type-resource .product-page--download > a").attr(
            "aria-label",
            `Link to ${arialabel}`
          );
          const dltext = $(
            "article.node--type-resource .product-page--download > a button"
          ).text();
          $("article.node--type-resource .product-page--download > a button").text(
            dltext.replace("Download", "View Resource")
          );

          if ($("article.node--type-resource div.product-page--download").length) {
            $("article.node--type-resource div.resource-link").hide();
          }

          //  ---------------- PARAGRAPHS ----------------

          //  -----PARAGRAPH TYPE - ADD PID
          $(".paragraph").each((i, el) => {
            const pid = $(el).attr("data-quickedit-entity-id");
            $(el).attr("id", pid);
          });

          // HOMEPAGE SPOTLIGHT HIDE/SHOW EDIT BUTTON
          $(".samhsa-spotlight .hs-button-edit").addClass("visually-hidden");
          $(".samhsa-spotlight").mouseenter(() => {
            $(".samhsa-spotlight .hs-button-edit").removeClass("visually-hidden");
            $(".samhsa-spotlight .hs-button-edit-button").removeClass(
              "visually-hidden"
            );
          });
          $(".samhsa-spotlight").mouseleave(() => {
            $(".samhsa-spotlight .hs-button-edit").addClass("visually-hidden");
            $(".samhsa-spotlight .hs-button-edit-button").addClass("visually-hidden");
          });

          // blog image aspect ratio
          $(".blog__body img").parent().addClass("blog_body_image");

          // remove extra asterisk
          if ($(".form-required:contains(*)").length > 0) {
            const formrequired = $(".form-required:contains(*)");
            const formasterisk = formrequired.html().replace("*", "");
            formrequired.html(formasterisk);
          }

          // ADD LANG ENGLISH ATTRIBUTE TO LEFT NAVIGATION IN SPANISH PAGE
          if (window.location.href.includes("/linea-nacional-ayuda") === true) {
            $(".side-menu a").each((i, el) => {
              if (!$(el).hasClass("is-active")) {
                $(el).attr("lang", "en");
              }
            });
            $(".side-menu a[href='/salud-mental']").attr("lang", "es");
            $(".side-menu a[href='/encuentra-apoyo']").attr("lang", "es");
            $(".side-menu a[href='/linea-nacional-ayuda']").attr("lang", "es");
          }

          // ADD LANG SPANISH ATTRIBUTE TO LEFT NAVIGATION
          if (window.location.href.includes("/find-help/national-helpline") === true) {
            $(".side-menu a[href='/salud-mental']").attr("lang", "es");
            $(".side-menu a[href='/linea-nacional-ayuda']").attr("lang", "es");
          }

          // ADD ARIA LABEL FOR ACTIVE PAGE IN LEFT NAVIGATION
          $(".side-menu a").each((i, el) => {
            if ($(el).hasClass("is-active")) {
              $(el).attr("aria-current", "page");
            }
          });

          // OCWT-2783
          $("#views-exposed-form-rc-solr-search-page-1 .form-item-sort-bef-combine")
            .detach()
            .insertAfter("#views-exposed-form-rc-solr-search-page-1 .form-actions");
          $("#views-exposed-form-rc-solr-search-page-1 .form-item-items-per-page")
            .detach()
            .insertAfter(
              "#views-exposed-form-rc-solr-search-page-1 .form-item-sort-bef-combine"
            );

          // OCWT-3221
          $("h2").each((i, el) => {
            if ($(el).hasClass("visually-hidden")) {
              $(el).attr("aria-hidden", "true");
            }
          });

          if (
            $(".bhn-story--pillButton-container.slide-in-elliptic-top-fwd").hasClass(
              "espanol"
            )
          ) {
            $("#main").attr("lang", "es");
          }

          // OCWT-5903
          $(".webform-element-help.js-webform-element-help").each((i, el) => {
            const eventString = $(el)
              .attr("data-webform-help")
              .replace(/(<([^>]+)>)/gi, " ");
            $(el).attr("aria-label", eventString);
          });

          if (window.location.href.includes("/speaker-request-form") === true) {
            setTimeout(() => {
              $("#usasearch_sayt #ui-id-1").attr("id", "ui-id-11");
            }, 2000);
          }
        })(jQuery);
      }));
    }
  }
})(jQuery, Drupal);

 // EBPRC filter accordion.
(function ($, Drupal) {
  Drupal.behaviors.filterAccordion = {
    attach(context) {
      $(once("filterAccordion", "html", context).forEach((element) => {
      const resourceLabel = $("fieldset[data-drupal-selector=edit-field-ebp-resource-type-target-id] legend");
      const resourcePortal = $(".views-exposed-form fieldset[data-drupal-selector=edit-field-ebp-portal-target-id]");
      const resourceContent = resourceLabel.next();
      const windowWidth = $(window).width();
      const results = $("h2.search-results");
      // For iPad & smaller devices.
      if (windowWidth <= 863) {
        $(resourceLabel).addClass("active").next().slideUp();
        $(once("", resourceLabel)).click((e) => {
          e.preventDefault();
          resourcePortal.css({ "min-height": `${270}px` });
          results.css({ top: `${504}px` });
          const dropDown = $(this).closest("div").find(resourceContent);
          $(this)
            .closest(resourceLabel)
            .find(resourceContent)
            .not(dropDown)
            .slideDown();
          if ($(this).hasClass("active")) {
            $(this).removeClass("active");
            $(resourcePortal).css({ "min-height": `${450}px` });
            results.css({ top: `${685}px` });
          } else {
            $(this)
              .closest(resourceLabel)
              .find(".active")
              .removeClass("active");
            $(this).addClass("active");
          }
          dropDown.stop(false, true).slideToggle(0);
        });
      }

      $(
        ".views-exposed-form div[data-drupal-selector=edit-actions] input[value=Reset]"
      ).on("click keypress", (e) => {
        e.stopPropagation();
        e.preventDefault();
        window.location.href =
          window.location.origin + window.location.pathname;
      });
      }));
    }
  };
})(jQuery, Drupal);

// Responsive image maps
(function ($, Drupal) {
  Drupal.behaviors.imageMaps = {
    attach(context) {
      $(once("imageMaps","img[usemap]")).rwdImageMaps();
    },
  };
})(jQuery, Drupal);