(function () { function initCarousel(root) { var track = root.querySelector(".aibui-carousel-track"); if (!track) return; var slides = root.querySelectorAll(".aibui-carousel-slide"); var dots = root.querySelectorAll(".aibui-carousel-dot"); var prev = root.querySelector(".aibui-carousel-prev"); var next = root.querySelector(".aibui-carousel-next"); var idx = 0; var autoplay = root.getAttribute("data-autoplay") === "1"; var delay = parseInt(root.getAttribute("data-delay") || "4", 10) * 1000; function getViewportWidth() { var holder = root.querySelector(".aibui-carousel-preview") || root; return holder.clientWidth || holder.offsetWidth || 0; } function update() { var w = getViewportWidth(); track.style.transform = "translateX(" + -idx * w + "px)"; dots.forEach(function (d, i) { d.classList.toggle("is-active", i === idx); }); } function go(n) { idx = (n + slides.length) % slides.length; update(); } if (prev) prev.addEventListener("click", function () { go(idx - 1); }); if (next) next.addEventListener("click", function () { go(idx + 1); }); var timer; function start() { if (autoplay && slides.length > 1) { stop(); timer = setInterval(function () { go(idx + 1); }, delay); } } function stop() { if (timer) { clearInterval(timer); timer = null; } } root.addEventListener("mouseenter", stop); root.addEventListener("mouseleave", start); dots.forEach(function (d, i) { d.addEventListener("click", function () { go(i); }); }); update(); start(); // keep alignment on resize window.addEventListener("resize", update); } function boot() { var roots = document.querySelectorAll( ".wp-block-ai-builder-aibui-carousel" ); for (var i = 0; i < roots.length; i++) { initCarousel(roots[i]); } } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", boot); } else { boot(); } })();