 (function () {
    tp = window["tp"] || [];
    // For randomized testing  
    document.cookie = "_pc_random="+Math.floor(Math.random()*3)+"; path=/;"
    /* 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() {
    }

    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

        return false;
    }

    /* 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();
    }
  
    function onShowTemplate(templateParams) {
        cX.setEventAttributes({origin: "mai-web", persistedQueryId: '8fa5ebf7ea7084503670f3547744fe709bb1983c'});
        var customParams = {experienceId:templateParams.experienceId,templateId:templateParams.templateId};
        if (templateParams.templateVariantId) {
          customParams.templateVariantId = templateParams.templateVariantId;
        }
        cX.sendEvent("showTemplate",customParams);
    }
    function checkMetaTag(tagName) {
      try {
        const tag = document.querySelector('meta[name="'+tagName+'"]');
        if (tag) { return tag.content; }
        else { return null; }
      } catch(e) {
        console.log(e);
        return null;
    　}
    }
    function addPubDate() {
      const pubDate = checkMetaTag('cXenseParse:recs:publishtime');
      if (pubDate) { tp.push(["setContentCreated", pubDate]); }
    }
    function checkUserAgent() {
      if (navigator.userAgent.includes('GSA')) {
        tp.push(["setCustomVariable", "userAgent", "GSA"]);
      }
    }
    addPubDate();
    checkUserAgent();
  
    tp.push(["setAid", 'gmsPBEZtpj']);
    tp.push(["setEndpoint", 'https://buy-ap.piano.io/api/v3']);
    tp.push(["setUseTinypassAccounts", false ]);
    tp.push(["setUsePianoIdUserProvider", false ]);
    tp.push(["setUsePianoIdLiteUserProvider", false ]);
    tp.push(["setPianoIdUrl", 'https://id-ap.piano.io']);

    /* 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", "showTemplate", onShowTemplate]);

    tp.push(["init", function () {
      cX.callQueue.push(["invoke", function() {
        cX.getUserSegmentIds({
          persistedQueryId: '93a84cfdd688c99ecc7a464da0173ced06a227e9',
          timeout: 100,
          callback: function (segments) {
            if (segments && segments.length > 0) {
              tp.setCustomVariable("dmpSegments",segments);
            }
            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)})("//code.piano.io/api/tinypass.min.js");
    // |END   INCLUDE TINYPASS JS|

