// Menus 
function chartered_accountant_menu_open_nav() {
  jQuery(".sidenav").addClass('show');
}
function chartered_accountant_menu_close_nav() {
  jQuery(".sidenav").removeClass('show');
}

( function( window, document ) {
  function chartered_accountant_keepFocusInMenu() {
    document.addEventListener( 'keydown', function( e ) {
      const chartered_accountant_nav = document.querySelector( '.sidenav' );

      if ( ! chartered_accountant_nav || ! chartered_accountant_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...chartered_accountant_nav.querySelectorAll( 'input, a, button' )],
        chartered_accountant_lastEl = elements[ elements.length - 1 ],
        chartered_accountant_firstEl = elements[0],
        chartered_accountant_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && chartered_accountant_lastEl === chartered_accountant_activeEl ) {
        e.preventDefault();
        chartered_accountant_firstEl.focus();
      }

      if ( shiftKey && tabKey && chartered_accountant_firstEl === chartered_accountant_activeEl ) {
        e.preventDefault();
        chartered_accountant_lastEl.focus();
      }
    } );
  }
  chartered_accountant_keepFocusInMenu();
} )( window, document );

jQuery('document').ready(function($){
	// preloader
  setTimeout(function () {
		jQuery("#preloader").fadeOut("slow");
  },1000);

  // Sticky Header
  $(window).scroll(function(){
		var sticky = $('.header-sticky'),
			scroll = $(window).scrollTop();

		if (scroll >= 100) sticky.addClass('header-fixed');
		else sticky.removeClass('header-fixed');
	});

});

/*sticky copyright*/
window.addEventListener('scroll', function() {
  var sticky = document.querySelector('.copyright-sticky');
  if (!sticky) return;

  var scrollTop = window.scrollY || document.documentElement.scrollTop;
  var windowHeight = window.innerHeight;
  var documentHeight = document.documentElement.scrollHeight;

  var isBottom = scrollTop + windowHeight >= documentHeight-100;

  if (scrollTop >= 100 && !isBottom) {
    sticky.classList.add('copyright-fixed');
  } else {
    sticky.classList.remove('copyright-fixed');
  }
});

// Scroller
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
    if (jQuery(this).scrollTop() > 100) {
      jQuery('.scrollup i').fadeIn();
    } else {
      jQuery('.scrollup i').fadeOut();
    }
	});
	jQuery('.scrollup i').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
	});
});

// search
jQuery(document).ready(function () {
  function chartered_accountant_search_loop_focus(element) {
    var chartered_accountant_focus = element.find('select, input, textarea, button, a[href]');
    var chartered_accountant_firstFocus = chartered_accountant_focus[0];  
    var chartered_accountant_lastFocus = chartered_accountant_focus[chartered_accountant_focus.length - 1];
    var KEYCODE_TAB = 9;

    element.on('keydown', function chartered_accountant_search_loop_focus(e) {
      var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);

      if (!isTabPressed) { 
        return; 
      }

      if ( e.shiftKey ) /* shift + tab */ {
        if (document.activeElement === chartered_accountant_firstFocus) {
          chartered_accountant_lastFocus.focus();
            e.preventDefault();
          }
        } else /* tab */ {
        if (document.activeElement === chartered_accountant_lastFocus) {
          chartered_accountant_firstFocus.focus();
            e.preventDefault();
          }
        }
    });
  }
  jQuery('.search-box span a').click(function(){
      jQuery(".serach_outer").slideDown(1000);
      chartered_accountant_search_loop_focus(jQuery('.serach_outer'));
  });

  jQuery('.closepop a').click(function(){
      jQuery(".serach_outer").slideUp(1000);
  });
});

// Slider
jQuery(document).ready(function(){
  var owl = jQuery('#our-case-section .owl-carousel');
  owl.owlCarousel({
    margin: 20,
    loop: true,
    dots: false,
    autoplay: true,
    nav: true,
    navText: ['<i class="fa-solid fa-angle-left"></i>', '<i class="fa-solid fa-angle-right"></i>'],
    responsive: {
      0: { 
        items: 1 
      },
      768: { 
        items: 2 
      },
      992: { 
        items: 2 
      },
      1200: { 
        items: 2 
      }
    },
    mouseDrag: true
  });

  // Function to add class to first active item
  function chartered_accountant_setFirstActiveClass() {
    jQuery('.owl-item').removeClass('first-active');
    jQuery('.owl-item.active').first().addClass('first-active');
  }

  // Run on initialization
  owl.on('initialized.owl.carousel', function(){
    setTimeout(chartered_accountant_setFirstActiveClass, 100);
  });

  // Run on slide change
  owl.on('changed.owl.carousel', function(){
    setTimeout(chartered_accountant_setFirstActiveClass, 100);
  });

  // Set class immediately after page load
  setTimeout(chartered_accountant_setFirstActiveClass, 100);
});

/*sticky sidebar*/
window.addEventListener('scroll', function() {
  var sticky = document.querySelector('.sidebar-sticky');
  if (!sticky) return;

  var scrollTop = window.scrollY || document.documentElement.scrollTop;
  var windowHeight = window.innerHeight;
  var documentHeight = document.documentElement.scrollHeight;

  var isBottom = scrollTop + windowHeight >= documentHeight-100;

  if (scrollTop >= 100 && !isBottom) {
    sticky.classList.add('sidebar-fixed');
  } else {
    sticky.classList.remove('sidebar-fixed');
  }
});
/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const chartered_accountant_progressBar =
        document.getElementById("chartered_accountant_elemento_progress_bar");
    if (!chartered_accountant_progressBar) return;
    window.addEventListener("scroll", function () {
        const chartered_accountant_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const chartered_accountant_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const chartered_accountant_scrolled =
            (chartered_accountant_scrollTop / chartered_accountant_height) * 100;
        chartered_accountant_progressBar.style.width =
            chartered_accountant_scrolled + "%";
    });
});