(function( $ ) {
	'use strict';
    
    $(document).ready(function() {
        $('.olympus-main-nav-item-submenu-trigger').each(function(){
            const dropdown = $(this);
            const popup = $(this).next('.olympus-main-nav-item-submenu');
            dropdown.hover(
                function(){
                    popup.fadeIn(200);
                },
                function(){
                    if(!popup.is(":hover")){
                        popup.fadeOut(200);
                    }
                }
            );

            dropdown.click(function(){
                if(popup.is(':visible') && popup.css('opacity') == 1){
                    popup.fadeOut(200);
                }else{
                    popup.fadeIn(200);
                }
            });

            dropdown.focusin(function(){
                popup.fadeIn(200);
            });

            dropdown.focusout(function(){
                popup.fadeOut(10);
            });

            popup.find('.main-nav-popup-inside-link').each(function(){
                $(this).focusin(function(){
                    popup.fadeIn(10);
                });
            });

            popup.find('.main-nav-popup-inside-link').each(function(){
                $(this).focusout(function(){
                    popup.fadeOut(10);
                });
            });
        });

        $('.olympus-main-nav-item-submenu').each(function(){
            const dropdown = $(this).prev('.olympus-main-nav-item-submenu-trigger');
            $(this).hover(
                function(){
                    
                },
                function(){
                    const popup = $(this);
                    if(!dropdown.is(":hover")){
                        popup.fadeOut(200);
                    }
                }
            );
        });

        const nav = $('.olympus-main-nav');
        nav.find('.olympus-mobile-nav-toggle').first().on('click', function(){
            const mobileMenu = nav.find('.olympus-main-nav-mobile').first();

            if($(this).hasClass('open')){
                $(this).removeClass('open');
                mobileMenu.slideUp(200);
            }else{
                $(this).addClass('open');
                mobileMenu.slideDown(200);
            }
        });

        nav.find('.main-nav-mobile-dropdown-toggle').each(function(){
            const mobileDropdownToggle = $(this);
            const mobileDropdown = mobileDropdownToggle.parent('.main-nav-primary-link-mobile').next('.main-nav-mobile-dropdown-content-container');
            mobileDropdownToggle.on('click',function(){
                if(mobileDropdownToggle.hasClass('open')){
                    mobileDropdownToggle.removeClass('open');
                    mobileDropdown.slideUp(200);
                }else{
                    mobileDropdownToggle.addClass('open');
                    mobileDropdown.slideDown(200);
                }
            });
        });

    });

})( jQuery );