let params = {"auto_play": false, "embed": false, "facebook": false, "height": "100%", "loop": false, "responsive": true, "title": true, "twitter": false, "volume": 80, "width": "100%"};
let videos_current = 0; let videos_list = [{"url":"https:\/\/video.proxi.tools\/videoentreprise\/other\/BE-C00084818\/BE-C00084818_2-1_1080.mp4","title":"EDS Wesley Bleys","type":"corporate","splash":"https:\/\/medias.bizbookchannel.be\/BE-C00084818_48922_thumb_img008-big.webp"}]; let socials = false;
let style_controls = document.createElement("link"); style_controls.rel = "stylesheet"; style_controls.href = "https://videoplayer.proxi.tools/styles/shaka.min.css"; document.body.appendChild(style_controls);
let style_youtube = document.createElement("link"); style_youtube.rel = "stylesheet"; style_youtube.href = "https://videoplayer.proxi.tools/styles/youtube-theme.min.css"; document.body.appendChild(style_youtube);
let style_custom = document.createElement("link"); style_custom.rel = "stylesheet"; style_custom.href = "https://videoplayer.proxi.tools/styles/script.min.css"; document.body.appendChild(style_custom);
let video, ui, controls, player, config, state;
function initApp() { shaka.polyfill.installAll(); if ( shaka.Player.isBrowserSupported() ) { initPlayer(); } else { console.error("Browser not supported!"); }}

            async function initPlayer()
            {
                // TITLE
                if ( params.title ) { shaka.ui.VideoTitle = class extends shaka.ui.Element { constructor(parent, controls) { super(parent, controls); this.button_ = document.createElement("div"); this.button_.textContent = videos_list[videos_current].title; this.button_.className = "video-title"; this.parent.appendChild(this.button_); }}; shaka.ui.VideoTitle.Factory = class { create(rootElement, controls) { return new shaka.ui.VideoTitle(rootElement, controls); } }; shaka.ui.Controls.registerElement("video_title", new shaka.ui.VideoTitle.Factory()); }

                // Facebook
                if ( params.facebook )
                {
                    shaka.ui.Facebook = class extends shaka.ui.Element { constructor(parent, controls) {
                        super(parent, controls);
                        this.button_ = document.createElement("div"); this.button_.innerHTML = "<img src=https://videoplayer.proxi.tools/images/button-facebook.png>"; this.button_.className = "shaka-small-social-button socials-images"; this.parent.appendChild(this.button_);
                        this.eventManager.listen(this.button_, "click", () => { window.open("//www.facebook.com/sharer.php?t="+videos_list[videos_current].title+"&u=", "new", ""); });
                    } };
                    shaka.ui.Facebook.Factory = class { create(rootElement, controls) { return new shaka.ui.Facebook(rootElement, controls); } };
                    shaka.ui.Controls.registerElement("facebook", new shaka.ui.Facebook.Factory());
                }            
                // Twitter
                if ( params.twitter )
                {
                    shaka.ui.Twitter = class extends shaka.ui.Element { constructor(parent, controls) {
                        super(parent, controls);
                        this.button_ = document.createElement("div"); this.button_.innerHTML = "<img src=https://videoplayer.proxi.tools/images/button-twitter.png>"; this.button_.className = "shaka-small-social-button socials-images"; this.parent.appendChild(this.button_);
                        this.eventManager.listen(this.button_, "click", () => { window.open("//twitter.com/share?text="+videos_list[videos_current].title+"&url=", "new", ""); });
                    } };
                    shaka.ui.Twitter.Factory = class { create(rootElement, controls) { return new shaka.ui.Twitter(rootElement, controls); } };
                    shaka.ui.Controls.registerElement("twitter", new shaka.ui.Twitter.Factory());
                }            

                // BUTTONS PREVIOUS + NEXT
                if ( videos_list.length > 1 )
                {
                    shaka.ui.NextVideo = class extends shaka.ui.Element { constructor(parent, controls) {
                        super(parent, controls);
                        this.button_ = document.createElement("button"); this.button_.textContent = "skip_next"; this.button_.className = "shaka-small-play-button material-icons-round"; this.parent.appendChild(this.button_);
                        this.eventManager.listen(this.button_, "click", () => { loadNext(); });
                    } };
                    shaka.ui.NextVideo.Factory = class { create(rootElement, controls) { return new shaka.ui.NextVideo(rootElement, controls); } };
                    shaka.ui.Controls.registerElement("next_video", new shaka.ui.NextVideo.Factory());            
                                
                    shaka.ui.PreviousVideo = class extends shaka.ui.Element { constructor(parent, controls) {
                        super(parent, controls);
                        this.button_ = document.createElement("button"); this.button_.textContent = "skip_previous"; this.button_.className = "shaka-small-play-button material-icons-round"; this.parent.appendChild(this.button_);
                        this.eventManager.listen(this.button_, "click", () => { loadPrevious(); });
                    } };
                    shaka.ui.PreviousVideo.Factory = class { create(rootElement, controls) { return new shaka.ui.PreviousVideo(rootElement, controls); } };
                    shaka.ui.Controls.registerElement("previous_video", new shaka.ui.PreviousVideo.Factory());            
                }
                
                // Config
                video = document.getElementById("video");
                ui = video["ui"];
                controls = ui.getControls();
                player = controls.getPlayer();
                localplayer = ui.getControls().getLocalPlayer();
                video.volume = params.auto_play ? 0 : params.volume/100;  
                
                config = {controlPanelElements: [], overflowMenuButtons: ["quality", "picture_in_picture", "playback_rate"], seekBarColors: {base: "rgba(255,255,255,.2)", buffered: "rgba(255,255,255,.4)", played: "rgb(255,0,0)"}};
                if ( videos_list.length > 1 ) { config.controlPanelElements.push("previous_video"); }
                config.controlPanelElements.push("play_pause");
                if ( videos_list.length > 1 ) { config.controlPanelElements.push("next_video"); }
                config.controlPanelElements.push("mute");
                config.controlPanelElements.push("volume");
                config.controlPanelElements.push("time_and_duration");
                if ( params.title ) { config.controlPanelElements.push("video_title"); }
                config.controlPanelElements.push("spacer");
                if ( params.facebook ) { config.controlPanelElements.push("facebook"); }
                if ( params.twitter ) { config.controlPanelElements.push("twitter"); }
                config.controlPanelElements.push("fullscreen");
                config.controlPanelElements.push("overflow_menu");
                ui.configure(config);

                window.player = player;
                window.ui = ui;
                window.video = video;
                
                // Listeners
                player.addEventListener("error", onPlayerErrorEvent);
                controls.addEventListener("error", onUIErrorEvent);
                video.addEventListener("playing", onPlaying);
                video.addEventListener("pause", onPause);
                if ( params.loop ) { video.addEventListener("ended", onEnded); }
                
                // Functions
                function onPlaying() { state = "play"; }
                function onPause() {  }
                function onEnded() { if ( videos_list.length > 1 ) { loadNext(); } else { video.play(); } }
                
                function loadNext()
                {
                    if ( videos_current == videos_list.length-1 ) { videos_current = 0; } else { videos_current += 1; }
                    if ( videos_list[videos_current].type != "square" ) { document.querySelector("#video_container").style.background = "url("+videos_list[videos_current].splash+")"; } else { document.querySelector("#video_container").style.backgroundColor = "#000000"; }
                    this.player.load(videos_list[videos_current].url).then(function(){ if ( state == "play" ) { video.play(); } });
                    if ( params.title ) { document.querySelector(".video-title").innerHTML = videos_list[videos_current].title; }
                }
                function loadPrevious()
                {
                    if ( videos_current == 0 ) { videos_current = videos_list.length-1; } else { videos_current -= 1; }
                    if ( videos_list[videos_current].type != "square" ) { document.querySelector("#video_container").style.background = "url("+videos_list[videos_current].splash+")"; } else { document.querySelector("#video_container").style.backgroundColor = "#000000"; }
                    this.player.load(videos_list[videos_current].url).then(function(){ if ( state == "play" ) { video.play(); } });
                    if ( params.title ) { document.querySelector(".video-title").innerHTML = videos_list[videos_current].title; }
                }
                
                document.querySelector(".shaka-overflow-menu-button").innerHTML = "settings";
                
                // Launch player
                try { await player.load(videos_list[videos_current].url).then(function(){ if ( params.auto_play ) { video.play(); } }); } catch (e) { onPlayerError(e); }
            }
        
function onPlayerErrorEvent(errorEvent) { onPlayerError(event.detail); }
function onPlayerError(error) { console.error("Error code", error.code, "object", error); }
function onUIErrorEvent(errorEvent) { onPlayerError(event.detail); }
function initFailed(errorEvent) { console.error("Unable to load the UI library!"); }
document.write('<div id="video_container" class="youtube-theme" style="background-color: #000000; background-repeat: no-repeat; background-size: cover; background-position: center; width: '+params.width+'; height: '+params.height+';" data-shaka-player-container><video id="video" style="width:100%;height:100%" data-shaka-player ></video></div>');
if ( videos_list[videos_current].type == "square" || params.embed ) { document.querySelector("#video_container").style.backgroundColor = "#000000"; } else { document.querySelector("#video_container").style.background = "url("+videos_list[videos_current].splash+")"; }
let script_ui = document.createElement("script"); script_ui.src = "https://videoplayer.proxi.tools/scripts/shaka.min.js"; document.body.appendChild(script_ui);
document.addEventListener("shaka-ui-loaded", initApp);
