<!DOCTYPE html>
<html>
<head>
    <script src="https://js.onclckvd.com/in-stream-ad-admanager/tma.js"></script>

    <style>
        /* style for clickable div */
        [onclickaad] {
            width: 200px;
            height: 60px;
            background: #222;
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            border-radius: 8px;
            margin-bottom: 20px;
            user-select: none;

            /* Disabled by default */
            opacity: 0.5;
            pointer-events: none;
        }

        /* Button disabled by default */
        #showAdBtn {
            opacity: 0.5;
            pointer-events: none;
        }

        /* When ad system is ready */
        .ready {
            opacity: 1 !important;
            pointer-events: auto !important;
        }
    </style>
</head>
<body>

<script>
    let showAdFunction = null;

    // Initialize ad engine
    window.initCdTma?.({ id: 6101738 })
        .then(show => {
            showAdFunction = show;
            console.log("Ad system ready!");

            // Enable DIV and Button when ready
            document.querySelector('[onclickaad]').classList.add("ready");
            document.querySelector('#showAdBtn').classList.add("ready");
        })
        .catch(err => console.error("Init error:", err));

    // Function to play ad
    function playAd() {
        if (!showAdFunction) {
            console.log("Ad not ready yet");
            return;
        }

        showAdFunction()
            .then(() => console.log("Ad played successfully"))
            .catch(err => console.error("Ad failed:", err));
    }
</script>

<!-- DIV selector -->
<div onclickaad id="onclickaad" class="onclickaad">
    Click DIV (selector: [onclickaad])
</div>

<!-- Button selector -->
<button id="showAdBtn">
    Show Ad
</button>

<script>
    // DIV triggers ad
    document.querySelector('[onclickaad]').addEventListener('click', playAd);

    // Button triggers ad
    document.querySelector('#showAdBtn').addEventListener('click', playAd);
</script>

</body>
</html>
