function getCalcValueInPixels(calcValue) {
    var $tempElement = $('<div>');
    $tempElement.css({
        position: 'absolute',
        visibility: 'hidden',
        width: calcValue
    });
    $('body').append($tempElement);
    var pixelValue = $tempElement.width();
    $tempElement.remove();
    return pixelValue;
}


function createScrollTriggers() {


  var sectionMarginValue = getComputedStyle(document.documentElement).getPropertyValue('--sectionmargin').trim();
  var sectionMarginInPixels = getCalcValueInPixels(sectionMarginValue);
  var headerHeight = $(".cheader").height(); // Ensure headerHeight is defined

  var startValue = `top top+=${headerHeight + sectionMarginInPixels}`;

  var jqStickySide = $(".jqstickyside");
  var stopSticky = $(".e-stopsticky");

  // Calculate the end position
  var stickyHeight = jqStickySide.outerHeight(true)+250;
  var endValue = `bottom top+=${stickyHeight}`;


  // Re-initialize the GSAP animation
  gsap.to(jqStickySide, {
      scrollTrigger: {
          trigger: jqStickySide,
          start: startValue,
          endTrigger: stopSticky,
          end: endValue,
          pin: true,
          pinSpacing: false,
          scrub: true,
          // markers:true
      }
  });


  $('.csectioninner,.cslickimg').each(function(){
    var thethis = $(this);
    var stext = $(".csectiontext", this);

    var startscroll = 230;

    if ($(this).hasClass("csectioninnerbanner")){
      startscroll = 130;
      if ($(window).width()<1650){
        startscroll = 100;

      }
      if ($(window).width()<1250){
        startscroll = 40;

      }
    }

    console.log("tessst");

    var tl1 = gsap.timeline({
      scrollTrigger: {
        invalidateOnRefresh: true,
        trigger: thethis,
        start: "top-=" + startscroll + " top",
        end: "+=30%",
        // markers: true,
        scrub: true,
        pin: false,
        pinSpacing: false,
      }
    })
    .to(thethis, {"margin-left": -1, "margin-right": -1}, 0);
  });



  $('h1,h2,h3,.sh1,.sh2,.sh3').each(function() {
    var thethis = $(this);
    var split = new SplitText(thethis, { type: "lines" });
    var split2 = new SplitText(thethis, { type: "lines" });

    var delay=0.5;
    if ($(this).closest("section").hasClass("csectionbannerdefault")){
      delay=1.2;
    }
    var animation = gsap.timeline({
      scrollTrigger: {
        trigger: thethis, // Use the current ".csection" element as the trigger
        // markers: true,
        start: "top 100%", // Adjust the start position as needed
        end: "bottom 20%", // Adjust the end position as needed
        toggleActions: "play none none none", // Define toggle actions
      }
    });

    // Animation for each word
    animation.from(split.lines, { stagger: 0.05, yPercent: 150, duration: 0.7,delay:delay });
  });

}
$(document).ready(function() {
   gsap.registerPlugin(ScrollTrigger);



  // Initial creation of scroll triggers
  createScrollTriggers();

  // Add event listener to refresh scroll triggers on window resize

});

$(window).on('resize', function() {
  if ($(window).width()>1000){
    ScrollTrigger.refresh();
  }
});
