jQuery(document).ready(function($) {
  	let lastScrollTop = 0;
    const header = $('.fusion-tb-header');
    const stickyClass = 'is-fixed';
    const hiddenClass = 'is-hidden';

    $(window).scroll(function () {
       let scrollTop = $(this).scrollTop();           
      if (scrollTop > lastScrollTop) {
                    // Scrolling down
                    header.addClass(stickyClass).addClass(hiddenClass);
                } else {
                    // Scrolling up
                    header.removeClass(hiddenClass);
                    
                    if (scrollTop === 0) {
                        // At the top
                        header.removeClass(stickyClass);
                    }
                }
                lastScrollTop = scrollTop;
	});
	
 //page section
  $('.main-section-material #material1').addClass('active');
  $('.main-section-material-content #content-material1').addClass('active');
      
  $('.material-box').click(function() {
    // Remove active class from all materials and content
    $('.main-section-material .material-box').removeClass('active');
    $('.main-section-material-content .fusion-layout-column').removeClass('active');
  
    // Get the id of the clicked material
    var id = $(this).attr('id');
  
    // Add active class to the clicked material and corresponding content
    $(this).addClass('active');
    $('#content-' + id).addClass('active');
  });
	
  //Video play pause
  $('.innovize-video-wrap .video-wrapper').click(function() {
    var video = $(this).find('video')[0];
    if (video.paused) {
      video.play();
      $(this).addClass('video-playing');
    } else {
      video.pause();
      $(this).removeClass('video-playing');
    }
  });

  // Team Slider
  $('.team-slider').slick({
    slidesToShow: 4,
    slidesToScroll: 4,
    autoplay: false,
    autoplaySpeed: 3000,
    dots: false,
    arrows: true,
    infinite: true,
    responsive: [
      {
          breakpoint: 1281,
          settings: {
              slidesToShow: 3,
              slidesToScroll: 3
          }
      },
      {
          breakpoint: 769,
          settings: {
              slidesToShow: 2,
              slidesToScroll: 2
          }
      },
      {
          breakpoint: 541,
          settings: {
              slidesToShow: 1,
              slidesToScroll: 1
          }
      }
    ]
  });

  // Wrap the navigation arrows inside a div
  $('.team-slider .slick-next, .team-slider .slick-prev').wrapAll('<div class="slider-arrows"></div>');

  //Upcoming Event Slider
  $('.upcoming-event-slider').slick({
    slidesToShow: 3,
    slidesToScroll: 1,
    dots: false,
    arrows: true,
    infinite: false,
    responsive: [
      {
          breakpoint: 1025, // define breakpoint for tablets
          settings: {
              slidesToShow: 2,
              slidesToScroll: 1
          }
      },
      {
          breakpoint: 541, // define breakpoint for mobile devices
          settings: {
              slidesToShow: 1,
              slidesToScroll: 1
          }
      }
  ]
  });

  // Wrap the navigation arrows inside a div
  $('.custom-swiper-slider.fusion-image-carousel .awb-swiper-button-prev, .custom-swiper-slider.fusion-image-carousel .awb-swiper-button-next').wrapAll('<div class="slider-arrows"></div>');
  
  //Company History Slider
  $('.company-history-wrap .slider-for').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: false,
    fade: true,
    asNavFor: '.slider-year',
    autoplay: false,
    autoplaySpeed: 3000
  });
  
  $('.company-history-wrap .slider-year').slick({
    slidesToShow: 10,
    slidesToScroll: 1,
    asNavFor: '.slider-for',
    dots: false,
    arrows: true,
    focusOnSelect: true,
    infinite: false,
    responsive: [
      {
        breakpoint: 1025,
        settings: {
          slidesToShow: 8,
          slidesToScroll: 1,
          arrows: true
        }
      },
      {
        breakpoint: 768,
        settings: {
          slidesToShow: 6,
          slidesToScroll: 1,
          arrows: true
        }
      },
      {
        breakpoint: 541,
        settings: {
          slidesToShow: 4,
          slidesToScroll: 1,
          arrows: true
        }
      }
    ]
  });  
  // Add class on company history slider
  $('.company-history-wrap .slider-year').on('beforeChange', function(event, slick, currentSlide, nextSlide){
    // Remove the class from all slides
    $(slick.$slides).removeClass('before-current');
    // Add class to all slides before the 'slick-current' div
    $(slick.$slides).slice(0, nextSlide).addClass('before-current');
  });

  // Wrap the navigation arrows inside a div
  $('.company-history-wrap .slick-prev, .company-history-wrap .slick-next').wrapAll('<div class="slider-arrows"></div>');

  //Add class on faq section
  $('.faq-section .fusion-faq-post').click(function() {
    $('.faq-section .fusion-faq-post').not(this).removeClass('active'); // Remove active class from other elements
    $(this).toggleClass('active');
  });

  //case study slider
  $('.case-study-slider').slick({
    slidesToShow: 2,
    slidesToScroll: 2,
    autoplay: true,
    autoplaySpeed: 3000,
    dots: true,
    arrows: true,
    infinite: true,
    responsive: [
      {
          breakpoint: 540,
          settings: {
              slidesToShow: 1,
              slidesToScroll: 1
          }
      }
  ]
  });

  // Wrap the navigation arrows inside a div
  $('.case-study-slider .slick-next, .case-study-slider .slick-prev').wrapAll('<div class="slider-arrows"></div>');
	
	// faq section Set the first item as show by default
  $('.faq-section .accordian .fusion-faq-post').first().addClass('show');
  var firstPostIdClass = $('.faq-section .accordian .fusion-faq-post').first().attr('class').split(' ').filter(function(cls) {
    return cls.startsWith('fusion-faq-post-');
  })[0];
  $('.faq-section .faq-list .' + firstPostIdClass).addClass('show');

  // Toggle show class on click
  $('.faq-section .accordian .fusion-faq-post').on('click', function() {
    var postIdClass = $(this).attr('class').split(' ').filter(function(cls) {
      return cls.startsWith('fusion-faq-post-');
    })[0];

    // Remove show class from all elements
    $('.faq-section .accordian .fusion-faq-post').removeClass('show');
    $('.faq-section .faq-list .faq-item').removeClass('show');
        
    // Add show class to the clicked element and corresponding faq-item
    $(this).addClass('show');
    $('.faq-list .' + postIdClass).addClass('show');
  });

});