document.addEventListener('DOMContentLoaded', function() {

    // modal videos
    const videoModal = document.getElementById('videoModal');
    const modalIframe = document.getElementById('modalVideoIframe');
    const modalTitle = document.getElementById('videoModalLabel');
    
    if ( videoModal || modalIframe || modalTitle ) {
      // when modal open...
      videoModal.addEventListener('show.bs.modal', function (event) {
          const button = event.relatedTarget;
          const videoUrl = button.getAttribute('data-video-url');
          const videoTitle = button.getAttribute('data-video-title');
          const videoAllow = button.getAttribute('data-video-allow');
          
          // Add autoplay to URL
          const separator = videoUrl.includes('?') ? '&' : '?';
          const autoplayUrl = videoUrl + separator + 'autoplay=1';
          
          modalIframe.src = autoplayUrl;
          modalIframe.title = videoTitle;
          modalIframe.allow = videoAllow;
          modalTitle.textContent = videoTitle;
      });
      
      // When modal close, video stops.
      videoModal.addEventListener('hide.bs.modal', function () {
          modalIframe.src = '';
      });
    }

}); //end DMContentLoaded
