(function($) { $(window).on('load', function() { //returns the youtube embed format url from a normal link to a youtube video const embedYoutubeVideo = (link) => { let index = link.lastIndexOf("=") > -1 ? link.lastIndexOf("=") : link.lastIndexOf("/"); let videoId = link.substring(index + 1); return "https://www.youtube.com/embed/"+ videoId +"?autoplay=0&start=0&rel=0"; } /* --------------- */ /* pop up de video */ /* --------------- */ $('.views-field-field-cta-video').each(function(i){ var src = $(this).find('a').length > 0 ? embedYoutubeVideo($(this).find('a').attr('href')) : $(this).find('iframe').attr('src'); $(this).attr('data-src', src); if($('myModal' + i).length == 0){ $(this).parent().find('.views-field-field-texto-video.video-btn').after('
'); } }); // Gets the video src from the data-src on each button var videoSrc; $('.video-btn').click(function() { $('#header').css("z-index", "9998"); var modal = $(this).next('.video.modal'); var iframe = modal.find('iframe'); if (iframe.length == 0) { videoSrc = modal.data('src'); modal.find('.embed-responsive-16by9').html(''); } modal.addClass('show'); }); // when the modal is opened autoplay it $('.video.modal').on('shown', function (e) { $('#header').css("z-index", "9998"); // set the video src to autoplay and not to show related video. Youtube related video is like a box of chocolates... you never know what you're gonna get $(this).find(".embed-responsive-item").attr('src',videoSrc + "?autoplay=1&modestbranding=1&showinfo=0" ); }) // stop playing the youtube video when I close the modal $('.video.modal').on('hide', function (e) { $('#header').css("z-index", "9999"); if(videoSrc.includes("autoplay")){ var array = videoSrc.split("?autoplay"); videoSrc = array[0]; } $(this).next(".embed-responsive").find('iframe').attr('src', videoSrc); }) $('.video.modal .close').click(function() { $(this).closest('.video.modal').removeClass('show'); $('#header').css("z-index", "9999"); videoSrc = $(this).next(".embed-responsive").find('iframe').attr('src'); if(videoSrc.includes("autoplay")){ var array = videoSrc.split("?autoplay"); videoSrc = array[0]; } $(this).next(".embed-responsive").find('iframe').attr('src', videoSrc); }); /* --------------- */ /* --------------- */ /* --------------- */ }); })(jQuery);