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

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

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

        // For popout content ------------------------------
        Premium.jpxApi.setCollapsedHeight(18, 'percent');
        Premium.jpxApi.addSheet(".jpx-cf-wrapper{background:transparent !important;}");

        var items = [
            // .jpt-pseudo-body, .jpt-main-content ---------
            {
                state: "both",
                selector: ".jpt-pseudo-body, .jpt-wrapper",
                narrow: {
                    cssShort: { height: 1 }
                }
            },

            // .jpt-video-spacer --------------------------------------------
            {
                state: "collapsed",
                selector: ".jpt-popout .jpt-video-spacer",
                narrow: {
                    cssShort: { bottom: 0, height: 1.2 },
                    cssMedium: { bottom: 0, height: 1.1 },
                    cssTall: { bottom: 0.05, height: .9 }
                },
                medium: {
                    cssShort: { bottom: 0, height: 1.5 },
                    cssMedium: { bottom: 0, height: 1.2 }
                },
                wide: {
                    cssShort: { bottom: 0, height: 1.5 }
                }
            },
            {
                state: "expanded",
                selector: ".jpt-popout .jpt-video-spacer",
                narrow: {
                    cssShort: { bottom: 0, height: 1 },
                    cssMedium: { bottom: .1, height: .8 },
                    cssTall: { bottom: .15, height: .7 }
                },
                medium: {
                    cssShort: { bottom: 0, height: 1 },
                    cssTall: { bottom: .1, height: .8 }
                },
                wide: {
                    cssShort: { bottom: 0, height: 1 }
                }
            },
            // .jpt-video-container -----------------------------------------
            {
                state: "collapsed",
                selector: ".jpt-popout .jpt-video-container",
                narrow: {
                    cssShort: { bottom: 0 },
                    cssTall: { bottom: 0.05 }
                },
                medium: {
                    cssShort: { bottom: 0 }
                }
            },
            {
                state: "expanded",
                selector: ".jpt-popout .jpt-video-container",
                narrow: {
                    cssShort: { bottom: 0 },
                    cssMedium: { bottom: .1 },
                    cssTall: { bottom: .15 }
                },
                medium: {
                    cssShort: { bottom: 0 },
                    cssTall: { bottom: .1 }
                },
                wide: {
                    cssShort: { bottom: 0 }
                }
            }
        ];

        var responsiveHelper = new ResponsiveHelper(items);

        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 sectionCountdownEls = document.querySelectorAll(".jpt-section-countdown");
        Premium.template.setCountdownLanguage("en");
        var endDate = new Date("December 20 2022 09:00:00");
        for (let i = 0; i < sectionCountdownEls.length; i++) {
            var targetEl = sectionCountdownEls[i];
            var jpCountdown = new JPCountdown(targetEl.querySelector(".jpt-countdown"), endDate);
            (function(jpCountdown, targetEl) {
                jpCountdown.on("update", function(timeInfo, totalSecondsLeft) {
                    timeInfo = Premium.template.convertCountdownTime(totalSecondsLeft);
                    if (timeInfo.wholeDays > 0) {
                        targetEl.querySelector(".jpt-countdown").classList.add("narrow");
                    } else {
                        targetEl.querySelector(".jpt-countdown").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() {

        }

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

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

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