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

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

    lawyer_consultant_firm_navigation()

    var lawyer_consultant_firm_primary_menu_toggle = $('#masthead .menu-toggle');
    lawyer_consultant_firm_primary_menu_toggle.on('keydown', function(e) {
        var tabKey = e.keyCode === 9;
        var shiftKey = e.shiftKey;

        if (lawyer_consultant_firm_primary_menu_toggle.hasClass('open')) {
            if (shiftKey && tabKey) {
                e.preventDefault();
                const $the_last_li = $('.main-navigation').find("li").last()
                $the_last_li.find('a').focus()
                if (!$the_last_li.hasClass('focus')) {

                    const $is_parent_on_top = true
                    let $the_parent_ul = $the_last_li.closest('ul.sub-menu')

                    let count = 0

                    while (!!$the_parent_ul.length) {
                        ++count

                        const $the_parent_li = $the_parent_ul.closest('li')

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

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

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

                }

            };
        }
    })

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

    /* -----------------------------------------
    Main Slider
    ----------------------------------------- */

    jQuery(document).ready(function($){
        var $lawyer_consultant_firm_carousel = $(".slider-section .owl-carousel");
      
        $lawyer_consultant_firm_carousel.owlCarousel({
          items: 1,
          loop: true,
          autoplay: true,
          autoplayTimeout: 5000,
          nav: false,
          dots: true  
        });
      
        // Add numbers inside dots
        $lawyer_consultant_firm_carousel.find('.owl-dot').each(function(index){
            $(this).html('<span>'+(index+1)+'</span>');
        });
    
        function lawyer_consultant_firm_updateDots() {
            $lawyer_consultant_firm_carousel.find('.owl-dot.active').not(':last-child');
        }
    
        // Run on init + slide change
        lawyer_consultant_firm_updateDots();
        $lawyer_consultant_firm_carousel.on('changed.owl.carousel', lawyer_consultant_firm_updateDots);
    });

    /* -----------------------------------------
    Scroll Top
    ----------------------------------------- */
    var lawyer_consultant_firm_scrollToTopBtn = $('.lawyer-consultant-firm-scroll-to-top');

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

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

});

document.addEventListener('DOMContentLoaded', function() {
  const lawyer_consultant_firm_header = document.querySelector('.sticky-header');
  if (lawyer_consultant_firm_header) { // Check if header exists
    window.addEventListener('scroll', function() {
      if (window.scrollY > 0) {
        lawyer_consultant_firm_header.classList.add('is-sticky');
      } else {
        lawyer_consultant_firm_header.classList.remove('is-sticky');
      }
    });
  }
});

/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const lawyer_consultant_firm_progressBar =
        document.getElementById("lawyer_consultant_firm_aster_progress_bar");
    if (!lawyer_consultant_firm_progressBar) return;
    window.addEventListener("scroll", function () {
        const lawyer_consultant_firm_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const lawyer_consultant_firm_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const lawyer_consultant_firm_scrolled =
            (lawyer_consultant_firm_scrollTop / lawyer_consultant_firm_height) * 100;
        lawyer_consultant_firm_progressBar.style.width =
            lawyer_consultant_firm_scrolled + "%";
    });
});
