var youtubeApiAdded = false;
var youtubeApiAvailable = false;
function addYoutubeApi() {
    if (youtubeApiAvailable) {
        $('.js-listenOnYoutubeApi').trigger('youtubeIframeAPIReady');
    }
    if (!youtubeApiAdded) {
        youtubeApiAdded = true;
        var tag = document.createElement('script');
        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    }

}

function onYouTubeIframeAPIReady() {
    youtubeApiAvailable = true;
    $('.js-listenOnYoutubeApi').trigger('youtubeIframeAPIReady');
}

function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}

$('body').on('close.lightbox', function() {
    var ytPlayer = $('.lightbox-content').find('.js-listenOnYoutubeApi').remove();
})

$('body').on('youtubeIframeAPIReady', '.js-listenOnYoutubeApi', function(e) {
    var videoId = youtube_parser($(this).attr('data-src'));
    var $el = $(this);
    var elId = $el.attr('id');
    var player = new YT.Player(elId, {

        host: 'https://www.youtube-nocookie.com',
        wmode: 'transparent',
        playerVars:{
            wmode: 'transparent',
            showinfo:0,
            autohide:1,
        },
        height: '100%',
        width: '100%',
        videoId: videoId,
        events: {
            'onReady': function(event){
                event.target.playVideo();
            },
            'onStateChange': function(e){
                if (e.data == 0){
                    $('.lightbox-mask').trigger('click');
                }
            }
        }
    });
    $(this).data('test', 'noep');
    $(this).attr('data-blub', 'noep');
    $(this).data('ytplayer', player);
})

$(document).on('click', '.js-modalbox', $.proxy(function (e) {
    e.preventDefault();
    if (window.lightbox.clicked === false) {
        window.lightbox.clicked = true;
        $(e.currentTarget).bmLightBox();
    }
}, this));


$(document).on('click', '.js-modalVideo', $.proxy(function(e) {
    e.preventDefault();
    var src = $(this).data('src');
    src = src.replace('youtube.com', 'youtube-nocookie.com');

    //$(this).attr('href', false);
    var settings = {
        content: '<div class="embed-responsive embed-responsive-16by9"><div id="test1234" data-src="'+src+'" class="js-listenOnYoutubeApi img-responsive embed-responsive-item"></div></div>'
    };
    $(this).bmLightBox(settings);
    if (window.lightbox.clicked === false) {
        window.lightbox.clicked = true;
        //window.lightbox.updateSettings(settings);
        // window.lightbox.openBox();
    }
    addYoutubeApi();
}))

$('body').on('openLightbox', function(){
    $('.lightbox-content').on('scroll', function(){
        $(window).trigger('scroll');
    });
});
$('body').on('openLightbox', function(){
    $(window).trigger('content.changed');

});
if ($(location.hash).length) {
    if ($(location.hash).hasClass('js-modalbox')) {
        $(location.hash).bmLightBox();
    }
}

