// Testimonial Section
jQuery(document).ready(function() {
  jQuery('.testimonial-section .owl-carousel').owlCarousel({
    loop: true,
    margin: 15,
    nav: true,
    navText: ["<span class='left-btn p-3'></span>", "<span class='right-btn p-3'></span>"], 
    dots: false,
    rtl: false,
    responsive: {
    0: { 
      items: 1 
    },
    768: { 
      items: 2 
    },
    992: { 
      items: 2 
    },
    1200: { 
      items: 3 
    }
  },
  autoplay: true,
  });
});

// News Section
jQuery(document).ready(function() {
  jQuery('.news-section .owl-carousel').owlCarousel({
    loop: true,
    margin: 15,
    nav: false, 
    dots: false,
    rtl: false,
    responsive: {
    0: { 
      items: 1 
    },
    768: { 
      items: 2 
    },
    992: { 
      items: 2 
    },
    1200: { 
      items: 3 
    }
  },
  autoplay: true,
  });
});

// Scroll to Top
window.onscroll = function() {
  const veterinary_hospital_button = document.querySelector('.scroll-top-box');
  if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
    veterinary_hospital_button.style.display = "block";
  } else {
    veterinary_hospital_button.style.display = "none";
  }
};

document.querySelector('.scroll-top-box a').onclick = function(event) {
  event.preventDefault();
  window.scrollTo({top: 0, behavior: 'smooth'});
};

// Toggle
document.addEventListener("DOMContentLoaded", () => {
  const veterinary_hospital_toggle = document.querySelector(".header-info-btn a"),
        veterinary_hospital_info = document.querySelector(".header-info"),
        veterinary_hospital_close = document.querySelector(".info-close-btn a");

  if (!veterinary_hospital_toggle || !veterinary_hospital_info) return;

  veterinary_hospital_toggle.onclick = () => (veterinary_hospital_info.classList.add("active"), document.body.classList.add("header-info-open"));
  veterinary_hospital_close && (veterinary_hospital_close.onclick = () => (veterinary_hospital_info.classList.remove("active"), document.body.classList.remove("header-info-open")));

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

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

  if (!veterinary_hospital_toggleBtn || !veterinary_hospital_infoBox || !veterinary_hospital_closeBtn) return;

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

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

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

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

    const focusable = veterinary_hospital_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") {
      veterinary_hospital_closeBtn.click();
    }
  });
});