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

var videoViewed = null;

var videoKeyboard = false; /* 2020/06/28 accessibility fix for video and keyboard */

/* video ended function */
function vidEnded(vid){
	//alert('video complete ' + $(vid).attr('id'));
  //20190917 desktop portrait fix
	//videoActivated = null
	var vidNum = Number($(vid).attr('id').split("_")[2])
	document.currentSection = vidNum;
	
	if(os == "Other" && !isMac){
		$("#video_poster_"+vidNum).css("display","block")
	}
	
	/*if(isMac){
		$('.video_poster').css('display','none');
	}*/
	
	//$('#inline_video_'+videoViewed).css('display','none');
	//alert('ended');
	/* mark video as complete */
    document.audioComplete = true;
	document.intComplete = true;
	isPageComplete(document.currentSection);
	
	var video= $('#video_player_'+vidNum);//.get(0);   
	video.load();
	
	setVideoStartBtn();
  //20190917 desktop portrait fix

}

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

function setVideoStartBtn(){
	$('.inline_video_element').unbind();
	$('.video_player').unbind();
	$('.video_player').bind('ended', function () {
		/* done playing */
		vidEnded(this);
    //20190917 desktop portrait fix
	});
	/* 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_link').unbind();
    
    $('.transcript_link').click(function(e){
       		//20190917 desktop portrait fix
		var vid = $(this).closest(".row").prev().find(".video_player")
				
        var sec = $(vid).attr("id").split("_")[2];
        openTranscript(sec)
        
    });
    
	$('.transcript_link').keypress(function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            $(this).trigger('click');
        }
    });
	
	
	
	
	$('.video_poster').click(function(e){
       		
		var vidNum = Number($(this).attr("id").split("_")[2])
		
		document.getElementById("video_player_"+vidNum).play();
		$("#video_player_"+vidNum).focus();
		$(this).css("display","none")
        
    });
    
	$('.video_poster').keypress(function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            $(this).trigger('click');
        }
    });
	
	
	/* 2020/06/28 accessibility fix for video and keyboard */
	$(".video_player").keydown(function() {
		videoKeyboard = true;
	});
						   
	$(".video_player").on("timeupdate", function () {
		if(videoKeyboard){
			$(this).attr("controls", "controls")
		}

	});
	
	$(".video_player").on("play", function () {
		var vidNum = Number($(this).attr("id").split("_")[2])
		
		$("#video_poster_"+vidNum).css("display","none")
	});
	
	/* 2020/06/28 accessibility fix for video and keyboard */
	
	
}
setVideoStartBtn();


function transcriptClick(){
    //20190917 desktop portrait fix
    /*document.audioComplete = true;
	document.intComplete = true;
	if(document.currentSection != null){
		isPageComplete(document.currentSection);
	}*/
}


function openTranscript(sec){
//20190917 desktop portrait fix
	document.audioComplete = true;
	document.intComplete = true;
	document.currentSection = sec;
	if(document.currentSection != null){
		isPageComplete(document.currentSection);
	}
//20190917 desktop portrait fix
    window.open('video/transcript.html?&sec=sec_'+sec,'name','resizable=1,scrollbars=1,width=815,height=800');
}

// 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);
}

/*
if(isiPhone || isiPad){
	$('.video_poster').css('display','none');
}*/
