/* custom branching: common topic, a branching topic with branching screentype, two branches, no assessement 2023 */
/* only require action on selection of the branching buttons */
$(".branch_btn").click(function(e){

	/* enable the Home buttons */
	$('.home').css("opacity","1")
	$('.home').css("pointer-events","auto")
	$('#home_nav').css("opacity","1");
	$('#home_nav').css("pointer-events","auto")

	/* assign the branch for storing in the LMS */
	var branchNum = Number($(this).attr("id").split(" ")[0].split("_")[2]);

	document.userBranch = branchNum;
	
	/* update the home page menu */
	$(".menu_grid").css("display","block");	
	
	$(".branch_item").css("display","none");
	
	$(".branch_item_" + document.userBranch).css("display","none");
	
	$(".branch_item_" + document.userBranch + " .overlay").eq(0).css('display', 'none');
	
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('src', 'images/interface/topic_launch.png');			
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('title', localiseString('topic_start'));
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('alt', localiseString('topic_start'));

	/* go to the branch */
	var sec_saver = currentSec;
	var goTopic = Number($(this).attr("data-gotopic")) - 1;
	
	var secNum = Number($(this).attr("class").split(" ")[1].split("_")[2]);
	var btnNum = Number($(this).attr("class").split(" ")[1].split("_")[3])
		
	currentSec = goTopic;
	
	startedTopic(currentSec)
	op = 'left'
	nav = true
	cloneOne("right_nav", sec_saver, nav, op);
	
	/* update the burger menu */
	$(".burger_select.branch_item_" + document.userBranch + ":eq(0) .overlay").css('display', 'none');

	$('.burger_select.branch_item_' + document.userBranch + ':eq(0) img').attr('src', 'images/interface/burger_unlocked.png');
	$('.burger_select.branch_item_' + document.userBranch + ':eq(0) img').attr('title', localiseString('topic_start'));
	$('.burger_select.branch_item_' + document.userBranch + ':eq(0) img').attr('alt', localiseString('topic_start'));
	
	/* store the branch in the LMS */
	recordPercentComplete(1, document.subsectionCount, false);
	
});

$('.branch_btn').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});

/* custom branching */
/*$(".recap_btn").click(function(e){
       		
	var vidNum = Number($(this).attr("id").split("_")[2]);
	
	$(".recap_video").css("display","none");
	
	$(".recap_video_player").each(function() {
		this.pause();
	});
	
	$("#recap_video_holder_"+vidNum).css("display","block");
	
	$(".recap_video_holder").css("display","block")
	$('.recap_video_holder:visible').css({
		'position' : 'absolute',
		'top' : $(window.parent).height()/2,
		'margin-top' : -$('.recap_video_holder:visible').outerHeight()/2
	});
	
	$('.recap_video:visible').css({
		'position' : 'absolute',
		'top' : $(window.parent).height()/2,
		'margin-top' : -$('.recap_video:visible').outerHeight()/2,
		'left' : $(window.parent).width()/2,
		'margin-left' : -$('.recap_video:visible').outerWidth()/2
	});
	
	document.getElementById("recap_video_"+vidNum).play();
	
	
});

$('.recap_btn').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	


$(".recap_close").click(function(e){
	
	$(".recap_video").css("display","none");
	
	$(".recap_video_player").each(function() {
		this.pause();
	});
	
	$(".recap_video_holder").css("display","none")
	
});

$('.recap_close').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	

$(".recap_continue").click(function(e){
	
	$(".branch_grid").css("display","block");
	$(".recap_grid").css("display","none");
	
});

$('.recap_continue').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	


$(".branch_btn").click(function(e){
       		
	var branchNum = Number($(this).attr("id").split("_")[2]);

	document.userBranch = branchNum;
	
	$(".branch_continue").css("display","block")
	
});

$('.branch_btn').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	

$(".branch_continue").click(function(e){
	
	$(".menu_grid").css("display","block");
	$(".branch_grid").css("display","none");
	$(".branch_item").css("display","none");
	$(".branch_item_" + document.userBranch).css("display","block");
	
	$(".branch_item_" + document.userBranch + " .overlay").eq(0).css('display', 'none');
	
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('src', 'images/interface/topic_launch.png');			
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('title', localiseString('topic_start'));
	$('.branch_item_' + document.userBranch + ' .select_bottom:eq(0) img').attr('alt', localiseString('topic_start'));
	
	
	recordPercentComplete(1, document.subsectionCount, false);
	
});

$('.branch_continue').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	


$(".candidate_btn").click(function(e){
	
	var candidateNum = Number($(this).attr("id").split("_")[3]);
		
	var sec_saver = currentSec;
	
	currentSec = currentSec + candidateNum;
	
	startedTopic(currentSec)
	op = 'left'
	nav = true
	cloneOne("right_nav", sec_saver, nav, op)
	
});

$('.candidate_btn').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});	

$(".back_to_branch").click(function(e){
	
	
	$("#home_nav").trigger("click");
	branchDone();
	
	$(".menu_grid").css("display","none");
	$(".branch_grid").css("display","block");
});

$('.back_to_branch').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});*/

/*

	branchDone function
	Functioan called by button with "back_to_branch" class to
	caluclate if branch has been completed and mark current branch button
	complete

*/

/*function branchDone(){
	
	var tNum = 0;
	
	var bArr = [];
	
	$(".topic_select").each(function(index,element){
		tNum++;
	});
	
	for(var i = 1; i<= document.numberOfBranches; i++){
		
		var tempArr = []
	
		$(".branch_item_"+i).each(function(index,element){

			if($(this).index(".topic_select") != -1){			
				tempArr.push($(this).index(".topic_select"))
			}

		});
		bArr[i-1] = tempArr;
	}
		
	var progArr = document.sectionCompleted.split('');
	
	
	for(var i = 0; i != document.numberOfBranches; i++){
		
		var bCnt = 0;
		
		for(var j = 0; j != bArr[i].length; j++){
			
			if(progArr[bArr[i][j]] == 2){
				bCnt++;
			}
			
		}
		
		if(bCnt == bArr[i].length){			
			//$("#branch_btn_"+(i+1)).css("background-color","green");
			
			$("#branch_btn_"+(i+1)).addClass("branchComplete");
		}
	}
	
}

$(".goto_topic_btn").click(function(e){
	
	var className = $(this).attr("class").split(" ")[3];
	var goto = className.split("_")[0];
	var num = Number(className.split("_")[2]);
	var sec_saver = currentSec;
	
	if(goto == "prev"){
		currentSec = currentSec - num;
	}else if(goto == "next"){
		currentSec = currentSec + num;
	}
	
	startedTopic(currentSec)
	op = 'left'
	nav = true
	cloneOne("right_nav", sec_saver, nav, op);

});

$('.goto_topic_btn').keypress(function (e) {
	if (e.keyCode == 13 || e.keyCode == 32) {

		$(this).trigger('click');
	}		
});*/


/*

	endInterview function called from topic_nav.js
	this calculates if candidate before or after has been completed
	and shows relevant button to show next or previous candidate or jump to next topic/section

*/

/*function endInterview(n){
	

	var tStart, tEnd;
	
	
	if($("#sec_"+n).next(".section").hasClass("secStart")){
		
		tEnd = $("#sec_"+(n)).prevAll(".breaker").index(".section");
		tStart = $("#sec_"+(tEnd)).prevAll(".breaker").index(".section");
		
		var pageCnt = 0;
		var compCnt = 0;

		for(var i = tStart; i!= tEnd; i++){

			pageCnt++;
			if(document.trackingArr[i] == 1){
				compCnt++;
			}

		}

		if(compCnt == pageCnt){
			$("#sec_" + n + " .next_topic_1").css("display","block");
			
		}else if(compCnt == 0){
			$("#sec_" + n + " .prev_topic_1").css("display","block");
		}
				
	}else{
		
		tStart = n;
		tEnd = $("#sec_"+(n+1)).nextAll(".breaker").index(".section");
		
		var pageCnt = 0;
		var compCnt = 0;

		for(var i = tStart; i!= tEnd; i++){

			pageCnt++;
			if(document.trackingArr[i] == 1){
				compCnt++;
			}

		}

		if(compCnt == pageCnt){
			$("#sec_" + n + " .next_topic_2").css("display","block");
			
		}else if(compCnt == 0){
			$("#sec_" + n + " .next_topic_1").css("display","block");
		}
	}

	
}*/
