(function () {
function init() {
var btn = document.querySelector('a.js-sound-toggle');
if (!btn) return;
var section = btn.closest('.et_pb_section');
if (!section) return;
var video = section.querySelector('video');
if (!video) return;
// 1) Endlosschleife aus — das Video spielt nur einmal durch
video.loop = false;
video.removeAttribute('loop');
// 2) Beim Laden pausieren — startet nicht sofort
video.pause();
// 3) Erst starten, wenn die Section sichtbar im Viewport ist,
// und auch nur dieses eine Mal
var hasPlayed = false;
if ('IntersectionObserver' in window) {
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting && !hasPlayed) {
var p = video.play();
if (p && p.catch) p.catch(function () {});
hasPlayed = true;
observer.unobserve(video);
}
});
}, { threshold: 0.3 });
observer.observe(video);
} else {
// Fallback für sehr alte Browser
var p = video.play();
if (p && p.catch) p.catch(function () {});
}
// Sound-Toggle (wie gehabt) — plus: falls das Video schon
// durchgelaufen ist, beim Klick neu starten
btn.addEventListener('click', function (e) {
e.preventDefault();
video.muted = !video.muted;
if (video.ended || video.paused) {
if (video.ended) video.currentTime = 0;
var p = video.play();
if (p && p.catch) p.catch(function () {});
}
btn.textContent = video.muted ? 'SOUND ON' : 'SOUND OFF';
});
}
if (document.readyState !== 'loading') init();
else document.addEventListener('DOMContentLoaded', init);
})();