const images = Array.from(document.getElementsByClassName("content"));
let chances = 0;
var pop = document.getElementById("popup-result");

images.forEach((image) => {
  image.addEventListener("click", () => {
    // increment counter
    chances++;
    // vibrate animation
    image.classList.add("vibrate");
    setTimeout(() => {
      image.classList.remove("vibrate");
      setTimeout(() => {
        let nextImageUrl = "";
        if (chances == 1) {
          nextImageUrl = "./image/crossX.png";
          image.classList.add("crossX-img");
          image.style.pointerEvents = "none";
        } else {
          nextImageUrl = "./image/goldcoin.png";
          image.classList.add("goldcoin-img");
          image.style.pointerEvents = "none";
        }
        image.src = nextImageUrl;

        if (chances == 2) {
          nextImageUrl = "./image/crossX.png";
          image.classList.add("crossX-img");
          image.style.pointerEvents = "none";
        } else {
          nextImageUrl = "./image/goldcoin.png";
          image.classList.add("goldcoin-img");
          image.style.pointerEvents = "none";
        }
        image.src = nextImageUrl;

        if (chances == 3) {
          nextImageUrl = "./image/goldcoin.png";
          image.classList.add("goldcoin-img");
          image.style.pointerEvents = "none";
        } else {
          nextImageUrl = "./image/crossX.png";
          image.classList.add("crossX-img");
          image.style.pointerEvents = "none";
        }
        image.src = nextImageUrl;
      }, 100);
    }, 200);

    // freeze chest if 2 chances are used
    if (chances === 3) {
      images.forEach((image) => {
        image.style.pointerEvents = "none";
        setTimeout(showPopup, 2000);
      });
    }
  });
});

$(document).ready(function () {
  setInterval(function () {
    $(".count").html(function (i, val) {
      val = val.replace(/,/g, "");
      return (parseFloat(val, 1) + 0.01)
        .toFixed(2)
        .toString()
        .replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    });
  }, 7500 / 5.5);
});

function showPopup() {
  pop.innerHTML = '<img onclick="redirectLink()" src="image/Pop Up.png">';
  cnyPopup.classList.add("show");
}

function redirectLink() {
  window.location.href = "assets/load.html";
}
