Untitled

 avatar
unknown
plain_text
a month ago
5.3 kB
7
Indexable
(function () {
    "use strict";

    /**
     * Pauses all currently playing videos on the page.
     */
    function pauseAllPlayingVideo() {
        $('body video').each(function () {
            if (!$(this).get(0).paused && $(this).get(0).controls) {
                $(this).get(0).pause();
            }
        });
    }

    /**
     * Pauses all videos except the currently playing one.
     * @param {string} currentVideoId - ID of the currently playing video.
     */
    function pauseAllOtherPlayingVideo(currentVideoId) {
        $('body video').each(function () {
            if (!$(this).get(0).paused && $(this).get(0).controls && this.id !== currentVideoId) {
                $(this).get(0).pause();
            }
        });
    }

    /**
     * Initializes Slick carousels and attaches appropriate event listeners.
     */
    function initializeCarousels() {
        $('div[class^="featuredcontent-"]').each(function () {
            const $carousel = $(this);
            let fccId = '';
            let arrayList = this.classList;

            // Extract unique ID for the carousel
            Array.prototype.forEach.call(arrayList, function (name) {
                if (name.match(/\d+$/)) {
                    const fccIdClass = name.match(/\d+$/);
                    fccId = fccIdClass.input.split("responsiveImage_").length > 0 ?
                        fccIdClass.input.split("responsiveImage_")[1] : '';
                }
            });

            // Initialize Slick Carousel
            $carousel.slick({
                infinite: false,
                slidesToShow: $carousel.attr("layout"),
                slidesToScroll: 1,
                arrows: true,
                fade: true,
                cssEase: 'linear',
                prevArrow: $('.featuredcontent-responsiveImage_' + fccId + '__arrow-prev'),
                nextArrow: $('.featuredcontent-responsiveImage_' + fccId + '__arrow-next'),
                responsive: [
                    {
                        breakpoint: 767,
                        settings: {
                            slidesToShow: $carousel.attr("layout"),
                            slidesToScroll: 1,
                        }
                    }
                ]
            }).on('setPosition', function (event, slick) {
                // Adjust slide height based on viewport width
                const w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
                if (w > 850) {
                    slick.$slides.height('558px');
                } else if (w > 769 && w <= 850) {
                    slick.$slides.height('600px');
                } else if (w > 600 && w <= 769) {
                    slick.$slides.height('475px');
                } else if (w > 500 && w <= 600) {
                    slick.$slides.height('415px');
                } else {
                    slick.$slides.height('383px');
                }
            }).on('afterChange', function (event, slick, currentSlide) {
                // Handle arrow states for last slide
                const totalSlides = slick.slideCount;
                const $nextArrow = $carousel.siblings(".featuredcontent__arrow-next");
                if (currentSlide === totalSlides - 1) {
                    $nextArrow.addClass('slick-disabled');
                    $nextArrow.prev().addClass('last');
                } else {
                    $nextArrow.removeClass('slick-disabled');
                    $nextArrow.prev().removeClass('last');
                }
            });
        });
    }

    /**
     * Initializes video controls and event listeners.
     */
    function initializeVideoControls() {
        $('div[class^="fc__video-"]').each(function () {
            const videoWrapper = $(this)[0];
            const video = $(this).find('video')[0];

            if (videoWrapper && video && videoWrapper.contains(video)) {
                $('.featuredcontent__video').addClass('has-media-controls-hidden');
                $('.featuredcontent__video-overlay-play-button').on('click', function () {
                    const currentVideo = $(this).prev().find('.featuredcontent__video:visible');
                    $(this).prev().find('.darken-overlay').hide();
                    $(this).addClass('is-hidden');
                    pauseAllPlayingVideo();
                    currentVideo.trigger('play');
                    currentVideo.get(0).addEventListener('play', () => {
                        pauseAllOtherPlayingVideo(currentVideo.get(0).id);
                    });
                    currentVideo.removeClass('has-media-controls-hidden');
                    currentVideo.attr('controls', 'controls');
                });
            }
        });
    }

    /**
     * Main function to run on document ready.
     */
    function onDocumentReady() {
        initializeCarousels();
        initializeVideoControls();
    }

    // Run the script once the document is ready
    if (document.readyState !== "loading") {
        onDocumentReady();
    } else {
        document.addEventListener("DOMContentLoaded", onDocumentReady);
    }
})();
Leave a Comment