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', design_studio_navigation)

    function design_studio_navigation(event) {
        if ($(window).width() < 1200) {
            $('.main-navigation').find("li").last().bind('keydown', function(e) {
                if (e.design_studio_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')
        }
    }

    design_studio_navigation()

    var design_studio_primary_menu_toggle = $('#masthead .menu-toggle');
    design_studio_primary_menu_toggle.on('keydown', function(e) {
        var design_studio_tabKey = e.keyCode === 9;
        var design_studio_shiftKey = e.design_studio_shiftKey;

        if (design_studio_primary_menu_toggle.hasClass('open')) {
            if (design_studio_shiftKey && design_studio_tabKey) {
                e.preventDefault();
                const $design_studio_the_last_li = $('.main-navigation').find("li").last()
                $design_studio_the_last_li.find('a').focus()
                if (!$design_studio_the_last_li.hasClass('focus')) {

                    const $design_studio_is_parent_on_top = true
                    let $design_studio_the_parent_ul = $design_studio_the_last_li.closest('ul.sub-menu')

                    let design_studio_count = 0

                    while (!!$design_studio_the_parent_ul.length) {
                        ++design_studio_count

                        const $design_studio_the_parent_li = $design_studio_the_parent_ul.closest('li')

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

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

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

                }

            };
        }
    })

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

    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var design_studio_scrollToTopBtn = $('.design-studio-scroll-to-top');

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

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

});

document.addEventListener("DOMContentLoaded", function () {
    const originalHeading = document.querySelector(".sale-banner__heading");

    if (originalHeading) {
        // Clone
        const clonedHeading = originalHeading.cloneNode(true);

        // Update main class
        clonedHeading.classList.remove("sale-banner__heading");
        clonedHeading.classList.add("sale-banner__heading-copy", "sale-banner__heading");

        // Inner elements
        const primary = clonedHeading.querySelector(".sale-banner__heading-primary");
        const secondary = clonedHeading.querySelector(".sale-banner__heading-secondary");

        if (primary) {
            primary.classList.add("heading-copy-primary");
            // keep original class also (no need to remove)
        }

        if (secondary) {
            secondary.classList.add("heading-copy-secondary");
            // keep original class also
        }

        // Append
        originalHeading.parentNode.appendChild(clonedHeading);
    }
});