document.addEventListener("DOMContentLoaded", function() {
    // Find all iframes with pollbolt-iframe class
    var iframes = document.querySelectorAll("iframe.pollbolt-iframe");
    
    // Process each iframe
    iframes.forEach(function(iframe) {
        // Hide scrollbar for all iframes
        iframe.style.overflow = "hidden";
        iframe.setAttribute("scrolling", "no");
        
        // Create container for iframe if it doesn't exist
        var parent = iframe.parentNode;
        if (!parent.classList.contains("pollbolt-iframe-container")) {
            var container = document.createElement("div");
            container.className = "pollbolt-iframe-container";
            parent.insertBefore(container, iframe);
            container.appendChild(iframe);
        }
        
        // Function to adjust iframe height
        function adjustIframeHeight() {
            try {
                // Try to get content height via messages
                iframe.contentWindow.postMessage({ type: "pollbolt-get-height" }, "*");
                
                // Hide scrolls directly in iframe document
                try {
                    var iframeDoc = iframe.contentWindow.document;
                    var style = iframeDoc.createElement("style");
                    style.textContent = `
                        html, body {
                            overflow: hidden !important;
                            scrollbar-width: none !important;
                            -ms-overflow-style: none !important;
                        }
                        
                        ::-webkit-scrollbar {
                            display: none !important;
                            width: 0 !important;
                            height: 0 !important;
                        }
                    `;
                    iframeDoc.head.appendChild(style);
                } catch(e) {
                    // Expected cross-origin errors, ignore
                }
            } catch(e) {
                // Expected cross-origin errors, ignore
            }
        }
        
        // iframe load handler
        iframe.onload = function() {
            adjustIframeHeight();
            
            // Repeated checks for reliability
            setTimeout(adjustIframeHeight, 500);
            setTimeout(adjustIframeHeight, 1000);
            setTimeout(adjustIframeHeight, 2000);
        };
        
        // Listen for messages from iframe
        window.addEventListener("message", function(event) {
            if (typeof event.data === "object" && event.data.type === "pollbolt-height") {
                // Got height from iframe
                var height = parseInt(event.data.height) + 50; // Add padding
                iframe.style.height = height + "px";
            }
        });
    });
});
