jQuery(function($) {
  "use strict";

  // Define the functions in the global scope
  window.food_blogger_hub_menu_open_nav = function() {
    $(".sidenav").addClass('open');
  };

  window.food_blogger_hub_menu_close_nav = function() {
    $(".sidenav").removeClass('open');
  };

  // Search focus handler
  function searchFocusHandler() {
    const searchFirstTab = $('.inner_searchbox input[type="search"]');
    const searchLastTab = $('button.search-close');

    $(".open-search").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $('body').addClass("search-focus");
      searchFirstTab.focus();
    });

    $("button.search-close").click(function(e) {
      e.preventDefault();
      e.stopPropagation();
      $('body').removeClass("search-focus");
      $(".open-search").focus();
    });

    // Redirect last tab to first input
    searchLastTab.on('keydown', function(e) {
      if ($('body').hasClass('search-focus') && e.which === 9 && !e.shiftKey) {
        e.preventDefault();
        searchFirstTab.focus();
      }
    });

    // Redirect first shift+tab to last input
    searchFirstTab.on('keydown', function(e) {
      if ($('body').hasClass('search-focus') && e.which === 9 && e.shiftKey) {
        e.preventDefault();
        searchLastTab.focus();
      }
    });

    // Allow escape key to close menu
    $('.inner_searchbox').on('keyup', function(e) {
      if ($('body').hasClass('search-focus') && e.keyCode === 27) {
        $('body').removeClass('search-focus');
        searchLastTab.focus();
      }
    });
  }

  // Call the search focus handler
  searchFocusHandler();

  // Preloader
  $(document).ready(function() {
    setTimeout(function() {
      $(".loader").fadeOut("slow");
    }, 1000);
  });

  // Scroll to top
  $(window).scroll(function() {
    if ($(this).scrollTop() >= 50) {
      $('#return-to-top').fadeIn(200);
    } else {
      $('#return-to-top').fadeOut(200);
    }
  });

  $('#return-to-top').click(function() {
    $('body,html').animate({
      scrollTop: 0
    }, 500);
  });
});

jQuery(document).ready(function($) {
  // Initialize the .slide-cat slider
  $('.slide-cat .owl-carousel').owlCarousel({
    loop: false,
    margin: 20,
    nav: false,
    dots: false,
    autoplay: false,
    autoplayTimeout: 3000,
    autoplayHoverPause: true,
    items: 5,
    responsive: {
      0: { items: 3 },
      768: { items: 3 }
    }
  }).on('initialized.owl.carousel', function() {
  });

  // Initialize the .slider-slide-content slider
  $('.slideimg .owl-carousel').owlCarousel({
    loop: false,
    margin: 20,
    nav: true,
    dots: false,
    items: 1,
    autoplay: false,
    autoplayTimeout: 3000,
    autoplayHoverPause: true,
    onInitialized: setActiveCircle,
    onTranslated: setActiveCircle
  }).on('initialized.owl.carousel', function() {
  });

  // Click event handler for .slide-cat slider items
  $('.slide-cat .owl-carousel').on('click', '.owl-item', function(e) {
    e.preventDefault();
    var index = $(this).index();
    // Move slider-slide-content to corresponding item index
    $('.slideimg .owl-carousel').trigger('to.owl.carousel', [index, 300, true]);
  });

  function setActiveCircle() {
    var currentIndex = $('.slideimg .owl-carousel').find('.owl-item.active').index();
    $('.numbered-circles .circle').removeClass('active');
    $('.numbered-circles .circle').eq(currentIndex).addClass('active');
  }

  $('.numbered-circles .circle').on('click', function() {
    var index = $(this).data('index') - 1;
    $('.slideimg .owl-carousel').trigger('to.owl.carousel', [index, 300]);
  });
});