// Slider Section
jQuery(document).ready(function($) {
  const section = $('.main-banner-section');
  const owl = section.find('.owl-carousel').owlCarousel({
    loop: true,
    margin: 15,
    nav: false,
    dots: false,
    rtl: false,
    items: 1,
    autoplay: true,
  });

  // Custom dots (image thumbnails)
  const dots = section.find('.custom-dots').empty();
  section.find('.owl-item:not(.cloned)').each(function(i) {
    const img = $(this).find('img').attr('src');

    if (img) {
      dots.append(
        `<button class="dot" data-index="${i}">
          <img src="${img}" alt="thumb-${i}">
        </button>`
      );
    }
  });

  // Click on thumbnail
  dots.on('click', '.dot', function() {
    owl.trigger('to.owl.carousel', [$(this).data('index'), 300]);
  });

  // Active thumbnail update
  owl.on('changed.owl.carousel', function(e) {
    const index = (e.item.index - e.relatedTarget._clones.length / 2 + e.item.count) % e.item.count;
    dots.find('.dot').removeClass('active').eq(index).addClass('active');
  });

  // Set first active
  dots.find('.dot').eq(0).addClass('active');

});

// Product Section
jQuery(document).ready(function() {
  jQuery('.product-section .owl-carousel').owlCarousel({
    loop: true,
    margin: 30,
    nav: true,
    navText: ["<span class='dashicons dashicons-arrow-left-alt'></span>", "<span class='dashicons dashicons-arrow-right-alt'></span>"], 
    dots: true,
    rtl: false,
    responsive: {
    0: { 
      items: 1 
    },
    768: { 
      items: 2 
    },
    992: { 
      items: 3 
    },
    1400: { 
      items: 4 
    }
  },
  autoplay: true,
  });
});


// products tabs
document.addEventListener("DOMContentLoaded", () => {
  const bee_elegant_interior_tabTitles = [...document.querySelectorAll(".tab-title")];
  const bee_elegant_interior_tabContents = [...document.querySelectorAll(".tab-content")];

  if (!bee_elegant_interior_tabTitles.length) return;

  // Default: activate first tab
  bee_elegant_interior_tabTitles.forEach((tab, index) => {
    tab.classList.toggle("active", index === 0);
    tab.setAttribute("tabindex", "0"); // make focusable by keyboard
  });
  bee_elegant_interior_tabContents.forEach((content, index) => {
    content.classList.toggle("active", index === 0);
  });

  // Mouse click handling (FIXED)
  document.addEventListener("click", (e) => {
    const bee_elegant_interior_clickedTab = e.target.closest(".tab-title");
    if (bee_elegant_interior_clickedTab) {
      const bee_elegant_interior_clickedIndex = bee_elegant_interior_tabTitles.indexOf(bee_elegant_interior_clickedTab);
      if (bee_elegant_interior_clickedIndex !== -1) {
        bee_elegant_interior_setActive(bee_elegant_interior_clickedIndex);
      }
    }
  });

  // Keyboard navigation
  document.addEventListener("keydown", (e) => {
    const bee_elegant_interior_activeIndex = bee_elegant_interior_tabTitles.findIndex(tab => tab.classList.contains("active"));
    let bee_elegant_interior_newIndex = bee_elegant_interior_activeIndex;

    if (e.key === "ArrowRight") {
      bee_elegant_interior_newIndex = (bee_elegant_interior_activeIndex + 1) % bee_elegant_interior_tabTitles.length;
      bee_elegant_interior_tabTitles[bee_elegant_interior_newIndex].focus();
      bee_elegant_interior_setActive(bee_elegant_interior_newIndex);
    } else if (e.key === "ArrowLeft") {
      bee_elegant_interior_newIndex = (bee_elegant_interior_activeIndex - 1 + bee_elegant_interior_tabTitles.length) % bee_elegant_interior_tabTitles.length;
      bee_elegant_interior_tabTitles[bee_elegant_interior_newIndex].focus();
      bee_elegant_interior_setActive(bee_elegant_interior_newIndex);
    } else if (e.key === "Enter" || e.key === " ") {
      const focusedIndex = bee_elegant_interior_tabTitles.indexOf(document.activeElement);
      if (focusedIndex !== -1) bee_elegant_interior_setActive(focusedIndex);
    }
  });

  function bee_elegant_interior_setActive(index) {
    bee_elegant_interior_tabTitles.forEach((tab, i) => tab.classList.toggle("active", i === index));
    bee_elegant_interior_tabContents.forEach((content, i) => content.classList.toggle("active", i === index));
  }
});

//  Single Product Link
document.addEventListener("click", function(e) {
  const bee_elegant_interior_btn = e.target.closest(".product-section .product-box .product-btn");
  if (!bee_elegant_interior_btn) return;

  const bee_elegant_interior_productItem = bee_elegant_interior_btn.closest("li.product");
  if (!bee_elegant_interior_productItem) return;

  const bee_elegant_interior_productLink = bee_elegant_interior_productItem.querySelector("a[href*='/product/']");
  if (bee_elegant_interior_productLink) {
    window.location.href = bee_elegant_interior_productLink.getAttribute("href");
  }
});

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

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