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() {
        appliances_store_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");
            }
        }
    });

});

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

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

// dropdown menus
jQuery(document).ready(function(){
    // Show/hide the category dropdown
    jQuery(".category-btn").click(function(e){
        e.preventDefault();
        jQuery(".category-dropdown").toggle();
    });

    // Set the hidden input value based on the selected category
    jQuery(".category-dropdown a").click(function(e){
        e.preventDefault();
        var categoryId = jQuery(this).data("id");
        jQuery("#categoryInput").val(categoryId);
        jQuery(".category-btn").text(jQuery(this).text() + ' ').append('<i class="fas fa-chevron-down ps-5"></i>');
        jQuery(".category-dropdown").hide();
    });

    // Handle focus using Tab and Shift+Tab
    jQuery(".category-btn, .category-dropdown").on("keydown", function(e) {
        var dropdownItems = jQuery(".category-dropdown").find("a");
        
        if (e.keyCode === 9) { // Tab key
            if (!e.shiftKey && document.activeElement === dropdownItems.last().get(0)) {
                e.preventDefault();
                jQuery(".category-btn").focus();
            } else if (e.shiftKey && document.activeElement === dropdownItems.first().get(0)) {
                e.preventDefault();
                jQuery(".category-btn").focus();
            }
        }
    });
});

// tab js
function appliances_store_projetcs_tab(evt, tabId) {
    // Hide all tab content elements
    jQuery('.tabcontent').hide();
    
    // Show the selected tab content
    jQuery('#' + tabId).show();

    // Destroy any existing Owl Carousel instance in the current tab
    jQuery('.tabcontent .owl-carousel').trigger('destroy.owl.carousel');
    
    // Initialize Owl Carousel only in the active tab content
    jQuery('#' + tabId + ' .owl-carousel').owlCarousel({
        loop: true,
        margin: 10,
        nav: true,
        responsive: {
            0: { items: 1 },
            600: { items: 2 },
            1000: { items: 3 }
        }
    });

    // Remove 'active' class from all tab buttons
    jQuery('.tab button').removeClass('active');
    
    // Add 'active' class to the clicked tab button
    jQuery(evt.currentTarget).addClass('active');
}

// On page load, trigger click on the first tab to initialize the first carousel
jQuery(document).ready(function($) {
    $('.tab button').first().trigger('click');

    if (jQuery('.prod-box .tabcontent:first').length) {

        // Hide all tab content elements
        jQuery('.tabcontent').hide();
        
        // Show the selected tab content
        jQuery('.prod-box .tabcontent:first').show();
    
        // Destroy any existing Owl Carousel instance in the current tab
        jQuery('.prod-box .tabcontent:first .owl-carousel').trigger('destroy.owl.carousel');
        
        jQuery('.prod-box .tabcontent:first .owl-carousel').owlCarousel({
            loop: true,
            margin: 10,
            nav: true,
            responsive: {
                0: { items: 1 },
                600: { items: 2 },
                1000: { items: 3 }
            }
        });
    }
});

jQuery(document).ready(function($) {
    $('.main-tab-col .tab-carousel').owlCarousel({
        loop: false,
        margin: 10,
        nav: false,
        dots: false,
        autoplay: false,
        responsive: {
            0: {
                items: 1 // Show 1 item from 0 to 767
            },
            768: {
                items: 3 // Show 3 items from 768 to 999
            },
            1000: {
                items: 4 // Show 4 items from 1000 to 1699
            },
            1700: {
                items: 5 // Show 5 items for 1700 and above
            }
        }
    });
});

// timer js
jQuery(document).ready(function(){
    jQuery(".product-cat").hide();
    jQuery("button.product-btn").click(function(){
        jQuery(".product-cat").toggle();
    });

    var appliances_store_mydate = jQuery('.date').val();
    jQuery(".countdown").each(function(){
        appliances_store_countdown(jQuery(this), appliances_store_mydate);
    });
});

function appliances_store_countdown($timer, appliances_store_mydate) {
    // Set the date we're counting down to
    var appliances_store_countDownDate = new Date(appliances_store_mydate).getTime();
    
    // Update the countdown every 1 second
    var x = setInterval(function() {
        var now = new Date().getTime(); // Current time
        var distance = appliances_store_countDownDate - now; // Time difference

        // Time calculations
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Add leading zeros if needed
        days = (days < 10) ? "0" + days : days;
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;

        // Display the countdown in DD:HH:MM:SS format
        $timer.html(days + ":" + hours + ":" + minutes + ":" + seconds);

        // If the countdown is over
        if (distance < 0) {
            clearInterval(x);
            $timer.html("SALE EXPIRED");
        }
    }, 1000);
}