/* functions to control video playback using a video player */

var videoViewed = null;

/* video ended function */
function vidEnded(vidSecNum){
	//alert('video complete');
	//videoActivated = null
	setVideoStartBtn();
	//$('#inline_video_'+videoViewed).css('display','none');
	//alert('ended');
	/* mark video as complete */
	
	// 20200701 number 1 is used by menu splash video so do call page complete when that video ends
	if(vidSecNum != 1){
		document.intComplete = true;
		isPageComplete(vidSecNum);
	}else{
		showSplashBtn();
		hideSplashVideo();
	}
	
	var video= $('#video_player_'+vidSecNum);//.get(0);   
	video.load();   
	/*video.addEventListener('ended',function(){
		  
	},false);*/

	
}

/* set click function to launch video */
var videoActivated;

var videoKeyboard = false;

function setVideoStartBtn(){
	$('.inline_video_element').unbind();
	$('.video_player').unbind();
	$('.video_player').bind('ended', function () {
		/* done playing */
		vidEnded($(this).attr('id').split('_')[2]);
	});
	/* pause playing video when starting another */
	$('.video_player').bind('play', function() {
		videoActivated = this;
		$('.video_player').each(function() {
			if(this != videoActivated) this.pause();
		});
	});
	/* accessibility control */
	$('.transcript_btn').keypress(function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            /*videoTranscript();*/
        }
    });
	
	/* 2020/06/21 accessibility fix for video and keyboard */
	$(".video_player").keydown(function() {
		videoKeyboard = true;
	});

	
	$(".video_player").on("timeupdate", function () {
		if(videoKeyboard){
			$(this).attr("controls", "controls")
		}

	});
	/* 2020/06/21 accessibility fix for video and keyboard */
}
setVideoStartBtn();

// Pause all videos
function pauseAllVideos(){
	$('.video_player').each(function() {
		this.pause();
	});
}
// Pause all videos
	
function createPlayerVar(varName,varContent)
{
	var scriptStr = "var "+varName+"= \""+varContent+"\""
	var node_scriptCode = document.createTextNode( scriptStr )
	var node_script = document.createElement("script");
	node_script.type = "text/javascript"
	node_script.appendChild(node_scriptCode);
	var node_head = document.getElementsByTagName("head")[0]
	node_head.appendChild(node_script);
}

/*$('.video_player').on('seeking', function(){
  // do something to stop seeking
  //alert('seeking');  
  return false;
});
*/

if(document.videoLocked){
	$('.video_player').each(function(index, element) {
		var video = document.getElementById($(this).attr('id'));
		/*var supposedCurrentTime = 0;
		video.addEventListener('timeupdate', function() {
		  if (!video.seeking) {
				supposedCurrentTime = video.currentTime;
		  }
		});
		// prevent user from seeking
		video.addEventListener('seeking', function() {
		  // guard agains infinite recursion:
		  // user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....
		  var delta = video.currentTime - supposedCurrentTime;
		  if (Math.abs(delta) > 0.01) {
			console.log("Seeking is disabled");
			video.currentTime = supposedCurrentTime;
		  }
		});
		// delete the following event handler if rewind is not required
		video.addEventListener('ended', function() {
		  // reset state in order to allow for rewind
			supposedCurrentTime = 0;
		});*/		
		var timeTracking = {
			watchedTime: 0,
			currentTime: 0
		};
		var lastUpdated = 'currentTime';

		video.addEventListener('timeupdate', function () {
			if (!video.seeking) {
				if (video.currentTime > timeTracking.watchedTime) {
					timeTracking.watchedTime = video.currentTime;
					lastUpdated = 'watchedTime';
				}
				/* tracking time updated after user rewinds */
				else {
					timeTracking.currentTime = video.currentTime;
					lastUpdated = 'currentTime';
				}
			}
		});
		/*  prevent user from seeking */
		video.addEventListener('seeking', function () {
			/* guard against infinite recursion: user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....*/
			var delta = video.currentTime - timeTracking.watchedTime;
			if (delta > 0) {
				video.pause();
				/* play back from where the user started seeking after rewind or without rewind */
				video.currentTime = timeTracking[lastUpdated];
				video.play();
			}
		});
	});
}

// 20200701 splash video functionality. 
function hideSplashVideo(){
	$(".continue_vid_link").css("visibility","hidden");
	$(".splash_video_holder").css("display","none");
	$(".menu_content").fadeIn();
	topic_height();
	pauseAllVideos();
	//markTopicsVisited();
	unlockTopic(0)
}

function showSplashVideo(){
	$(".splash_video_holder").fadeIn();
	$(".menu_content").css("display","none");
	$(".continue_vid_link").css("visibility","visible");
}

function showSplashBtn(){
	$(".continue_vid_link").css("visibility","visible");
}



/*var video1 = document.getElementById('video_player_16');

function videoPausePlayHandler(e) {
	if (e.type == 'playing') {
		//add controls
		console.log("playing video")
		video1.setAttribute("controls","controls");

		$("video::-webkit-media-controls-panel").css("display", "flex !important")
		$("video::-webkit-media-controls-panel").css("opacity", "1 !important")

	} else if (e.type == 'pause') {
	}
}

//Add event listeners
video1.addEventListener('playing', videoPausePlayHandler, false);
video1.addEventListener('pause', videoPausePlayHandler, false);*/

/*var vidElem = document.getElementById("video_player_16");

    vidElem.addEventListener('timeupdate', videoTimeUpdateHandler, false);

    function videoTimeUpdateHandler(e)
    {
        vidElem.setAttribute("controls","controls");
    }

*/
/*
$(".video_player").on("timeupdate", function () {
	
	$(this).attr("controls", "controls")
	
});*/

