(function($) {
  "use strict";

  /**
   * Apply .scrolled class to the body as the page is scrolled down
   */
  function toggleScrolled() {
    const selectBody = document.querySelector('body');
    const selectHeader = document.querySelector('#header');
    if (!selectHeader.classList.contains('scroll-up-sticky') && !selectHeader.classList.contains('sticky-top') && !selectHeader.classList.contains('fixed-top')) return;
    window.scrollY > 100 ? selectBody.classList.add('scrolled') : selectBody.classList.remove('scrolled');
  }

  document.addEventListener('scroll', toggleScrolled);
  window.addEventListener('load', toggleScrolled);

  // var settings ****************************************************
  var $header = $('header');
  var $brand = $('.branding');
  var $logoLG = $('.logo-lg');
  var $logoSM = $('.logo-sm');
  var $overlay = $('<div class="overlay"></div>');
  var $menu = $('.mobileMenu');
  var $ul = $('<ul></ul>');

  // code setting  (mobile menu )*************************************

  // 01. append overlay div (see overlay var) to body
  $("body").append($overlay);

  // 02. lock page scroll when the mobile menu is active
  $overlay.add($menu).on('scroll touchmove mousewheel', function (event) {
      event.preventDefault();
  });

  // 03. on mobile button press, slide menu in and show overlay
  $('#mobileButton').click(function(event){
    event.preventDefault();
    $overlay.show().animate({ opacity: 0.8, }, 200);
    $menu.toggleClass("slideOut");
  });

  // 04. on overlay click, close menu and hide overlay
  $overlay.click(function(){
    $overlay.hide().animate({ opacity: 0, }, 200);
    $menu.toggleClass("slideOut");
  });

  // 05. obtain menu links from .listMenu (using wp_nav_menu output)
  $menu.append($ul);
  $('.listMenu a').each(function () {
    var $anchor = $(this);
    var $li = $('<li></li>');
    $anchor.clone().appendTo($li);
    $ul.append($li);
  });

  // 06. add the class stick to the fixed header on scroll
  $(window).scroll(function() {
    if ($(this).scrollTop() > 100){  
        $($header).addClass("stick");
        $($brand).addClass('stick');
        $($logoLG).hide();
        $($logoSM).show(); 
    } else {
        $($header).removeClass("stick");
        $($brand).removeClass('stick');  
        $($logoSM).hide();
        $($logoLG).show();
    }
  });

  /**
   * Preloader
   */
  const preloader = document.querySelector('#preloader');
  if (preloader) {
    window.addEventListener('load', () => {
      preloader.remove();
    });
  }

  /**
   * Scroll top button
   */
  let scrollTop = document.querySelector('.scroll-top');

  function toggleScrollTop() {
    if (scrollTop) {
      window.scrollY > 100 ? scrollTop.classList.add('active') : scrollTop.classList.remove('active');
    }
  }
  scrollTop.addEventListener('click', (e) => {
    e.preventDefault();
    window.scrollTo({
      top: 0,
      behavior: 'smooth'
    });
  });

  window.addEventListener('load', toggleScrollTop);
  document.addEventListener('scroll', toggleScrollTop);

  /**
   * Initiate glightbox
   */
  const glightbox = GLightbox({
    selector: '.glightbox'
  });

  /**
   * Init isotope layout and filters
   */
  document.querySelectorAll('.isotope-layout').forEach(function(isotopeItem) {
    let layout = isotopeItem.getAttribute('data-layout') ?? 'masonry';
    let filter = isotopeItem.getAttribute('data-default-filter') ?? '*';
    let sort = isotopeItem.getAttribute('data-sort') ?? 'original-order';

    let initIsotope;
    imagesLoaded(isotopeItem.querySelector('.isotope-container'), function() {
      initIsotope = new Isotope(isotopeItem.querySelector('.isotope-container'), {
        itemSelector: '.isotope-item',
        layoutMode: layout,
        filter: filter,
        sortBy: sort
      });
    });

    isotopeItem.querySelectorAll('.isotope-filters li').forEach(function(filters) {
      filters.addEventListener('click', function() {
        isotopeItem.querySelector('.isotope-filters .filter-active').classList.remove('filter-active');
        this.classList.add('filter-active');
        initIsotope.arrange({
          filter: this.getAttribute('data-filter')
        });
        if (typeof aosInit === 'function') {
          aosInit();
        }
      }, false);
    });

  });

  /**
   * Legal Services Tabs
   */
  document.addEventListener('DOMContentLoaded', function() {
    const tabs = document.querySelectorAll('.tabs button');
    const tabContents = document.querySelectorAll('.tab-content');

    tabs.forEach(tab => {
      tab.addEventListener('click', function() {
        tabs.forEach(btn => btn.classList.remove('active'));
        tab.classList.add('active');

        tabContents.forEach(content => content.classList.remove('active'));
        document.getElementById(tab.dataset.tab).classList.add('active');
      });
    });
  });


  // testimonial-slider 




  /**
   * Init swiper sliders
   */
  function initSwiper() {
    document.querySelectorAll('.swiper').forEach(function(swiper) {
      let config = JSON.parse(swiper.querySelector('.swiper-config').innerHTML.trim());
      new Swiper(swiper, config);
    });
  }
  window.addEventListener('load', initSwiper);

  /**
   * Initiate Pure Counter
   */
  new PureCounter();

})(jQuery);
