/*
CustomJS LIVE
Version 1.2
*/
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, "includes", {
    enumerable: false,
    value: function (obj) {
      var newArr = this.filter(function (el) {
        return el == obj;
      });
      return newArr.length > 0;
    }
  });
}

if (!(IMI.CustomJS && IMI.CustomJS.customJSisLoaded)) {
  (function () {
    IMI.CustomJS = {
      customJSisLoaded: true,
      locks: {},
      afterInterceptsLoaded: function () { },
      mods: {
        getIntercept: function (id) {
          for (var i = 0; i < IMI.Intercepts.length; i++) {
            if (IMI.Intercepts[i].id == id) {
              return IMI.Intercepts[i];
            }
          }
        },
        /**
         * Trigger Intercept via JavaScript.
         *
         * @param {integer} interceptId
         *   Id of intercept to trigger
         */
        triggerIntercept: function (interceptId) {
          var intercept = IMI.CustomJS.mods.getIntercept(interceptId);
          if (intercept) {
            IMI.InterceptUtil.appendAdditionalDataToURL(intercept);
            IMI.InterceptUtil.triggerIntercept.call(
              IMI.InterceptUtil.triggerIntercept,
              intercept,
              new Map()
            );
          }
        },
        /**
         * Check if site is in the include list
         *
         * Add as last trigger (make sure it is last! If not it will bypass any triggers you put after it):
         *
         *  Trigger type: JS Expression
         *
         *  Trigger Operator: Equals
         *
         *  Js Expression: IMI.CustomJS.mods.checkSiteIncluded(<interceptId>);
         *
         *  Return value to compare: true
         *
         * @param {integer} interceptId
         *   Id of intercept to trigger
         */
        checkSiteIncluded: function (interceptId) {
          var intercept = IMI.CustomJS.mods.getIntercept(interceptId);
          if (!intercept) {
            return false;
          }


          return false;
        },

        addCssToHead: function (css) {
          var head = document.head || document.getElementsByTagName('head')[0];
          var style = document.createElement('style');

          if (head) {
            head.appendChild(style);
            style.type = 'text/css';
            style.appendChild(document.createTextNode(css));
          }

        },

        setLocalFatigue: function (interceptId, fatigue) {
          window.localStorage.setItem("IMI_InterceptFatigue_" + interceptId, new Date().setDate(new Date().getDate() + fatigue));
        },

        checkLocalFatigue: function (interceptId) {
          var n = window.localStorage.getItem("IMI_InterceptFatigue_" + interceptId);
          if (n && (new Date(parseInt(n)) > (new Date()))) {
            return false;
          }
          return true;
        },

        fatigueOnPostMessage: function (interceptId, fatigue) {

          var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
          var eventer = window[eventMethod];
          var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

          // Listen to message from child window
          eventer(messageEvent, function (event) {
            // TODO change the fatigue here
            IMI.CustomJS.mods.setLocalFatigue(interceptId, fatigue);

          }, false);
        },

        styleStatsExperienceDesktop: function (interceptId) {

          if (!IMI.CustomJS.locks['styleStatsExperienceDesktop' + interceptId]) {
            IMI.CustomJS.locks['styleStatsExperienceDesktop' + interceptId] = true;

            var intercept = IMI.CustomJS.mods.getIntercept(interceptId);

            var visualId = intercept.visual.id;
            var subVisualId = visualId + 1;
            if (Object.getPrototypeOf(intercept.visual.type).name == 'BANNER') {
              var subVisualId = intercept.visual.bannerButtons[0].subVisual.id;
            }

            var styleModal =
              `#_im_banner__${visualId} #im_button1 {
  font-family: var(--g-typography-typeface) !important;
  font-size: 1.428rem !important;
  line-height: 1.286 !important;
  font-weight: 700 !important;
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - .8rem), calc(100% - .8rem) 100%, 0 100%);
  padding: 0px 20px;
}
#_im_banner__${visualId} div#im_button1 > svg { 
  height: 11px;
  margin-left: 8px;
  width: 11px;
}
#_im_banner__${visualId} { 
  font-family: var(--g-typography-typeface) !important;
  font-size: 1.428rem !important;
  line-height: 1.286 !important;
  font-weight: 500 !important;
}

@media only screen and (min-width: 800px) {
  #_im_banner__${visualId} { 
    width: calc(50% - 185px) !important;
    //left: calc(390px + calc(calc(50% - 185px) / 2)) !important;
	left: calc((100% - calc(calc(50% - 185px))) / 2) !important;
  }
  #_im_banner__${visualId} #banner_close {
    right: -15px
  }
}`;

            IMI.CustomJS.mods.addCssToHead(styleModal);
            
          }
          return true;
        },

        stylePlayExperienceDesktop: function (interceptId) {

          if (!IMI.CustomJS.locks['stylePlayExperienceDesktop' + interceptId]) {
            IMI.CustomJS.locks['stylePlayExperienceDesktop' + interceptId] = true;

            var intercept = IMI.CustomJS.mods.getIntercept(interceptId);

            var visualId = intercept.visual.id;
            var subVisualId = visualId + 1;
            if (Object.getPrototypeOf(intercept.visual.type).name == 'BANNER') {
              var subVisualId = intercept.visual.bannerButtons[0].subVisual.id;
            }

            var styleModal =
              `#_im_banner__${visualId} #im_button1 {
  font-family: var(--g-typography-typeface) !important;
  font-size: 1.428rem !important;
  line-height: 1.286 !important;
  font-weight: 700 !important;
  clip-path: polygon(0 0, 100% 0, 100% calc(100% - .8rem), calc(100% - .8rem) 100%, 0 100%);
  padding: 0px 20px;
}
#_im_banner__${visualId} div#im_button1 > svg { 
  height: 11px;
  margin-left: 8px;
  width: 11px;
}
#_im_banner__${visualId} { 
  font-family: var(--g-typography-typeface) !important;
  font-size: 1.428rem !important;
  line-height: 1.286 !important;
  font-weight: 500 !important;
}

@media only screen and (min-width: 900px) {
  #_im_banner__${visualId} { 
    width: calc(50% - 185px) !important;
    //left: calc(390px + calc(calc(50% - 185px) / 2)) !important;
	left: calc((100% - calc(calc(50% - 185px))) / 2) !important;
  }
  #_im_banner__${visualId} #banner_close {
    right: -15px
  }
}`;

            IMI.CustomJS.mods.addCssToHead(styleModal);
            
          }
          return true;
        },


        triggerInMomentUnsubscribeIntercept: function () {
          // find the fist intercept that matches UAT and PRD id.
          // UAT and PRD on different domains so only one should be on the domain. 
      
          const idList = [364,376];

          var ids = IMI.Intercepts.map(intercept => intercept.id);
          // Sort the ids in ascending order
          ids.sort((a, b) => a - b);

          // Find the first id in the sorted array that matches an id in idList
          const interceptId = ids.find(id => idList.includes(id));

          const intercept = IMI.CustomJS.mods.getIntercept(interceptId);

          var styleModal =
          `
#intercept_section__${intercept.visual.id}{
    top: 17% !important;
    left: 17% !important;
    height:551px !important;
}
@media  only screen and (max-width: 1060px){
    #intercept_section__${intercept.visual.id}{
        left: 5% !important;
        width: 90% !important;
    }

}
 @media only screen and (max-width:755px) {
    #intercept_section__${intercept.visual.id}{
        height:571px !important;
    }
 }

 @media only screen and (max-width:630px) {
    #intercept_section__${intercept.visual.id}{
        height:616px !important;
    }
 }

 @media only screen and (max-width:480px) {
    #intercept_section__${intercept.visual.id}{
        top:5% !important;
        left:5% !important;
        height:684px !important;
        max-height:90% !important;
    }
 }

`;

          IMI.CustomJS.mods.addCssToHead(styleModal);

          IMI.CustomJS.mods.triggerIntercept(interceptId)
          
        }

      },
    };
  })();
}
