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

    // Adds a search icon.
    $('.search-form input[type="submit"]').replaceWith('<button type="submit" class="search-submit" value="Search"><svg id="icon-search" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><path d="M21 21l-4.35-4.35"></path></svg></button>');

    // Sets scroll to top.
    var scroll    = $(window).scrollTop();  
    var scrollup  = $('.to-top');  

    $(window).scroll(function() {
        if ($(this).scrollTop() > 1) {
            scrollup.css({bottom:"25px"});
        } 
        else {
            scrollup.css({bottom:"-100px"});
        }
    });

    scrollup.click(function() {
        $('html, body').animate({scrollTop: '0px'}, 800);
        return false;
    });
    
});

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

    // Popular Section Carousel
    $('.popular-review-carousel').owlCarousel({
        loop: true,
        margin: 20,
        nav: false,
        dots: false,
        autoplay: true,
        autoplayTimeout: 6000,
        responsive: {
            0: { items: 1 },
            576: { items: 2 },
            768: { items: 3 },
            1100: { items: 4 }
        }
    });

    // Blog Hero Slider
    var owl = $('.blog-hero-slider');

    owl.owlCarousel({
      items:1,
      loop:true,
      nav:false,
      dots:false,
      autoplay: false,
      autoplayTimeout: 6000
    });

    // Custom Nav
    $('.hero-next').click(function() {
        owl.trigger('next.owl.carousel');
    });

    $('.hero-prev').click(function() {
        owl.trigger('prev.owl.carousel');
    });

    // Categories Carousel
    var catOwl = $('.categories-carousel');

    catOwl.owlCarousel({
        loop: true,
        margin: 15,
        nav: false,
        dots: false,
        responsive:{
            0:{ items:1 },
            576:{ items:2 },
            768:{ items:3 },
            992:{ items:4 }
        }
    });

    // Custom Nav
    $('.cat-next').click(function() {
        catOwl.trigger('next.owl.carousel');
    });

    $('.cat-prev').click(function() {
        catOwl.trigger('prev.owl.carousel');
    });

});
// Search js

document.addEventListener('DOMContentLoaded', function () {
  const searchIcon = document.getElementById('search-icon');
  const searchPopup = document.getElementById('search-popup');
  const closeBtn = document.querySelector('.close-popup');

  // bail early if required elements are missing
  if (!searchIcon || !searchPopup) {
    // nothing to do, avoid null dereference
    return;
  }

  const getFocusables = () => searchPopup.querySelectorAll('input, button');
  
  function blogging_hub_openPopup() {
    searchPopup.style.display = 'block';
    const input = searchPopup.querySelector('input[name="s"]');
    input && input.focus();
  }

  function blogging_hub_closePopup() {
    searchPopup.style.display = 'none';
    searchIcon.focus(); // Return focus
  }

  // Open popup
  searchIcon.addEventListener('click', blogging_hub_openPopup);
  searchIcon.addEventListener('keydown', function (e) {
    if (e.key === 'Enter' || e.key === ' ') {
      e.preventDefault();
      blogging_hub_openPopup();
    }
  });

  // Close popup on close button (closeBtn might be null so guard)
  if (closeBtn) {
    closeBtn.addEventListener('click', blogging_hub_closePopup);
  }

  // Close popup on outside click
  window.addEventListener('click', function (e) {
    if (e.target === searchPopup) {
      blogging_hub_closePopup();
    }
  });

  // ESC + Tab trap
  window.addEventListener('keydown', function (e) {
    if (searchPopup.style.display === 'block') {
      // ESC to close
      if (e.key === 'Escape') {
        blogging_hub_closePopup();
        return;
      }

      // Trap Tab focus
      const focusables = getFocusables();
      const first = focusables[0];
      const last = focusables[focusables.length - 1];

      if (e.key === 'Tab') {
        if (focusables.length === 0) return;

        if (e.shiftKey) {
          if (document.activeElement === first) {
            e.preventDefault();
            last.focus();
          }
        } else {
          if (document.activeElement === last) {
            e.preventDefault();
            first.focus();
          }
        }
      }
    }
  });
});