/*
 gumgum-ljp_creative_template v1.6.4 2024-04-29 
 */

"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        Premium.animation.delay = 1;
        Premium.cta.delay = 2;

        var buttonSmallPrintEl = document.querySelector(".jpt-button-legals");
        var smallPrintEl = document.querySelector(".jpt-legals");
        var hasPlayedAnimation;
        var isSmallPrintOpen = false;


        if (buttonSmallPrintEl) {
            buttonSmallPrintEl.addEventListener("click", function() {
                toggleOpenSmallPrint();
            })

            function toggleOpenSmallPrint() {
                isSmallPrintOpen = !isSmallPrintOpen;
                if (isSmallPrintOpen) {
                    gsap.set(buttonSmallPrintEl.querySelector("IMG"), { rotation: 180 });
                    smallPrintEl.classList.add("hidden");
                } else {
                    gsap.set(buttonSmallPrintEl.querySelector("IMG"), { rotation: 0 });
                    smallPrintEl.classList.remove("hidden");
                }
                resizeHandler();
            }
        }

        var sectionVideoEls = document.querySelectorAll(".jpt-video-container");
        var vidPlacementsToLoad = sectionVideoEls.length || 1;

        Premium.creative.createVideoPlayerAppended = function(video, container) {
            vidPlacementsToLoad--;
            if (vidPlacementsToLoad !== 0) return;
            var videoMasterEl = document.querySelector(".jpt-expanded [jp-controls] video") || document.querySelector("[jp-controls] video");
            var videoOtherEl = document.querySelector(".jpt-collapsed [jp-controls] video");
            if (videoMasterEl === videoOtherEl) videoOtherEl = undefined;
            if (videoMasterEl) {
                if (videoOtherEl) {
                    videoOtherEl.volume = 0;
                    videoMasterEl.parentElement.addEventListener("click", function() {
                        PremiumJpControls.getLastAction(videoOtherEl.parentElement).playPause = PremiumJpControls.getLastAction(videoMasterEl.parentElement).playPause;
                        PremiumJpControls.getLastAction(videoOtherEl.parentElement).muteUnmute = PremiumJpControls.getLastAction(videoMasterEl.parentElement).muteUnmute;
                    })
                    videoMasterEl.addEventListener("play", function() {
                        videoOtherEl.currentTime = videoMasterEl.currentTime;
                        videoOtherEl.play();
                    });
                    videoMasterEl.addEventListener("pause", function() {
                        videoOtherEl.currentTime = videoMasterEl.currentTime;
                        videoOtherEl.pause();

                    })
                    videoMasterEl.addEventListener("volumechange", function() {
                        videoOtherEl.muted = videoMasterEl.muted;
                    })
                }
                var prom = videoMasterEl.play();
                if (prom) {
                    prom.catch(function(e) {})
                }
            }
        }

        var carouselMasterEl = document.querySelector(".jpt-expanded [jp-carousel]") || document.querySelector("[jp-carousel]");
        var carouselOtherEl = document.querySelector(".jpt-collapsed [jp-carousel]");
        if (carouselMasterEl === carouselOtherEl) carouselOtherEl = undefined;
        var carouselLabels = ["Label 1", "Label 2", "Label Three", "Label Four"];
        var updateLabel = function(index, carouselObj) {
            var labelEl = carouselObj._targetEl.parentElement.querySelector(".jpt-p");
            gsap.timeline()
                .to(labelEl, .2, { opacity: 0 })
                .add(function() {
                    labelEl.innerText = carouselLabels[index];
                })
                .to(labelEl, .4, { opacity: 1 })
        }

        if (carouselMasterEl) {
            var carouselOtherObj;
            var carouselStopRef;
            carouselMasterEl.addEventListener("click", function(){
                clearTimeout(carouselStopRef);
            })
            if (carouselOtherEl) {
                JPCarousel.callOnReady(carouselOtherEl, function() {
                    carouselOtherObj = JPCarousel.getObject(carouselOtherEl);
                    carouselOtherEl.style.width = "99%";
                    setTimeout(function(){
                        carouselOtherEl.style.width = "100%";
                        carouselOtherObj.resizeHandler();
                    }, 100)
                    Premium.template.fixjpUrls(carouselOtherObj);
                    carouselOtherEl.appendChild(carouselOtherEl.parentElement.querySelector(".jpt-p"));
                    carouselOtherObj.setCallOnShowItem(function(index) {
                        updateLabel(index, carouselOtherObj);
                    });
                });
            }
            JPCarousel.callOnReady(carouselMasterEl, function() {
                var carouselMasterObj = JPCarousel.getObject(carouselMasterEl);
                carouselMasterEl.style.width = "99%";
                setTimeout(function(){
                    carouselMasterEl.style.width = "100%";
                    carouselMasterObj.resizeHandler();
                }, 100)
                Premium.template.fixjpUrls(carouselMasterObj);
                carouselMasterEl.appendChild(carouselMasterEl.parentElement.querySelector(".jpt-p"));
                carouselMasterObj.setCallOnShowItem(function(index) {
                    updateLabel(index, carouselMasterObj);
                    if (carouselOtherObj) carouselOtherObj.showItem(index);
                })
                carouselStopRef = setTimeout(function(){
                    carouselMasterObj.autoShowTimer(undefined);
                }, Premium.template.CarouselStopTime)
                carouselMasterObj.autoShowTimer(4000);
            });
        }

        var countdownEls = document.querySelectorAll(".jpt-countdown");
        var endDate = new Date("April 30 2022 09:00:00");
        for (let i = 0; i < countdownEls.length; i++){
            var targetEl = countdownEls[i];
            var jpCountdown = new JPCountdown(targetEl, endDate);
            (function(jpCountdown, targetEl){
                jpCountdown.on("update", function(timeInfo, totalSecondsLeft) {
                    timeInfo = Premium.template.convertCountdownTime(totalSecondsLeft);
                    if (timeInfo.wholeDays > 0) {
                        targetEl.classList.add("narrow");
                    } else {
                        targetEl.classList.remove("narrow");
                    }
                    targetEl.querySelector(".jpt-countdown-days-container").style.display = timeInfo.wholeDays > 0 ? "flex" : "none";
                    targetEl.querySelector(".jp-countdown-days").innerHTML = timeInfo.wholeDays;
                    targetEl.querySelector(".jp-countdown-hours").innerHTML = timeInfo.wholeHours;
                })
                jpCountdown.start();
            })(jpCountdown, targetEl)
        }

        function resizeHandler() {
            if (buttonSmallPrintEl) {
                if (isSmallPrintOpen) {
                    requestAnimationFrame(function() {
                        gsap.to(smallPrintEl, .3, { height: smallPrintEl.scrollHeight });
                    })
                } else {
                    gsap.to(smallPrintEl, .3, { height: "35%" });
                }
            }
        }

        function scrollHandler() {
            if (!hasPlayedAnimation && Premium.jpxApi.getPercentageVisibility() > 80) {
                hasPlayedAnimation = true;
                entranceAnimation.play();
                if (carousel) carousel.autoShowTimer(3000);
                if (Premium.template.initButtonEffects) {
                    Premium.template.initButtonEffects();
                }
            }
        }

        var animationObjFrom = { x: 30, opacity: 0 };
        var animationObjTo = { x: 0, opacity: 1, stagger: .1, delay: .5, ease: "back.out", overwrite:"all" }

        Premium.jpxApi.on("expand", function() {
            gsap.to(".jpt-collapsed", .5, { autoAlpha: 0 });
            gsap.to(".jpt-expanded", .5, { autoAlpha: 1 });

            gsap.timeline()
                .fromTo(".expand-animation", .5, animationObjFrom, animationObjTo)

        })

        Premium.jpxApi.on("default", function() {
            gsap.to(".jpt-collapsed", .5, { autoAlpha: 1 });
            gsap.to(".jpt-expanded", .5, { autoAlpha: 0 });

            gsap.timeline()
                .fromTo(".jp-entrance-anim", .5, animationObjFrom, animationObjTo)
        })

        window.addEventListener("resize", resizeHandler)
        Premium.product.scopeWindow.addEventListener("scroll", scrollHandler);

        /* END OF CUSTOM JS */
    },
    loaded:function(){
        document.body.style.opacity = 1;
    }
}