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

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

// Banner slider
jQuery(document).ready(function () {
  var rest_house_Slider = new Swiper(".main-slider.mySwiper", {
    slidesPerView: 1,
    speed: 1000,
    loop: false,
    grabCursor: true,
    autoplay: {
      delay: 5000,
      disableOnInteraction: false,
    },
    pagination: {
      el: ".main-slider-pagination",   // <-- fixed class
      clickable: true,
    },
    navigation: false,
  });

});

jQuery(document).ready(function ($) {
  // Target your query loop wrapper
  const $wrapper = $(".popular-dormitories-section .wp-block-post-template");

  // Add Swiper wrapper class
  $wrapper.addClass("swiper-wrapper");

  // Add Swiper slide class to each li
  $wrapper.find("li").addClass("swiper-slide");

  // Initialize Swiper
  var swiper = new Swiper(".popular-dormitories-section .mySwiper", {
    slidesPerView: 4,
    spaceBetween: 30,
    loop: false,
    speed: 800,
    autoplay: {
      delay: 3000,
      disableOnInteraction: false,
    },
    navigation: {
      nextEl: ".post-swiper-button-next",
      prevEl: ".post-swiper-button-prev",
    },
    breakpoints: {
      0: {
        slidesPerView: 1, 
      },
      768: {
        slidesPerView: 2,
      },
      992: {
        slidesPerView: 4,
      }
    }
  });
});

// tabs.js

// Initialize tabs and tab contents
const frest_house_tabTiles = [...document.querySelectorAll("div.main-tab div")];
const frest_house_tabContents = [...document.querySelectorAll(".tab-content")];

// Set first tab active
frest_house_tabTiles.forEach((tabTile, index) => {
  tabTile.classList.toggle('active', index === 0);
  tabTile.querySelector('a').setAttribute('tabindex', '0'); // all tabs focusable for Tab key
});

frest_house_tabContents.forEach((tabContent, index) => {
  tabContent.classList.toggle('active', index === 0);
});

// Function to activate a tab
function frest_house_activateTab(frest_house_tabDiv) {
  const frest_house_activeIndex = frest_house_tabTiles.findIndex(tab => tab === frest_house_tabDiv);

  frest_house_tabTiles.forEach((tabTile, index) => {
    tabTile.classList.toggle('active', index === frest_house_activeIndex);
  });

  frest_house_tabContents.forEach((tabContent, index) => {
    tabContent.classList.toggle('active', index === frest_house_activeIndex);
  });

  frest_house_tabDiv.querySelector('a').focus();
}

// Click on tab
document.addEventListener("click", e => {
  const frest_house_tabDiv = e.target.closest(".tab-title");
  if (frest_house_tabDiv) {
    frest_house_activateTab(frest_house_tabDiv);
  }
});

// Optional: Enter/Space for keyboard activation
frest_house_tabTiles.forEach(tabTile => {
  const frest_house_tabLink = tabTile.querySelector('a');
  frest_house_tabLink.addEventListener('keydown', e => {
    if (e.key === 'Enter' || e.key === ' ') {
      e.preventDefault();
      frest_house_activateTab(tabTile);
    }
  });
});


// Swiper Initializer Function
jQuery(document).ready(function($) {

  function rest_house_initCustomSwiper(containerSelector, ulExtraClass, slideExtraClass, paginationSelector) {
    var $rest_house_swiperEl = $(containerSelector);

    if ($rest_house_swiperEl.find("ul.swiper-wrapper").length) {
      var $rest_house_ul = $rest_house_swiperEl.find("ul.swiper-wrapper");
      var $rest_house_slides = $rest_house_ul.find("li");

      // Create new swiper wrapper
      var $rest_house_wrapper = $("<div class='swiper-wrapper " + ulExtraClass + "'></div>");
      $rest_house_slides.each(function () {
        var $rest_house_slide = $("<div class='swiper-slide " + slideExtraClass + "'></div>");
        $rest_house_slide.html($(this).html());
        $rest_house_wrapper.append($rest_house_slide);
      });

      // Replace old UL with new Swiper wrapper
      $rest_house_ul.replaceWith($rest_house_wrapper);
    }

    // Initialize Swiper
    new Swiper(containerSelector, {
      breakpoints: {
        0: { slidesPerView: 1 },
        600: { slidesPerView: 2 },
        992: { slidesPerView: 3 }
      },
      speed: 1200,
      autoplay: {
        delay: 3000,
        disableOnMulishaction: false,
      },
      spaceBetween: 30,
      pagination: {
        el: paginationSelector,
        clickable: true,
      },
    });
  }

  // News Slider
  rest_house_initCustomSwiper(".mySwiper2", "news-box", "news-inner-box", ".news-swiper-pagination");
});
