// SAMHSA - Functionality isolated to regional offices map: /about-us/who-we-are/regional-offices
(function ($, Drupal) {
  Drupal.behaviors.regionalOffices = {
    attach(context) {
      $(once("regionalOffices", "html", context).forEach((element) => {
        (($) => {
          $(document).ready(() => {
            if ($("div").hasClass("grid-col-12")) {
              $(`map[name="sr-image-map"] area`).each((i, j) => {
                j = i + 1;
                $(`#sr-id-${j}`).on("mouseenter", (e) => {
                  $(`.sr-tooltip-${j}`)
                    .parents()
                    .siblings()
                    .children(".sr-tooltip")
                    .css("display", "none");
                  if (j >= 1 && j <= 4) {
                    $(`.sr-tooltip-${j}`).css({
                      left:
                        e.pageX -
                        ($(".sr-image-map").offset().left +
                          $(`.sr-tooltip-${j}`).width()),
                      top: e.pageY - $(".sr-image-map").offset().top,
                      display: "block",
                    });
                  } else if (j >= 5 && j <= 7) {
                    $(`.sr-tooltip-${j}`).css({
                      left:
                        e.pageX -
                        ($(".sr-image-map").offset().left +
                          $(`.sr-tooltip-${j}`).width() / 2),
                      top: e.pageY - $(".sr-image-map").offset().top,
                      display: "block",
                    });
                  } else {
                    $(`.sr-tooltip-${j}`).css({
                      left: e.pageX - $(".sr-image-map").offset().left,
                      top: e.pageY - $(".sr-image-map").offset().top,
                      display: "block",
                    });
                  }
                });
              });
            } else {
              $(`map[name="sr-image-map"] area`).each((i, j) => {
                j = i + 1;
                $(`#sr-id-${j}`).on("mouseenter", (e) => {
                  $(`.sr-tooltip-${j}`)
                    .parents()
                    .siblings()
                    .children(".sr-tooltip")
                    .css("display", "none");
                  if (j >= 1 && j <= 4) {
                    $(`.sr-tooltip-${j}`).css({
                      left: e.pageX - $(`.sr-tooltip-${j}`).width(),
                      top: e.pageY - 10,
                      display: "block",
                    });
                  } else if (j >= 5 && j <= 7) {
                    $(`.sr-tooltip-${j}`).css({
                      left: e.pageX - $(`.sr-tooltip-${j}`).width() / 2,
                      top: e.pageY - 10,
                      display: "block",
                    });
                  } else {
                    $(`.sr-tooltip-${j}`).css({
                      left: e.pageX - 10,
                      top: e.pageY - 10,
                      display: "block",
                    });
                  }
                });
              });
            }

            $(".sr-image-map").on("mouseleave", () => {
              $(".sr-tooltip").css("display", "none");
            });

            $(document).ready(() => {
              $(".sr-image-map").rwdImageMaps();
            });
          });
        })(jQuery);
      }));
    }
  }
})(jQuery, Drupal);