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

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

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

        gsap.set(".jpt-kv-image", { xPercent: -50, yPercent: -50 });

        /* Premium.utils.blockScroll --------------------------------------------------- */
        Premium.utils.blockScroll = new function() {
            var yOffset;
            var blockScrollHandler = function(e) {
                window.top.scrollTo(0, yOffset)
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
                return false;
            };
            var block = this.block = function(block) {
                yOffset = window.top.pageYOffset;
                try {
                    if (block) {
                        window.top.addEventListener("scroll", blockScrollHandler, { passive: false });
                        window.addEventListener("scroll", blockScrollHandler, { passive: false });
                        window.top.addEventListener("wheel", blockScrollHandler, { passive: false });
                        window.addEventListener("wheel", blockScrollHandler, { passive: false });
                    } else {
                        window.top.removeEventListener("scroll", blockScrollHandler, { passive: false });
                        window.removeEventListener("scroll", blockScrollHandler, { passive: false });
                        window.top.removeEventListener("wheel", blockScrollHandler, { passive: false });
                        window.removeEventListener("wheel", blockScrollHandler, { passive: false });
                    }
                } catch (e) {}
            }
            window.addEventListener("unload", function() {
                block(false);
            })
        };
        /* Premium.utils.blockScroll END ----------------------------------------------- */

        // Premium.video.SyncAttemptInterval = 0;

        Premium.communicator.init(document.body.id !== "body_expanded" ? 4 : undefined);

        Premium.animation.delay = document.body.id === "body_top" ? 1 : .5;
        var entranceAnimations = Premium.animation.getEntranceAnimation();
        Premium.communicator.api.receiveMessage(function(mess) {
            if (mess.action === "start-entrance-animation") {
                entranceAnimations.play();
            }
        });
        Premium.communicator.api.sendMessage({ action: "start-entrance-animation" });

        // Entrance animations END -------------------------------------------------

        var bgVideo = document.querySelector(".jpt-bg video");
        if (bgVideo) {
            Premium.template.bgVideoLoop(bgVideo);
        }

        var sectionCountdownEl = document.querySelector(".jpt-section-countdown");
        if (sectionCountdownEl) {
            Premium.template.setCountdownLanguage("en");
            var endDate = new Date("June 20 2023 09:00:00");
            var jpCountdown = new JPCountdown(".jpt-countdown", endDate);
            jpCountdown.on("update", function(timeInfo, totalSecondsLeft) {
                timeInfo = Premium.template.convertCountdownTime(totalSecondsLeft);
                if (timeInfo.wholeDays > 0) {
                    sectionCountdownEl.querySelector(".jpt-countdown").classList.add("narrow");
                } else {
                    sectionCountdownEl.querySelector(".jpt-countdown").classList.remove("narrow");
                }
                sectionCountdownEl.querySelector(".jpt-countdown-days-container").style.display = timeInfo.wholeDays > 0 ? "flex" : "none";
                sectionCountdownEl.querySelector(".jp-countdown-days").innerHTML = timeInfo.wholeDays;
                sectionCountdownEl.querySelector(".jp-countdown-hours").innerHTML = timeInfo.wholeHours;
                Premium.communicator.api.sendMessage({ action: "countdown-update", time: timeInfo })
            })
            jpCountdown.start();
            window.addEventListener("resize", Premium.template.checkHideCountdownUnits);
            Premium.template.checkHideCountdownUnits();
        }

        if (document.querySelector("[jp-carousel]")) {
            var carouselLabels = ["Label 1", "Label 2", "Label Three", "Label Four"];
            JPCarousel.callOnReady(document.querySelector("[jp-carousel]"), function() {
                var carousel = JPCarousel.getObject("[jp-carousel]");
                Premium.template.initCarousel(carousel, document.querySelector(".jpt-section-carousel .jpt-expand-button"));
                carousel.on("showitem", function(index) {
                    gsap.timeline()
                        .to(".jp-carousel ~ .jpt-p", .2, { opacity: 0 })
                        .add(function() {
                            document.querySelector(".jp-carousel ~ .jpt-p").innerText = carouselLabels[index];
                        })
                        .to(".jp-carousel ~ .jpt-p", .4, { opacity: 1 })
                })
            })

        }

        const calculateMargin = function() {
            let percSpace = (window.innerHeight - document.querySelector(".jpt-section.jpt-header").offsetHeight - document.querySelector(".jpt-section.jpt-footer").offsetHeight) / window.innerHeight * 100;
            let numSections = document.querySelector(".jpt-wrapper").children.length - 1;
            let percHeight = document.querySelector(".jpt-show-when-scrolled").offsetHeight / window.innerHeight * 100;
            return `-${(percSpace/numSections) + percHeight}vh`

        }

        switch (document.body.id) {
            case "body_top":
                ;

                Premium.creative.createVideoPlayerAppended = function(video) {
                    Premium.video.sync(video, 1);
                    Premium.video.switchOnScroll(video.parentElement, 2);
                    PremiumJpControls.callOnClickFullScreen(function() {
                        video.muted = false;
                        Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                    });
                    var prom = video.play();
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                break;

            case "body_left":
            case "body_right":
                var hasVideoPlace = document.querySelector(".jpt-video-place")
                if (!hasVideoPlace) {
                    gsap.set(".jpt-video-container", { autoAlpha: 1 })
                }
                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    video.volume = 0;
                    PremiumJpControls.callOnClickFullScreen(function() {
                        video.muted = false;
                        Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                    });
                    Premium.video.sync(video, 1);

                    Premium.video.switchOnScroll(document.querySelector(".jpt-video-container"), 3, function(pos) {
                        if (hasVideoPlace){
                            if (pos === "down") {
                                gsap.timeline()
                                    .to(".jpt-video-place", { duration: .4, autoAlpha: 0 }, 0)
                                    .to(".jpt-video-container", { duration: .4, autoAlpha: 1, pointerEvents:"auto" , onUpdate: PremiumJpControls.resizeAll }, 0)
                            } else {
                                gsap.timeline()
                                    .to(".jpt-video-container", { duration: .4, autoAlpha: 0, pointerEvents:"none" }, 0)
                                    .to(".jpt-video-place", { duration: .4, autoAlpha: 1 }, 0)
                            }
                        }
                    });
                    var prom = video.play();
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                gsap.set(".jpt-show-when-scrolled", { marginBottom: calculateMargin(), autoAlpha: 0 });

                Premium.video.switchOnScroll(undefined, 2, function(pos) {
                    if (pos === "down") {
                        gsap.to(".jpt-show-when-scrolled", .2, { height: "auto", marginBottom: "0", autoAlpha: 1 });
                    } else {
                        gsap.timeline().to(".jpt-show-when-scrolled", .2, { autoAlpha: 0 })
                            .to(".jpt-show-when-scrolled", .2, { marginBottom: calculateMargin() }, "-=.17");
                    }
                });

                break;


            case "body_back":
                // back panel code here

                break;

            case "body_expanded":
                if (document.querySelector(".jpt-section-video")) {
                    Premium.creative.createVideoPlayerAppended = function(video) {
                        video.volume = 0;
                        Premium.video.sync(video, undefined, Premium.video.SyncType_Get);
                        video.addEventListener("playing", function() {
                            // setTimeout(function(){
                            document.body.style.display = "block";
                            document.body.style.opacity = 1;
                            // }, 200)  
                        }, { once: true })
                        var prom = video.play();
                        if (prom) {
                            prom.catch(function() {})
                        }
                        // Safari fix
                        document.body.addEventListener("click", function(e) {
                            if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
                                if (!video.paused) {
                                    setTimeout(function() {
                                        video.play();
                                    }, 800)
                                }
                            }
                        });
                    }
                } else {
                    document.body.style.display = "block";
                    document.body.style.opacity = 1;
                }

                // Premium.utils.blockScroll.block(true)

                break;
        }

        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");
                }
                Premium.template.hideScrollBar(smallPrintEl.querySelector("P"));
            }
            window.addEventListener("resize", doSmallPrint);
            window.addEventListener("load", doSmallPrint, { once: true })
        }

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