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

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

    events_and_celebrations_navigation()

    var events_and_celebrations_primary_menu_toggle = $('#masthead .menu-toggle');
    events_and_celebrations_primary_menu_toggle.on('keydown', function(e) {
        var events_and_celebrations_tabKey = e.keyCode === 9;
        var events_and_celebrations_shiftKey = e.events_and_celebrations_shiftKey;

        if (events_and_celebrations_primary_menu_toggle.hasClass('open')) {
            if (events_and_celebrations_shiftKey && events_and_celebrations_tabKey) {
                e.preventDefault();
                const $events_and_celebrations_the_last_li = $('.main-navigation').find("li").last()
                $events_and_celebrations_the_last_li.find('a').focus()
                if (!$events_and_celebrations_the_last_li.hasClass('focus')) {

                    const $events_and_celebrations_is_parent_on_top = true
                    let $events_and_celebrations_the_parent_ul = $events_and_celebrations_the_last_li.closest('ul.sub-menu')

                    let events_and_celebrations_count = 0

                    while (!!$events_and_celebrations_the_parent_ul.length) {
                        ++events_and_celebrations_count

                        const $events_and_celebrations_the_parent_li = $events_and_celebrations_the_parent_ul.closest('li')

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

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

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

                }

            };
        }
    })

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

    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var events_and_celebrations_scrollToTopBtn = $('.events-and-celebrations-scroll-to-top');

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

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

    // last two words
    jQuery(".sale-banner__content .sale-banner__title").html(function(){  
        var textArray = jQuery(this).text().trim().split(" ");
        
        if (textArray.length > 1) {
            var lastTwo = textArray.splice(-1).join(" "); 
            return textArray.join(" ") + ` <span class='banner-text'>${lastTwo}</span>`;
        } else {
            return `<span class='banner-text'>${textArray.join(" ")}</span>`;
        }
    });

    if(document.getElementById("openModalButton")) {
        if(document.getElementById("openModalButton").getAttribute('data-modal-src')) {
            var modal=document.getElementById("myModal")
            var openModalButton=document.getElementById("openModalButton")
            var closeModalButton=document.getElementById("closeModalButton");
            openModalButton.addEventListener("click",function(){
            let e=jQuery(this).attr("data-modal-src");
            document.getElementById("videoPlayer").src=e
            modal.style.display="block"
            })
            closeModalButton.addEventListener("click",function(){
            document.getElementById("videoPlayer").src=""
            modal.style.display="none"
            })
            window.addEventListener("click",function(e){
            if(e.target==modal) {
                modal.style.display="none"
            } else {
                document.getElementById("videoPlayer").src=""
            }
            });
        }
    }

});