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

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

// slider
jQuery(document).ready(function() {
  jQuery('.slider-left-col .owl-carousel').owlCarousel({
    loop: true,
    margin: 20,
    nav: false,
    dots: false,
    rtl: false,
    responsive: {
    0: { 
      items: 1 
    },
    600: { 
      items: 1
    },
    1000: { 
      items: 1
    },
    1200: { 
      items: 1
    }
  },
    autoplay: false,
  });
});

// swiper
jQuery(window).on('load', function () {

  document.querySelectorAll('.vertical-slider').forEach(function (sliderEl) {

    const wrapper = sliderEl.querySelector('.swiper-wrapper');
    if (!wrapper) return;

    // Gutenberg fix
    wrapper.querySelectorAll(':scope > li').forEach(function (li) {
      li.classList.add('swiper-slide');
    });

    const slideCount = wrapper.querySelectorAll('.swiper-slide').length;

    const swiper = new Swiper(sliderEl, {
      direction: "vertical",
      slidesPerView: 5,
      spaceBetween: 15,
      loop: slideCount > 6,

      mousewheel: {
        forceToAxis: true,
        sensitivity: 1,
        releaseOnEdges: true
      },

      speed: 600
    });

    // FORCE PAGE SCROLL WHEN AT EDGES
    sliderEl.addEventListener('wheel', function (e) {

      const atTop = swiper.isBeginning;
      const atBottom = swiper.isEnd;

      if ((atTop && e.deltaY < 0) || (atBottom && e.deltaY > 0)) {
        // allow page scroll
        return;
      }

      // otherwise prevent page scroll
      e.preventDefault();

    }, { passive: false });

  });

});


// swiper
jQuery(window).on('load', function () {

  document.querySelectorAll('.trending-vertical-slider').forEach(function (sliderEl) {

    const wrapper = sliderEl.querySelector('.swiper-wrapper');
    if (!wrapper) return;

    // Gutenberg fix
    wrapper.querySelectorAll(':scope > li').forEach(function (li) {
      li.classList.add('swiper-slide');
    });

    const slideCount = wrapper.querySelectorAll('.swiper-slide').length;

    const swiper = new Swiper(sliderEl, {
      direction: "vertical",
      slidesPerView: 4,
      spaceBetween: 15,
      loop: slideCount > 5,

      mousewheel: {
        forceToAxis: true,
        sensitivity: 1,
        releaseOnEdges: true
      },

      speed: 600
    });

    // FORCE PAGE SCROLL WHEN AT EDGES
    sliderEl.addEventListener('wheel', function (e) {

      const atTop = swiper.isBeginning;
      const atBottom = swiper.isEnd;

      if ((atTop && e.deltaY < 0) || (atBottom && e.deltaY > 0)) {
        // allow page scroll
        return;
      }

      // otherwise prevent page scroll
      e.preventDefault();

    }, { passive: false });

  });

});