let attempt = 0;
let musicOn = !0;
function toggleMusic() {
  musicOn = !musicOn;
  document.getElementById("musicToggle").textContent = musicOn
    ? "🔊 On"
    : "🔇 Off";
  if (!musicOn) {
    bgmPage1.pause();
    bgmPage2.pause();
  } else {
    if (page1.style.display === "flex") bgmPage1.play();
    if (page2.style.display === "flex") bgmPage2.play();
  }
}
function startGame() {
  bgmPage1.pause();
  bgmPage1.currentTime = 0;
  if (musicOn) bgmPage2.play();
  page1.style.display = "none";
  page2.style.display = "flex";
}
function animateStick() {
  stick.style.transform = "translateX(-50%) rotate(60deg)";
  setTimeout(() => {
    stick.style.transform = "translateX(-50%) rotate(0deg)";
  }, 200);
}
function hitCan() {
  document.getElementById("hitBtn").disabled = !0;
  attempt++;
  animateStick();
  hitSound.currentTime = 0;
  hitSound.play();
  const jars = document.querySelectorAll(".jar");
  jars.forEach((jar) => {
    jar.classList.add("shake");
    setTimeout(() => jar.classList.remove("shake"), 300);
  });
  document.getElementById("resultText").style.display = "flex";
  if (attempt === 1 || attempt === 2) {
    resultText.innerText = `អវាសនៈ! តេស្តម្ដងទៀត, អ្នកនៅសល់ ${3 - attempt} ដង។`;
    document.getElementById("hitBtn").disabled = !1;
  } else if (attempt === 3) {
    document.getElementById("hitBtn").disabled = !0;
    resultText.innerText = "អ្នកវាយត្រូវហើយ! 🎉";
    document.getElementById("resultText").style.display = "flex";
    const jarTrack = document.querySelector("#jarTrack");
    const hitBtn = document.getElementById("hitBtn");
    const successSound = document.getElementById("successSound");
    const breakSound = document.getElementById("breakSound");
    jarTrack.style.animation = "none";
    const middleJar = document.querySelectorAll(".jar")[2];
    middleJar.src = "images/pg2-kdm-broken.webp";
    middleJar.classList.add("broken");
    breakSound.currentTime = 1;
    breakSound.play();
    bgmPage2.pause();
    successSound.currentTime = 0;
    successSound.play();
    hitBtn.style.display = "none";
    successSound.onended = () => {
      if (musicOn) {
        bgmPage2.currentTime = 0;
        bgmPage2.play();
      }
    };
    setTimeout(() => {
      document.getElementById("successOverlay").style.display = "flex";
    }, 1200);
  }
}
window.addEventListener("click", function onceClickToPlay() {
  if (musicOn && page1.style.display === "flex") {
    bgmPage1.play().catch((e) => console.warn("Autoplay blocked:", e));
  }
  window.removeEventListener("click", onceClickToPlay);
});
var getUrlParameter = function getUrlParameter(sParam) {
  var sPageURL = window.location.search.substring(1),
    sURLVariables = sPageURL.split("&"),
    sParameterName,
    i;
  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split("=");
    if (sParameterName[0] === sParam) {
      return typeof sParameterName[1] === undefined
        ? !0
        : decodeURIComponent(sParameterName[1]);
    }
  }
  return !1;
};
$(document).ready(function ($) {
  var affiliate = getUrlParameter("aff");
  if (affiliate === "" || affiliate === "false" || affiliate === !1) {
    var affiliate = getUrlParameter("code");
    if (affiliate === "" || affiliate === "false" || affiliate === !1) {
      affiliate = "51263";
    }
  }
  $("#btnCta").attr(
    "href",
    "https://www.bk8bh.com/km-kh/register?affid=" + affiliate
  );
  $("#btnCta").attr("target", "_top");
});
