(function($) {
    'use strict';
    class MMPDBModal {
        constructor() {
            // Don't initialize if we're in a page builder
            if (window.Bricks || window.elementorFrontend || window.wpb) {
                return;
            }
            this.init();
        }
        init() {
            // Only create modal if we have modal links on the page
            if ($('.modal-link').length === 0) {
                return;
            }
            this.createModalHTML();
            this.bindEvents();
        }
        createModalHTML() {
            // Check if modal already exists
            if ($('.mmpdb-modal').length > 0) {
                return;
            }
            const modalHTML = `
                <div class="mmpdb-modal">
                    <div class="mmpdb-modal__content">
                        <button class="mmpdb-modal__close">&times;</button>
                        <iframe class="mmpdb-modal__iframe" src="" frameborder="0"></iframe>
                    </div>
                </div>
            `;
            
            $('body').append(modalHTML);
        }
        bindEvents() {
            $(document).on('click', '.modal-link', (e) => {
                e.preventDefault();
                this.openModal($(e.currentTarget).attr('href'));
            });
            $('.mmpdb-modal__close').on('click', () => this.closeModal());
            $(document).on('keyup', (e) => {
                if (e.key === 'Escape') this.closeModal();
            });
            // Close on overlay click
            $('.mmpdb-modal').on('click', (e) => {
                if ($(e.target).hasClass('mmpdb-modal')) {
                    this.closeModal();
                }
            });
        }
        openModal(url) {
            $('.mmpdb-modal__iframe').attr('src', url);
            $('.mmpdb-modal').addClass('mmpdb-modal--active');
            $('body').addClass('modal-is-open');
        }
        closeModal() {
            $('.mmpdb-modal').removeClass('mmpdb-modal--active');
            $('body').removeClass('modal-is-open');
            $('.mmpdb-modal__iframe').attr('src', '');
        }
    }
    // Initialize on document ready
    $(document).ready(() => {
        window.mmpdbModal = new MMPDBModal();
    });
})(jQuery); 