document.addEventListener("DOMContentLoaded", function () {
    var iframes = document.getElementsByTagName("iframe");
    // Get the swiper-wrapper element and all its child elements
    const swiperWrapper = document.querySelector('.swiper-wrapper');
    const swiperSlides = Array.from(swiperWrapper.children);
    const native_video_player = false;
    var stop_transition = false;
    let timeoutId;
    for (var i = 0; i < iframes.length; i++) {
        var iframe = iframes[i];
        iframe.src += "?" + window.location.search.substr(1);
    }
    // Set initial index to 0 for first slide
    let currentIndex = swiperSlides.length - 1;
    // Function to show the next slide
    function showNextSlide() {
        // Check if there is a video element in the current slide
        const videoElement = swiperSlides[currentIndex].querySelector('video');
        if (videoElement) {
            // Pause the video if it is currently playing
            if (!videoElement.paused) {
                videoElement.pause();
                videoElement.load();
            }
        }
        // Hide the current slide
        swiperSlides[currentIndex].classList.remove('active');
        // Increment the index for the next slide
        currentIndex = (currentIndex + 1) % swiperSlides.length;
        // Show the next slide
        swiperSlides[currentIndex].classList.add('active');
        // Get the timeout value from the sc-timeout attribute
        const timeout = swiperSlides[currentIndex].getAttribute('sc-timeout') || 0;
        // Check if the current slide has a video element
        const nextVideoElement = swiperSlides[currentIndex].querySelector('video');
        if (nextVideoElement) {
            if (native_video_player && typeof Android != 'undefined' && typeof Android.playVideoPlaylistNoRefresh != 'undefined') {
                if (!stop_transition) {
                    stop_transition = true;
                    Android.playVideoPlaylistNoRefresh("web/" + nextVideoElement.querySelector('source').getAttribute('src'));
                    //showNextSlide();
                }
            }
            else {
                // Play the video
                nextVideoElement.play();
                // Set a timeout to show the next slide after video duration
                timeoutId = setTimeout(showNextSlide, nextVideoElement.duration * 1000);
            }
        } else {
            // Set a timeout to show the next slide after timeout duration
            timeoutId = setTimeout(showNextSlide, timeout * 1000);
        }
    }
    // Show the first slide initially
    swiperSlides[currentIndex].classList.add('active');
    // Start showing the slides
    showNextSlide();
    window.addEventListener('focus', function () {
        if (native_video_player && typeof Android != 'undefined' && typeof Android.playVideoPlaylistNoRefresh != 'undefined') {
            stop_transition = false;
            showNextSlide();
        }
    });
    window.addEventListener('blur', function () {
        if (native_video_player && typeof Android != 'undefined' && typeof Android.playVideoPlaylistNoRefresh != 'undefined') {
            //clearTimeout(timeoutId);
        }
    });
});