(function () {
    tp = window["tp"] || [];
  	var cX = cX || {}; cX.callQueue = cX.callQueue || [];

    /* Checkout related */
    /**
     * Event properties
     *
     * chargeAmount - amount of purchase
     * chargeCurrency
     * uid
     * email
     * expires
     * rid
     * startedAt
     * termConversionId
     * termId
     * promotionId
     * token_list
     * cookie_domain
     * user_token
     *
     */
    function onCheckoutComplete(data) {}

    function onCheckoutExternalEvent(e) {
      if (getCookie('Loggedin') !== true) {
         window.dataLayer = window.dataLayer || [];
         window.dataLayer.push({
           event: 'elofizetes_kezdemenyezes',
           offer: e.eventName
         });
       }
    }

    function onCheckoutClose(event) {
        /* Default behavior is to refresh the page on successful checkout */
        if (event && event.state == "checkoutCompleted") {
            location.reload();
        }
    }

    function onCheckoutCancel() {}

    function onCheckoutError() {}

    function onCheckoutSubmitPayment() {}

    /* Meter callback */
    function onMeterExpired() {

    }

    /* Meter callback */
    function onMeterActive() {

    }

    /* Callback executed when a user must login */
    function onLoginRequired() {
        // this is a reference implementation only
        // your own custom login/registration implementation would
        // need to return the tinypass-compatible userRef inside the callback

        // mysite.showLoginRegistration(function (tinypassUserRef)
        // tp.push(["setUserRef", tinypassUserRef]); // tp.offer.startCheckout(params); // }
        // this will prevent the tinypass error screen from displaying
      	console.log('Login required');
        window.location.href = "https://www.glamour.hu/login?source=buy_flow";

        return true;
    }

    /* Callback executed after a tinypassAccounts login */
    function onLoginSuccess() {}

    /* Callback executed after an experience executed successfully */
    function onExperienceExecute(event) {}

    /* Callback executed if experience execution has been failed */
    function onExperienceExecutionFailed(event) {}

    /* Callback executed if external checkout has been completed successfully */
    function onExternalCheckoutComplete(event) {
        /* Default behavior is to refresh the page on successful checkout */
        location.reload();
    }

    tp.push(["setAid", 'aSQFvCGype']);
    tp.push(["setCxenseSiteId", "5857337009830273772"]);
    tp.push(["setEndpoint", 'https://buy-eu.piano.io/api/v3']);
    tp.push(['setPianoIdUrl', 'https://id-eu.piano.io']);
    tp.push(["setUseTinypassAccounts", false]);
    tp.push(["setUsePianoIdUserProvider", false]);
    tp.push(["setUsePianoIdLiteUserProvider", true]);
  
    if (window.pianoAnalyticsData) {
      cX.callQueue.push(['setCustomParameters', window.pianoAnalyticsData]);
    }

    /* checkout related events */
    tp.push(["addHandler", "checkoutComplete", onCheckoutComplete]);
    tp.push(["addHandler", "checkoutClose", onCheckoutClose]);
    tp.push(["addHandler", "checkoutCustomEvent", onCheckoutExternalEvent]);
    tp.push(["addHandler", "checkoutCancel", onCheckoutCancel]);
    tp.push(["addHandler", "checkoutError", onCheckoutError]);
    tp.push(["addHandler", "checkoutSubmitPayment", onCheckoutSubmitPayment]);

    /* user login events */
    tp.push(["addHandler", "loginRequired", onLoginRequired]);
    tp.push(["addHandler", "loginSuccess", onLoginSuccess]);

    /* meter related */
    tp.push(["addHandler", "meterExpired", onMeterExpired]);
    tp.push(["addHandler", "meterActive", onMeterActive]);

    tp.push(["addHandler", "experienceExecute", onExperienceExecute]);
    tp.push(["addHandler", "experienceExecutionFailed", onExperienceExecutionFailed]);

    /* external checkout related events */
    tp.push(["addHandler", "externalCheckoutComplete", onExternalCheckoutComplete]);

    tp.push(['addHandler', 'logout', function () {
        let ssoCookie = tp.util.findCookieByName('__sso');
        tp.util.deleteCookie('__piano');
        tp.util.deleteCookie('__sso');
        tp.util.deleteCookie('mondriaanAccess');
        tp.util.deleteCookie('mondriaanRefresh');
        tp.util.deleteCookie('mondriaanExpires');
        tp.util.deleteCookie('__id');
        window.location.href = 'https://login.login.ringier.hu/session/end?post_logout_redirect_uri=https://www.glamour.hu/plusz/logout&id_token_hint=' + ssoCookie;
    }]);
  
  	if (document.querySelector('body.dark')) {
      tp.push(['setCustomVariable', 'darkmode', 'true']);
    }

    tp.push(["init", function () {
        let externalJWT = tp.util.findCookieByName('__piano');
        tp.push(["setExternalJWT", externalJWT]);
      	let jwt = {sub: 'anon'};
        if (tp.getExternalJWT()) {
          jwt = tp.jwtDecode(tp.getExternalJWT());
        }
        let publishedDateMeta = document.querySelector('meta[property="article:published_time"]');
        let publicationDate = '';
        let publicationTime = ''
        if (publishedDateMeta && publishedDateMeta.content) {
            let dateSegments = publishedDateMeta.content.split('T');
            publicationDate = dateSegments[0];
            publicationTime = dateSegments[1].split('+');
        }
        let articleUUID = document.querySelector('meta[name="DC.Identifier"]');
        let contentId = '';
        if (articleUUID && articleUUID.content) {
            contentId = articleUUID.content
        }
        let author = document.querySelector('meta[name="author"]');
        let authorName = '';
        if (author && author.content) {
            authorName = author.content;
        }
      	
      	if (jwt.sub !== 'anon') {
          tp.setGA4Config({
            measurementId: 'G-730JSMR4FL',
            eventParameters: {
              send_page_view: true,
              page_location: location.href,
              page_title: document.title,
              user_id: jwt.sub,
              publication_date: publicationDate + ' ' + publicationTime[0],
              content_id: contentId,
              content_author: authorName
            }
          });
        }
        
        tp.experience.init();
    }]);
})();


// do not change this section
// |BEGIN INCLUDE TINYPASS JS|
(function (src) {
    var a = document.createElement("script");
    a.type = "text/javascript";
    a.async = true;
    a.src = src;
    var b = document.getElementsByTagName("script")[0];
    b.parentNode.insertBefore(a, b)
})("https://cdn-eu.piano.io/api/tinypass.min.js");
// |END   INCLUDE TINYPASS JS|


tp.push(["init", function () {
    const externalJWTLoadedInterval = setInterval(() => {
        if (!tp.externalJWT) return;
        const externalJWTLoaded = new Event("externalJWTLoaded");
        window.dispatchEvent(externalJWTLoaded);
        clearInterval(externalJWTLoadedInterval);
    }, 100);

    window.addEventListener("externalJWTLoaded", () => {
        setTimeout(() => {
            const loadingCover = document.querySelector(".loadingCover");
            if (loadingCover) loadingCover.remove();
        }, 300);
    });
}]);