jQuery(function($) {

    /* -----------------------------------------
    Preloader
    ----------------------------------------- */
    $('#preloader').delay(1000).fadeOut();
    $('#loader').delay(1000).fadeOut("slow");

    /* -----------------------------------------
    Navigation
    ----------------------------------------- */
    $('.menu-toggle').click(function() {
        $(this).toggleClass('open');
    });

    /* -----------------------------------------
    Keyboard Navigation
    ----------------------------------------- */
    $(window).on('load resize', clothing_marketplace_navigation)

    function clothing_marketplace_navigation(event) {
        if ($(window).width() < 1200) {
            $('.main-navigation').find("li").last().bind('keydown', function(e) {
                if (e.clothing_marketplace_shiftKey && e.which === 9) {
                    if ($(this).hasClass('focus')) {
                    }

                } else if (e.which === 9) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }                
            })
        } else {
            $('.main-navigation').find("li").unbind('keydown')
        }
    }

    clothing_marketplace_navigation()

    var clothing_marketplace_primary_menu_toggle = $('#masthead .menu-toggle');
    clothing_marketplace_primary_menu_toggle.on('keydown', function(e) {
        var clothing_marketplace_tabKey = e.keyCode === 9;
        var clothing_marketplace_shiftKey = e.clothing_marketplace_shiftKey;

        if (clothing_marketplace_primary_menu_toggle.hasClass('open')) {
            if (clothing_marketplace_shiftKey && clothing_marketplace_tabKey) {
                e.preventDefault();
                const $clothing_marketplace_the_last_li = $('.main-navigation').find("li").last()
                $clothing_marketplace_the_last_li.find('a').focus()
                if (!$clothing_marketplace_the_last_li.hasClass('focus')) {

                    const $clothing_marketplace_is_parent_on_top = true
                    let $clothing_marketplace_the_parent_ul = $clothing_marketplace_the_last_li.closest('ul.sub-menu')

                    let clothing_marketplace_count = 0

                    while (!!$clothing_marketplace_the_parent_ul.length) {
                        ++clothing_marketplace_count

                        const $clothing_marketplace_the_parent_li = $clothing_marketplace_the_parent_ul.closest('li')

                        if (!!$clothing_marketplace_the_parent_li.length) {
                            $clothing_marketplace_the_parent_li.addClass('focus')
                            $clothing_marketplace_the_parent_ul = $clothing_marketplace_the_parent_li.closest('ul.sub-menu')

                            // Blur the cross
                            $(this).blur()
                            $clothing_marketplace_the_last_li.addClass('focus')
                        }

                        if (!$clothing_marketplace_the_parent_ul.length) {
                            break;
                        }
                    }

                }

            };
        }
    })

    // Determine if the document is RTL
    var isRtl = $('html').attr('dir') === 'rtl';

  /* -----------------------------------------
  Services Section
  ----------------------------------------- */

    jQuery(document).ready(function($) {
        var $clothing_marketplace_productCarousel = $('.service-section-wrapper.wc-products-section__grid');

        if ( ! $clothing_marketplace_productCarousel.length ) {
            return;
        }

        if ( typeof $.fn.owlCarousel !== 'function' ) {
            // Fallback: keep content visible even if Owl is not available.
            $clothing_marketplace_productCarousel.addClass('owl-loaded').css('display', 'block');
            return;
        }

        $clothing_marketplace_productCarousel.owlCarousel({
            loop: true,
            margin: 30,
            nav: false,
            rtl: isRtl,
            dots: false,
            autoplay: true,
            autoplayTimeout: 4000,
            autoplayHoverPause: true,
            responsive: {
                0: { items: 1, margin: 12, stagePadding: 70 },
                480: { items: 1, margin: 14, stagePadding: 70 },
                576: { items: 2, margin: 25, stagePadding: 130 },
                768: { items: 2, margin: 25, stagePadding: 140 },
                1024: { items: 3, margin: 20, stagePadding: 140 },
                1200: { items: 4, margin: 30, stagePadding: 140 },
                1550: { items: 5, margin: 30, stagePadding: 170 }
            }
        });
    });
    
    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var clothing_marketplace_scrollToTopBtn = $('.clothing-marketplace-scroll-to-top');

    $(window).scroll(function() {
        if ($(window).scrollTop() > 400) {
            clothing_marketplace_scrollToTopBtn.addClass('show');
        } else {
            clothing_marketplace_scrollToTopBtn.removeClass('show');
        }
    });

    clothing_marketplace_scrollToTopBtn.on('click', function(e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: 0
        }, '300');
    }); 

});