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

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

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

        Premium.jpxApi.setCollapsedHeight(18, 'percent');

        var sectionVideoEls = document.querySelectorAll(".section-video");
        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 bgVideo = document.querySelector(".jpt-bg video");
        if (bgVideo) {
            Premium.template.bgVideoLoop(bgVideo);
        }

        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)
        }

        var smallPrintEl = document.querySelector(".jpt-legals");
        if (smallPrintEl) {
            var smallPrintContentEl = document.querySelector(".jpt-legals-content");
            var doSmallPrint = function() {
                if (smallPrintContentEl.scrollHeight <= smallPrintEl.clientHeight) {
                    smallPrintEl.classList.remove("jpt-legals-edge");
                } else {
                    smallPrintEl.classList.add("jpt-legals-edge");
                }
            }
            window.addEventListener("resize", doSmallPrint);
            window.addEventListener("load", doSmallPrint, { once: true })
        }

        var resizeHandler = function() {

        }

        var animationObjFrom = { y: 30, opacity: 0 };
        var animationObjTo = { y: 0, opacity: 1, stagger: .1, delay: .2, ease: "back.out" }

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

            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.set(".jpt-bg", { height: "50%" });

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

        window.addEventListener("resize", resizeHandler);
        window.addEventListener("load", function() {
            // setTimeout(function(){
            // 	PremiumJpControls.resizeAll();
            // }, 1000)
            PremiumJpControls.resizeAll();
            document.body.className = document.body.className.replace("preload", "");
            resizeHandler();
        });
        /* END OF CUSTOM JS */
    },
    loaded:function(){
        document.body.style.opacity = 1;
    }
}