HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/anvil.komma.pro/resources/assets/js/site/youtubeHandler.js
/* ==========================================================================
    Youtube handler
 ========================================================================== */

var YoutubeHandler = {

    elementId : 'ytplayer',
    youtubeId : '',
    player: '',
    homeVideo: false,

    /**
     * 
     */
    init : function()
    {

        var youtubeElement = document.getElementById(YoutubeHandler.elementId);

        if(isset(youtubeElement)){

            YoutubeHandler.youtubeId = youtubeElement.getAttribute('data-ytlink');

            YoutubeHandler.initVideo();

            document.getElementById('trigger-video-button').addEventListener('click', function () {
                YoutubeHandler.homeVideo = true;
                YoutubeHandler.enableHomeVideoFormation();
            });
        }
    },
    
    /**
     * Check if external script is loaded
     * 
     */
    initVideo: function() {
        // See if YT variable exists
        if (typeof(YT) == 'undefined' || typeof(YT.Player) == 'undefined') {
            // Setup API ready function
            window.onYouTubePlayerAPIReady = function() {
                YoutubeHandler.loadPlayer();
            };
            // Load external script
            $.getScript('https://www.youtube.com/iframe_api');
        // If YT already exists load player
        } else {
            YoutubeHandler.loadPlayer();
        }
    },

    /**
     * Load Youtube player with parameters
     *
     */
    loadPlayer: function() {
        // Load player
        YoutubeHandler.player = new YT.Player(YoutubeHandler.elementId,{
            height: 200,
            width: 200,
            videoId: YoutubeHandler.youtubeId,
            host: 'https://www.youtube-nocookie.com',
            playerVars: {
                modestbranding: 0,
                showinfo: 0,
                rel: 0,
                disablekb: 1
            },
            events: {
            //     'onReady': YoutubeHandler.onReady,
                'onStateChange': YoutubeHandler.onStateChange
            }
        });
    },

    /**
     * Listener for Youtube state change
     *
     * @param state
     */
    onStateChange : function(state) {
        // Loop video
        if ((state.data === YT.PlayerState.ENDED) && YoutubeHandler.homeVideo) {
            // Do stuff
            YoutubeHandler.disableHomeVideoFormation();
        }
        if ((state.data === YT.PlayerState.PAUSED) && YoutubeHandler.homeVideo) {
            YoutubeHandler.preventPausedBufferBlink();
        }
    },

    preventPausedBufferBlink :function(){
        setTimeout(function () {
            if(YoutubeHandler.player.getPlayerState() === YT.PlayerState.PAUSED){
                YoutubeHandler.disableHomeVideoFormation();
            }
        }, 300);
    },

    /*
     * Slide block for video formation on the home page
     */
    enableHomeVideoFormation : function () {

        var homeIntroSection = document.querySelector('#home .home-intro');
        var homeIntroVideoSection = document.querySelector('#home .home-intro .video-block');
        var homeIntroVideoHolder = document.querySelector('#home .home-intro .video-block .video-overlay');

        if(window.innerWidth >= 1200){

            homeIntroSection.classList.add('video-formation');

            setTimeout(function () {
                homeIntroVideoSection.classList.add('video-formation-size');
            }, 600);

            setTimeout(function () {
                homeIntroSection.classList.add('video-formation-max-height-size');
            }, 1200);

            setTimeout(function () {
                YoutubeHandler.player.playVideo();
            }, 1500);

            setTimeout(function () {
                homeIntroVideoHolder.classList.add('show-video');
            }, 1800);

        }
        else{
            homeIntroSection.classList.add('video-formation');
            YoutubeHandler.player.playVideo();
        }

    },

    disableHomeVideoFormation : function () {

        var homeIntroSection = document.querySelector('#home .home-intro');
        var homeIntroVideoSection = document.querySelector('#home .home-intro .video-block');
        var homeIntroVideoHolder = document.querySelector('#home .home-intro .video-block .video-overlay');

        if(window.innerWidth >= 1200) {

            homeIntroVideoHolder.classList.remove('show-video');

            setTimeout(function () {
                homeIntroSection.classList.add('keep-display-settings');
                homeIntroSection.classList.remove('video-formation-max-height-size');
            }, 600);

            setTimeout(function () {
                homeIntroSection.classList.remove('keep-display-settings');
                homeIntroVideoSection.classList.remove('video-formation-size');
            }, 1200);

            setTimeout(function () {
                homeIntroSection.classList.remove('video-formation');
            }, 1800);

        }
        else{
            homeIntroSection.classList.remove('video-formation');
        }

    },

    /**
     * When player is ready to play
     */
    // onReady : function() {
    //
    //     // Show video
    //     setTimeout(function(){ $('#' + YoutubeHandler.elementId).stop().animate({ opacity: 1 },1000) },800);
    //
    //     // If not on tablet or mobile, play on high quality
    //     window.player.mute();
    //     window.player.playVideo();
    //     window.player.setPlaybackQuality('hd1080');
    // },

};

YoutubeHandler.init();