Explode active video

by stevehauk

HTML

<div  id="loading"><b><br>Loading ...</b></div>
<div id="visibility_wrapper" style="display: none">
<article id="0" data-ytvideocode="dykmb3dHLU4">Please wait</article>
<article id="1" data-ytvideocode="Ki7AuEen8Rw">Please wait</article>
<article id="2" data-ytvideocode="amgybZEP-TI">Please wait</article>
<article id="3" data-ytvideocode="0DTtyHV3R2Q">Please wait</article>
<article id="4" data-ytvideocode="He6SdbpiCCs">Please wait</article>
<article id="5" data-ytvideocode="4vuW6tQ0218">Please wait</article>
<article id="6" data-ytvideocode="z0yBf1JKTw8">Please wait</article>
<article id="7" data-ytvideocode="5ZLtcTZP2js">Please wait</article>
</div>

JavaScript

var players = [];
var arts = [];
var VideoCount;
var LoadedSoFar=0;

InitialiseYoutubeAPI();

function InitialiseYoutubeAPI()
/*This function should be called before attempting to play any Video
BUT *NOT* again*/
{
	/*Load in the Youtube API - must do this exactly once per page load*/
	var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
	//....and await onYouTubeIframeAPIReady()
}

function onYouTubeIframeAPIReady() 
{ 
	/*Optional: If you want to take some action when the API is reasdy, write a onAllInitialised() function to do it*/
	if (typeof onAllInitialised === 'function') { onAllInitialised();}
	
	arts = document.getElementsByTagName("article"); /*all the <div> elements*/
	VideoCount = arts.length; /*keep a note of the total*/
	if(arts.length>0) 
	{
		InitVideo(arts.item(arts.length-1), arts.length-1);
	}

}

function InitVideo(adiv, i)
/*Called exactly once for each video prior to attempting to play it 
Each player sits on its own panel/fold*/
{ 
	document.getElementById("loading").innerHTML = "<br>Loading "+(LoadedSoFar+1)+" of "+VideoCount;
	LoadedSoFar = LoadedSoFar+1;

	players[i] = new YT.Player(adiv.id, 
	{
        videoId: adiv.getAttribute("data-ytvideocode"),
			width: 300,
			height: 169,
           events: {
                  'onReady': onPlayerReady,
                  'onStateChange': onPlayerStateChange
                   }
        });
}
function onPlayerReady(event)
{ 
	if(arts.length>0) 
	{
		InitVideo(arts.item(arts.length-1), arts.length-1);
	}
	else
	{
		document.getElementById("visibility_wrapper").style.display = "block";
		document.getElementById("loading").style.display = "none";		
	}
 }
 
function onPlayerStateChange(event)
{
	if( event.data == YT.PlayerState.PLAYING )
	{
		{
			/*stop all other...