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

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

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

    // Side navigation toggle
    $('.gb_toggle').on('click', function() {
        packers_and_movers_Keyboard_loop($('.side_gb_nav'));
    });

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

    // Sticky menu
    $(window).on('scroll', function() {
        var data_sticky = $('.menubar').data('sticky');
        if (data_sticky === true) {
            if ($(this).scrollTop() > 1) {
                $('.menubar').addClass("stick_head");
            } else {
                $('.menubar').removeClass("stick_head");
            }
        }
    });

    // slider number paginations
    $(document).ready(function () {
      var $slider = $('#slider .owl-carousel');

      if ($slider.length > 0) {
        var owl = $slider.owlCarousel({
          loop: true, 
          items: 1, 
          nav: true, 
          dots: false, 
          autoplay: false, 
        });
        // Function to update slide numbers
        function updateSlideNumbers(event) {
          var totalItems = event.item.count; // Total number of slides
          var currentIndex = event.item.index - event.relatedTarget._clones.length / 2;
          // Correct index to handle clones
          if (currentIndex < 0) {
            currentIndex = totalItems + currentIndex;
          }
          var nextIndex = (currentIndex + 1) % totalItems;
          // Update current and next slide numbers
          $('.current-number').text(('0' + ((currentIndex % totalItems) + 1)).slice(-2));
          $('.next-number').text(('0' + ((nextIndex % totalItems) + 1)).slice(-2));
        }
        // Bind the update function to the 'changed.owl.carousel' event
        owl.on('changed.owl.carousel', updateSlideNumbers);
        // Trigger the initial update
        $slider.on('initialized.owl.carousel', function (event) {
          updateSlideNumbers(event);
        });
        // Click event for the next number
        $('.next-number').on('click', function () {
          owl.trigger('next.owl.carousel');
        });
        // Click event for the current number (optional, slide to the beginning if needed)
        $('.current-number').on('click', function () {
          owl.trigger('to.owl.carousel', [0]); // Slides to the first slide
        });
      }
    });

});

// Mobile responsive menu
function packers_and_movers_menu_open_nav() {
    jQuery(".sidenav").addClass('open');
}

function packers_and_movers_menu_close_nav() {
    jQuery(".sidenav").removeClass('open');
}