// Scroll to Top
document.addEventListener('DOMContentLoaded', () => {
  const montessori_kindergarten_btn = document.body.appendChild(document.createElement('button'));
  montessori_kindergarten_btn.className = 'return-to-top-btn';
  montessori_kindergarten_btn.innerHTML = '<span class="dashicons dashicons-arrow-up-alt"></span>';

  window.addEventListener('scroll', () =>
    montessori_kindergarten_btn.classList.toggle('show', window.scrollY > 300)
  );

  montessori_kindergarten_btn.onclick = () => window.scrollTo({ top: 0, behavior: 'smooth' });
});

// Main Slider Js
jQuery(document).ready(function(){
  var owl = jQuery('.owl-carousel');
    owl.owlCarousel({
    margin: 20,
    nav: true,
    navText: ["<span class='dashicons dashicons-arrow-left-alt2'></span>", "<span class='dashicons dashicons-arrow-right-alt2'></span>"],
    autoplay: true,
    autoplayTimeout: 3000,
    lazyLoad: true,
    loop: true,
    dots: false,
    responsive: {
      0: {
        items: 1
      },
      600: {
        items: 1
      },
      1000: {
        items: 1
      }
    },
    mouseDrag: true
  });
});

// Toggle
document.addEventListener("DOMContentLoaded", () => {
  const montessori_kindergarten_toggle = document.querySelector(".header-open-btn a"),
        montessori_kindergarten_info = document.querySelector(".header-info"),
        montessori_kindergarten_close = document.querySelector(".header-close-btn a");

  if (!montessori_kindergarten_toggle || !montessori_kindergarten_info) return;

  montessori_kindergarten_toggle.onclick = () => (montessori_kindergarten_info.classList.add("active"), document.body.classList.add("header-info-open"));
  montessori_kindergarten_close && (montessori_kindergarten_close.onclick = () => (montessori_kindergarten_info.classList.remove("active"), document.body.classList.remove("header-info-open")));

  document.addEventListener("click", e => {
    if (document.body.classList.contains("header-info-open") &&
        !montessori_kindergarten_info.contains(e.target) && !montessori_kindergarten_toggle.contains(e.target)) {
      montessori_kindergarten_info.classList.remove("active");
      document.body.classList.remove("header-info-open");
    }
  });
});

document.addEventListener("DOMContentLoaded", () => {
  const montessori_kindergarten_toggleBtn = document.querySelector(".header-open-btn a"); // open button
  const montessori_kindergarten_infoBox = document.querySelector(".header-info"); // info box
  const montessori_kindergarten_closeBtn = document.querySelector(".header-close-btn a"); // close button

  if (!montessori_kindergarten_toggleBtn || !montessori_kindergarten_infoBox || !montessori_kindergarten_closeBtn) return;

  // OPEN the info box
  montessori_kindergarten_toggleBtn.addEventListener("click", () => {
    montessori_kindergarten_infoBox.classList.add("active");
    document.body.classList.add("header-info-active");

    // Optional: move keyboard focus inside
    const firstFocusable = montessori_kindergarten_infoBox.querySelector(
      'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
    );
    if (firstFocusable) firstFocusable.focus();
  });

  // CLOSE the info box (when clicking the close icon)
  montessori_kindergarten_closeBtn.addEventListener("click", () => {
    montessori_kindergarten_infoBox.classList.remove("active");
    document.body.classList.remove("header-info-active");
    montessori_kindergarten_toggleBtn.focus(); // return focus to the button
  });

  // Trap focus inside when active
  document.addEventListener("keydown", (e) => {
    if (!montessori_kindergarten_infoBox.classList.contains("active")) return;

    const focusable = montessori_kindergarten_infoBox.querySelectorAll(
      'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
    );
    const first = focusable[0];
    const last = focusable[focusable.length - 1];

    if (e.key === "Tab") {
      if (e.shiftKey && document.activeElement === first) {
        e.preventDefault();
        last.focus();
      } else if (!e.shiftKey && document.activeElement === last) {
        e.preventDefault();
        first.focus();
      }
    }

    // ESC key also closes the box
    if (e.key === "Escape") {
      montessori_kindergarten_closeBtn.click();
    }
  });
});