{% if youtubeVideoId is defined and youtubeVideoId %}
<script>
$(function () {
const tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
});
{% if videoInsertType is defined and videoInsertType == constant('Plugin\\ApgVideoInsert42\\Domain\\VideoInsertType::SLIDE') %}
$(function () {
$(".item_visual").on("beforeChange", function (event, slick) {
slick = $(slick.$slider);
playPauseVideo(slick, "pause");
});
$(".item_visual").on("afterChange", function (event, slick) {
slick = $(slick.$slider);
playPauseVideo(slick, "play");
});
$(".item_visual").on('init', function (slick) {
$('.slick-dots .apg_video_youtube_player_image').show();
});
});
{% endif %}
let player;
function onYouTubeIframeAPIReady() {
$('.apg_video_youtube_player_image').hide();
$('.slick-dots .apg_video_youtube_player_image').show();
{% if videoInsertType is defined and videoInsertType == constant('Plugin\\ApgVideoInsert42\\Domain\\VideoInsertType::SLIDE') %}
let ifram_height = $('.apg_video_youtube_player_image:first').height();
if (!ifram_height || ifram_height < 360) {
if (window.matchMedia('(max-width: 767px)').matches) {
ifram_height = '360';
} else {
ifram_height = '480';
}
}
{% else %}
ifram_height = '480';
if (window.matchMedia('(max-width: 767px)').matches) {
ifram_height = '360';
}
{% endif %}
$('.apg_video_youtube').height(ifram_height);
let targetDom = null;
if($('#apg_video_youtube_player').length > 0){
targetDom = $('#apg_video_youtube_player')[0]
} else if($('.apg_video_youtube_player').length > 0) {
targetDom = $('.apg_video_youtube_player')[0]
}
if(targetDom !== null){
player = new YT.Player(targetDom, {
height: ifram_height,
width: '1000',
videoId: '{{ youtubeVideoId }}',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
},
playerVars: {}
});
}
}
function onPlayerReady(event) {
{% if videoInsertType is defined and videoInsertType == constant('Plugin\\ApgVideoInsert42\\Domain\\VideoInsertType::SLIDE') %}
event.target.playVideo();
{% endif %}
event.target.mute();
}
function onPlayerStateChange(event) {
}
function playVideo(time) {
player.seekTo(time);
player.playVideo();
}
function stopVideo() {
player.stopVideo();
}
function playPauseVideo(slick, control) {
const currentSlide = slick.find(".slick-active");
const player = currentSlide.find("iframe").get(0);
switch (control) {
case "play":
if (player) {
playVideo(0);
}
break;
case "pause":
if (player) {
stopVideo();
}
break;
}
}
</script>
{% endif %}