jQuery(function($) {

    /* -----------------------------------------
    Services Carousel
    ----------------------------------------- */
    if ($('.cm-services__carousel').length) {
        $('.cm-services__carousel').owlCarousel({
            loop       : true,
            center     : true,
            items      : 1,
            stagePadding: 200,
            margin     : 20,
            autoplay   : false,
            autoplayTimeout    : 4000,
            autoplayHoverPause : true,
            dots : false,
            nav  : false,
            responsive : {
                0   : { stagePadding: 20,  margin: 12 },
                576 : { stagePadding: 100,  margin: 16 },
                768 : { stagePadding: 150, margin: 20 },
                1024: { stagePadding: 220, margin: 25 },
                1280: { stagePadding: 300, margin: 30 }
            }
        });
    }

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

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

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

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

    electrical_services_navigation()

    var electrical_services_primary_menu_toggle = $('#masthead .menu-toggle');
    electrical_services_primary_menu_toggle.on('keydown', function(e) {
        var electrical_services_tabKey = e.keyCode === 9;
        var electrical_services_shiftKey = e.electrical_services_shiftKey;

        if (electrical_services_primary_menu_toggle.hasClass('open')) {
            if (electrical_services_shiftKey && electrical_services_tabKey) {
                e.preventDefault();
                const $electrical_services_the_last_li = $('.main-navigation').find("li").last()
                $electrical_services_the_last_li.find('a').focus()
                if (!$electrical_services_the_last_li.hasClass('focus')) {

                    const $electrical_services_is_parent_on_top = true
                    let $electrical_services_the_parent_ul = $electrical_services_the_last_li.closest('ul.sub-menu')

                    let electrical_services_count = 0

                    while (!!$electrical_services_the_parent_ul.length) {
                        ++electrical_services_count

                        const $electrical_services_the_parent_li = $electrical_services_the_parent_ul.closest('li')

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

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

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

                }

            };
        }
    })

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

    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var electrical_services_scrollToTopBtn = $('.electrical-services-scroll-to-top');

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

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

});