jQuery(document).ready(function ($) {
  $(window).on("scroll", function () {
    if ($(this).scrollTop() > 100) {
      $(".back-to-top a").fadeIn();
    } else {
      $(".back-to-top a").fadeOut();
    }
  });

  $(".back-to-top a").on("click", function (e) {
    e.preventDefault();
    $("html, body").animate({ scrollTop: 0 }, 800);
  });

});

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

  if (!pixel_jewelry_tabTitles.length) return;

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

  // Mouse click handling (FIXED)
  document.addEventListener("click", (e) => {
    const pixel_jewelry_clickedTab = e.target.closest(".tab-title");
    if (pixel_jewelry_clickedTab) {
      const pixel_jewelry_clickedIndex = pixel_jewelry_tabTitles.indexOf(pixel_jewelry_clickedTab);
      if (pixel_jewelry_clickedIndex !== -1) {
        pixel_jewelry_setActive(pixel_jewelry_clickedIndex);
      }
    }
  });

  // Keyboard navigation
  document.addEventListener("keydown", (e) => {
    const pixel_jewelry_activeIndex = pixel_jewelry_tabTitles.findIndex(tab => tab.classList.contains("active"));
    let pixel_jewelry_newIndex = pixel_jewelry_activeIndex;

    if (e.key === "ArrowRight") {
      pixel_jewelry_newIndex = (pixel_jewelry_activeIndex + 1) % pixel_jewelry_tabTitles.length;
      pixel_jewelry_tabTitles[pixel_jewelry_newIndex].focus();
      pixel_jewelry_setActive(pixel_jewelry_newIndex);
    } else if (e.key === "ArrowLeft") {
      pixel_jewelry_newIndex = (pixel_jewelry_activeIndex - 1 + pixel_jewelry_tabTitles.length) % pixel_jewelry_tabTitles.length;
      pixel_jewelry_tabTitles[pixel_jewelry_newIndex].focus();
      pixel_jewelry_setActive(pixel_jewelry_newIndex);
    } else if (e.key === "Enter" || e.key === " ") {
      const focusedIndex = pixel_jewelry_tabTitles.indexOf(document.activeElement);
      if (focusedIndex !== -1) pixel_jewelry_setActive(focusedIndex);
    }
  });

  function pixel_jewelry_setActive(index) {
    pixel_jewelry_tabTitles.forEach((tab, i) => tab.classList.toggle("active", i === index));
    pixel_jewelry_tabContents.forEach((content, i) => content.classList.toggle("active", i === index));
  }
});

// product owl
jQuery(document).ready(function($){

    $('.woocommerce-uses-block-theme .product-available').owlCarousel({
        loop: true,
        margin: 20,
        nav: false,
        dots: false,
        autoplay: false,
        responsive: {
            0: {
                items: 1
            },
            768: {
                items: 2
            },
            1024: {
                items: 3
            },
            1200: {
                items: 4
            }
        }
    });

});

jQuery(document).ready(function($){

    $('.tab-content.owl-carousel').owlCarousel({
        loop: true,
        margin: 20,
        nav: false,
        dots: false,
        autoplay: false,
        responsive: {
            0: {
                items: 1
            },
            768: {
                items: 2
            },
            1024: {
                items: 3
            },
            1200: {
                items: 4
            }
        }
    });

});

// banner
jQuery(document).ready(function() {
  jQuery('.slider-main.owl-carousel').owlCarousel({
    loop: true,
    margin: 30,
    nav: true,
    dots: false,
    rtl: false,
    navText: [
      '<span class="custom-arrow prev-arrow"><i class="fa-solid fa-arrow-left"></i></span>',
      '<span class="custom-arrow next-arrow"><i class="fa-solid fa-arrow-right"></i></span>'
    ],
    responsive: {
      0: { 
        items: 1 
      },
      600: { 
        items: 1 
      },
      1025: { 
        items: 1
      },
      1200: { 
        items: 1
      }
    },
    autoplay: false
  });
});

