// // Header Dropdown Script Start
$(document).ready(function() {
    $("header a:is([data-toggle='dropdown'])").attr("href", "javascript:;");
    var windowWidth = $(window).width();
    if (windowWidth <= 991) {
        var subMenu = $("header .navbar-nav li").find(".dropdown-menu");
        subMenu.hide();
        $("header .navbar-nav li").click(function() {
            $(this).find(".dropdown-menu").slideToggle();
            $(this).siblings("li").find(".dropdown-menu").slideUp();

        });
    }
});
// // Header Dropdown Script End

// Progress bar Line fucntion When the user scrolls the page, execute myFunction 
window.onscroll = function () { scrollFunction() };

function scrollFunction() {
    var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
    var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    var scrolled = (winScroll / height) * 100;  // Change 50 to 100
    document.getElementById("myBar").style.width = scrolled + "%";
}
//  Progress bar Line fucntion End

// new swap section
if ($(window).width() > 991) {
   
    if ($(".philosophie-desktop").length) {
        // console.log("Gsap works");
        const colors = [
        "#E6E7F9",
        "#BEEBFF",
        "#FDFFD0",
        "#E6E7F9",
        "#BEEBFF",
        "#FDFFD0",
        "#DFE0FD",
    
        // "#E6E7F9",
        // "#E0EDF3",
        // "#F7F9D0",
        // "#DFE0FD",
        // "#E0EDF3",
        // "#F7F9D0",
        // "#E6E7F9",
        ];
    
        // console.log(colors);
        
    
        let details = gsap.utils.toArray(".point");
    
        gsap.registerPlugin(ScrollTrigger);
    
        gsap.set(".photo:not(:first-child)", {
          // opacity: 1,
          scale: 1,
          // y: '100%',
          clipPath: "inset(100% 0% 0%)",
          scale: 1,
        });
    
        gsap.set(".photo img", {
          y: 5,
        });
    
        const animation = gsap.to(".photo:not(:first-child)", {
          // opacity: 1,
          scale: 1,
          duration: 1,
          stagger: 1,
          // y: '0%',
          clipPath: "inset(0% 0% 0%)",
          scale: 1,
          snap: true,
        });
    
        gsap.to(".photo img", {
          y: -5,
          duration: 1,
          ease: "power1.inOut",
        });
    
        const startValue = window.innerWidth <= 1600 ? "80px" : "top top";
    
        ScrollTrigger.create({
          trigger: ".philosophie-desktop",
        //   start: "top top",
          start: startValue,
          end: "bottom bottom",
          pin: ".right",
          animation: animation,
          scrub: 2,
          // markers: true,
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[0],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-1",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[1],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-2",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[2],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-3",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[3],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-4",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[4],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-5",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[5],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-6",
            scrub: true,
          },
        });
    
        gsap.to(".philosophie-desktop", {
          duration: 1,
          backgroundColor: colors[6],
          transition: "1s ease-in-out",
          scrollTrigger: {
            trigger: "#philosophie-desktop-animation .slide-7",
            scrub: true,
          },
        });
    
        $(window).on("load", function () {
          ScrollTrigger.refresh();
        });
    
        $(window).on("resize", function () {
          ScrollTrigger.refresh(); 
        });
    }

    // For Website CTA Button moving animation script start
    const animated_cta_btn = document.querySelectorAll('.animated-cta-btn');
    animated_cta_btn.forEach(button => {
        button.addEventListener('mousemove', (e) => {
            const rect = button.getBoundingClientRect();
            const x = ((e.clientX - rect.left) / button.offsetWidth - 0.5) * 35;
            const y = ((e.clientY - rect.top) / button.offsetHeight - 0.5) * 35;

            gsap.to(button, {
                x: x,
                y: y,
                duration: 0.03,
                ease: 'power2.out'
            });
        });

        button.addEventListener('mouseleave', () => {
            gsap.to(button, {
                x: 0,
                y: 0,
                duration: 0.2,
                ease: 'power2.out'
            });
        });
    });
    // For Website CTA Button moving animation script End

    // Animated All Website Buttons Animation Script Start
    const animated_btns = document.querySelectorAll('.animated-btn');
    animated_btns.forEach(button => {
        button.addEventListener('mousemove', (e) => {
            const rect = button.getBoundingClientRect();
            const x = ((e.clientX - rect.left) / button.offsetWidth - 0.5) * 65;
            const y = ((e.clientY - rect.top) / button.offsetHeight - 0.5) * 35;

            gsap.to(button, {
                x: x,
                y: y,
                duration: .08,
                ease: 'power2.out'
            });
        });

        button.addEventListener('mouseleave', () => {
            gsap.to(button, {
                x: 0,
                y: 0,
                duration: 0.1,
                ease: 'power2.out'
            });
        });
    });
    // Animated All Website Buttons Animation Script End
}

// Looping over all the indicators...
// indicators.forEach( function(indicator, i) {
//   // ...forEach of them adding a click-event listener...
//   indicator.addEventListener('click', function() {
//     // ...leveraging the ScrollToPlugin and ...
//     // ...ScrollTrigger's  labelToScroll() method
//     gsap.to(window, {
//       duration: 1,
//       scrollTo: tl.scrollTrigger.labelToScroll(`position-${i}`)
//     });

//   })

// })
// new swap section


// count number js
function startCounter(e) {
    if (e.hasClass("notVisible")) {
        e.removeClass("notVisible");
        e.prop("Counter", 0).animate(
            {
                Counter: e.attr("data-counter-lim")
            },
            {
                duration: 4000, // 4e3 = 4000ms
                easing: "swing",
                step: function (o) {
                    e.text(Math.ceil(o).toLocaleString());
                },
                complete: function () {
                    // Wrap each character in a <small> tag
                    const finalValue = e.text();
                    const wrappedValue = finalValue
                        .split("") // Split into individual characters
                        .map(char => `<small>${char}</small>`) // Wrap each character
                        .join(""); // Join back into a single string
                    e.html(wrappedValue);
                }
            }
        );
    }
}

$(document).ready(function () {
    $(window).scroll(function () {
        $(".count").each(function () {
            if ($(this).isOnScreen()) {
                startCounter($(this));
            } else if (!$(this).hasClass("notVisible")) {
                $(this).stop();
                $(this).addClass("notVisible");
                $(this).text("0");
            }
        });
    });
});

$.fn.isOnScreen = function () {
    const e = $(window),
        o = {
            top: e.scrollTop(),
            left: e.scrollLeft()
        };
    o.right = o.left + e.width();
    o.bottom = o.top + e.height();

    const t = this.offset();
    t.right = t.left + this.outerWidth();
    t.bottom = t.top + this.outerHeight();

    return !(o.right < t.left || o.left > t.right || o.bottom < t.top || o.top > t.bottom);
};

$(".count").each(function () {
    $(this).addClass("notVisible");
    if ($(this).isOnScreen()) {
        startCounter($(this));
    }
});
// count number js end



$(document).ready(function () {
  // remove role attr form owl button
  $(".owl-carousel").each((function(){$(this).find(".owl-prev").each((function(a){$(this).removeAttr("role");}))}));
  $(".owl-carousel").each((function(){$(this).find(".owl-next").each((function(a){$(this).removeAttr("role");}))}));

  $(".owl-carousel").each((function(){$(this).find(".owl-prev").each((function(a){$(this).attr("aria-label",a+1)}))}));
  $(".owl-carousel").each((function(){$(this).find(".owl-next").each((function(a){$(this).attr("aria-label",a+1)}))}));


  // remove aria-hidden attr form dial code
  setTimeout(function(){
    var itiFlag = $(".iti__selected-flag").removeAttr("aria-hidden");
    // alert(itiFlag[]);
  },2000);

});

// --------------- Header Sticky Script Start ---------------
document.addEventListener("DOMContentLoaded", function () {
  const header = document.querySelector("header");

  if (header) {
    function toggleStickyHeader() {
      const scrollTop =
        window.pageYOffset || document.documentElement.scrollTop;
      const pageHeight =
        document.documentElement.scrollHeight - window.innerHeight;
      const triggerPoint = pageHeight * 0.03;

      if (scrollTop > triggerPoint) {
        header.classList.add("is-sticky");
      } else {
        header.classList.remove("is-sticky");
      }
    }

    window.addEventListener("scroll", toggleStickyHeader, { passive: true });
    window.addEventListener("load", toggleStickyHeader);
    window.addEventListener("resize", toggleStickyHeader);
  }
});
// --------------- Header Sticky Script End ---------------