function tappxReceiveInterstitial() {
  console.log('tappxReceiveInterstitial');
}

function tappxAdClosedInterstitial() {
  console.log('AdClosed');
}

function tappxInterstitialLeftApplication() {
  console.log('LeftApplication');
}

function tappxFailInterstitial(ErrorCode) {
  console.log('FailInterstitial: ' + ErrorCode);
}

function tappxAdClosedInterstitial() {
  console.log('AdClosedInterstitial');
}

var adManager = {
  message1: "The game will continue in ",
  message2: " seconds",
  self: null,
  adCreated: false,
  adContainer: null,
  adTime: 20,
  currentAdTime: 0,
  countDownTimer: null,
  isDisplayed: false,
  userClosed: true,
  callBack: null,
  adTimeOut: true,
  elMessage: null,
  init: function() {
    InitTappxInterstitial(tappxKey,tappxDiv);
    adManager.adContainer = document.getElementById('adContainer');
    tappxAdClosedInterstitial = adManager.tappxAdClosedInterstitialEvent;
    //if (!adManager.adCreated) {
    //	adManager.createAd();
    //}
  },
  createAd: function() {
    CreateInterstitial();
    adManager.adCreated = true;
  },
  removeAd: function() {
    adManager.adContainer.innerHTML = "<div id='tappxDiv'></div>";
    adManager.adCreated = false;
  },
  refreshAd: function() {
    RefreshInterstitial();
  },
  showAd: function(callBack) {
    adManager.callBack = callBack;
    
    if (adManager.elMessage == null) {
      adManager.elMessage = document.createElement("div");
      adManager.elMessage.style.position = "fixed";
      adManager.elMessage.style.width = "100%";
      adManager.elMessage.style.bottom = "0px";
      adManager.elMessage.style.padding = "8px";
      adManager.elMessage.style.textAlign = "center";
      adManager.elMessage.style.color = "white";
      var elTappx = document.getElementById('tappxDiv');
      elTappx.appendChild(adManager.elMessage);
    }
    
    tappxReceiveInterstitial = adManager.waitForAdReady;
    if (adManager.isDisplayed) return;
    console.log('showAd');
    adManager.adContainer.style.display = 'block';
    adManager.isDisplayed = true;
    adManager.currentAdTime = adManager.adTime;

    if (!adManager.adCreated) {
      adManager.createAd();
      adManager.refreshAd();
    } else {
      if (adManager.userClosed) {
        adManager.refreshAd();
      } else {
        adManager.countDownAd();
      }
    }
  },
  waitForAdReady: function() {
    console.log("AdReady");
    adManager.countDownAd();
  },
  countDownAd: function() {
    if (!adManager.adTimeOut) return;
    adManager.elMessage.innerHTML = adManager.message1 + adManager.currentAdTime + adManager.message2;
    //console.log(adManager.currentAdTime);
    adManager.currentAdTime--;
    if (adManager.currentAdTime <= 0) {
      adManager.hideAd();
    } else {
      adManager.countDownTimer = window.setTimeout(function() {adManager.countDownAd();}, 1000);
    }
  },
  tappxAdClosedInterstitialEvent: function() {
    adManager.userClosed = true;
    adManager.hideAd();
  },
  hideAd: function() {
    //adManager.removeAd();
    console.log('hideAd');
    clearTimeout(adManager.countDownTimer);
    adManager.countDownTimer = null;
    adManager.adContainer.style.display = 'none';
    adManager.isDisplayed = false;
    if (adManager.callBack != null) {
      adManager.callBack();
      adManager.callBack = null;
    }
  }
};