$(document).ready(function () {
  // Appending initial list items to #slots.
  // Note: There seem to be three identical HTML strings being prepended in the
  // original code. This might be intentional or a copy-paste error in the source.

  $("#slots").prepend(`
    <li data-area="game_slot" data="Slots">
      <div class="game-wrapper">
        <div class="game-list-core">
          <div class="game-list-content">
            <span class="game-favorite" data-id="1876"><i class="fas fa-star"></i></span>
            <a data-id="74" data-href="/play/pgs/74" data-loading="https://asetpermata.xyz/img/pg.gif" class="open-game custom-popup-game">
              <img width="100%" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1Vwo9RJDdgHqTCLUe0h85Uqhe1-1xpIs9a2UQklFvNkoKv4kaS7u0zYCVYkfK0Qajs9i4WvKA0KiocsJS9DsH5gSJOzNADvwYtDRUMUwAuCW2BemgiEkLk6gQFCNmCvvc3l2UVfS2QbVj4iWD3bLC48OUL7KTecqqflxy8Ics2kZE0lJfgbNKAPjjV9sU/s600/mj2%20(2).gif" alt="Mahjong-ways-2">
            </a>
          </div>
        </div>
        <div class="figure-text">
          <p>MJ 2 SUPER SCATTER</p>
        </div>
      </div>
  </li>
  `);
  $("#slots").prepend(`
    <li data-area="game_slot" data="Slots">
      <div class="game-wrapper">
        <div class="game-list-core">
          <div class="game-list-content">
            <span class="game-favorite" data-id="1869"><i class="fas fa-star"></i></span>
            <a data-id="65" data-href="/play/pgs/65" data-loading="https://asetpermata.xyz/img/pg.gif" class="open-game custom-popup-game">
              <img width="100%" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVAhYtceKC0t-9sJ5Pu48l6gD_59xJ9AoyNIpEr3Mlp4EhIH1sE9jMkzaMv3kfl7MaoC9Wsk_DMZl_ueRRaKcea6AeSNaeO1Urru68oyO7V7LKLKo8RU3pdVbrupOUsq11bJbPc_jymYeZXjcdmIiQ34dpgCNyO8YtP6ikjJ_F65zNJg0ca2qu5H-82FdW/s960/MJ1%20(2).gif" alt="Mahjong-ways">
            </a>
          </div>
        </div>
        <div class="figure-text">
          <p>MJ 1 SUPER SCATTER</p>
        </div>
      </div>
  </li>
  `);
  $("#slots").prepend(`
    <li data-area="game_slot" data="Slots">
      <div class="game-wrapper">
        <div class="game-list-core">
          <div class="game-list-content">
            <span class="game-favorite" data-id="5055"><i class="fas fa-star"></i></span>
            <a data-id="104" data-href="/play/pgs/104" class="open-game custom-popup-game">
              <img width="100%" src="https://asetpermata.xyz/img/wild-bandito.gif" alt="New Wild Bounty">
            </a>
          </div>
        </div>
        <div class="figure-text">
          <p>New Wild Bandito</p>
        </div>
      </div>
  </li>
  `);

  $("#slots").prepend(`
    <li data-area="game_slot" data="Slots">
      <div class="game-wrapper">
        <div class="game-list-core">
          <div class="game-list-content">
            <span class="game-favorite" data-id="5055"><i class="fas fa-star"></i></span>
            <a data-id="135" data-href="/play/pgs/135" class="open-game custom-popup-game">
              <img width="100%" src="https://blogger.googleusercontent.com/img/a/AVvXsEj37NPT1cII-KGdnLkIgNm2x_caFe1Ky8W4NR1IpqQTu73y2bTM7h7lkzd_0ZyckuatzkXrFHKSFtosQMU1BRnzp0ovNpKu5JIgAHGV8ylPNcv-LKKJ2Jvbb7uFDpgVouK3EH9NZMVt6qfl6-JWgCOrNlQEg9kVrrdREBz0KorM-6GjS4Cmg5tdadrcCQ02" alt="New Wild Bounty">
            </a>
          </div>
        </div>
        <div class="figure-text">
          <p>New Wild Bounty</p>
        </div>
      </div>
  </li>
  `);

  // Attach a click event handler to elements with the class 'a.custom-popup-game'
  $(document).on("click", "a.custom-popup-game", function (event) {
    // Prevent the default action (following the link)
    event.preventDefault();
    // Stop the event from propagating further
    event.stopImmediatePropagation();

    // Get the target URL from data-href or href attribute, default to '#' if neither is found
    const targetUrl = $(this).attr("data-href") || $(this).attr("href") || "#";
    // Get the loading image URL from the data-loading attribute
    let loadingImageUrl = $(this).attr("data-loading");

    // If data-loading is null or empty after trimming whitespace, use a default image
    if (!loadingImageUrl || !loadingImageUrl.trim()) {
      loadingImageUrl =
        "https://asetpermata.xyz/img/pg.gif";
    }

    // Show the loading popup with the determined image URL
    showLoadingPopup(loadingImageUrl);

    // After a delay of 1152 milliseconds, navigate to the target URL
    setTimeout(() => {
      window.location.href = targetUrl;
    }, 1152); // 0xbb8 in hex is 1152 milliseconds
  });

  // Function to show a loading popup
  function showLoadingPopup(imageUrl) {
    // If a loading popup already exists, do nothing
    if ($("#loading-popup").length > 0) {
      return;
    }

    // Create a new div element for the popup
    const popupDiv = $("<div>", {
      id: "loading-popup",
      css: {
        position: "fixed", // Position relative to the viewport
        top: 0,
        left: 0,
        width: "100%",
        height: "100%",
        backgroundColor: "rgba(0, 0, 0, 0.8)", // Semi-transparent dark background
        display: "flex", // Use flexbox for centering content
        justifyContent: "center", // Center horizontally
        alignItems: "center", // Center vertically
        zIndex: 10000, // Ensure it's on top of other content
      },
    });

    // Create an img element for the loading image
    const loadingImg = $("<img>", {
      src: imageUrl,
      alt: "Loading...",
      css: {
        maxWidth: "400px", // Limit image width
        borderRadius: "10px", // Round the corners
      },
    });

    // Append the image to the popup div
    popupDiv.append(loadingImg);
    // Append the popup div to the body of the document
    $("body").append(popupDiv);
  }
});