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

	document.userBranch = branchNum;
	
		
	recordPercentComplete(1, document.subsectionCount, false);
	
	
	$(".branch_item").css("display","none");
	
	$(".branch_item_" + document.userBranch).css("display","block");
	
	
	hideBranchScreen();
	
	$(".topic_select_even").last().addClass("topic_select_first");
	
});

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

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


function hideBranchScreen(){
	$(".branch_screen").css("display","none");
	$(".menu_content").fadeIn();
	topic_height();
	pauseAllVideos();
	unlockTopic(0);
	
	

}

function showBranches(){
	
	$(".branch_screen").css("display","block");
				
	$(".splash_video_holder").css("display","none");
	$(".menu_content").css("display","none");
	$(".continue_vid_link").css("visibility","hidden");
	
	updateBranch();
	
	//$("#menu_h1").focus();
	setTimeout(function(){
	//document.getElementById("menu_h1").focus();
		removeMenuTabIndex();
		setMenuTabIndex();
		$("#branch_leadin").focus();
		},300)
}

function endSharedContent(){

	if(document.hasBranches){
		if(document.userBranch != 0 && document.userBranch <= document.numberOfBranches){
						
			var endShared = $(".end_shared").index(".section");
			var lastTopic = document.topicsCount;
			
			$(".branch_item").css("display","none");
			$(".branch_item_" + document.userBranch).css("display","block");
			assignAltNumbering();
			if(Number(document.trackingArr[endShared]) == 1){
				$(".branch_item_" + document.userBranch + " .overlay").eq(0).css('display', 'none');
				
				if($('.branch_item_' + document.userBranch + ' .topic_duration').eq(0).html() != localiseString('topic_complete')){				
					
					$('.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'));
				}
			}
			
			endBranch();
		}else{
			var endShared = $(".end_shared").index(".section");
			var lastTopic = document.topicsCount;
			if(Number(document.trackingArr[endShared]) == 1){
				$('.topic_select .overlay').eq(lastTopic).css('display', 'none');

				if($('.topic_select:eq(' + lastTopic + ') .topic_duration').eq(0).html() != localiseString('topic_complete')){					
					$('.select_bottom:eq('+lastTopic+') img').attr('title', localiseString('topic_start'));
					$('.select_bottom:eq('+lastTopic+') img').attr('alt', localiseString('topic_start'));
				}
			}
			
		}
	}
	
}

function endBranch(n){
	
	var lastTopic = document.topicsCount;
	
	if(Number(document.trackingArr[n-1]) == 1){	
		$('.topic_select .overlay').eq(lastTopic).css('display', 'none')
		$('.select_bottom:eq('+lastTopic+') img').attr('title', localiseString('topic_start'));
		$('.select_bottom:eq('+lastTopic+') img').attr('alt', localiseString('topic_start'));
		
	}
	
	if(n == undefined){
		
		if($('.branch_item_' + document.userBranch + ' .topic_duration').eq(0).html() == localiseString('topic_complete')){
			
			$('.topic_select .overlay').eq(lastTopic).css('display', 'none');
			
			if($('.topic_select:eq(' + lastTopic + ') .topic_duration').eq(0).html() != localiseString('topic_complete')){				
				$('.select_bottom:eq('+lastTopic+') img').attr('title', localiseString('topic_start'));
				$('.select_bottom:eq('+lastTopic+') img').attr('alt', localiseString('topic_start'));
			}
			
		}
		
	}
	
}

function updateBranch(){
	
	for(var i = 0; i != document.numberOfBranches; i++){
		
		if($('.branch_item_' + (i+1) + ' .topic_duration').eq(0).html() == localiseString('topic_complete')){
			
			var imgEle, img, ext
			$('#branch_btn_'+(i+1)).find('img').each(function(index, element) {
				imgEle = $(this);
				img = $(this).attr('src').split('.')[0];
				ext = $(this).attr('src').split('.')[1];
			});

			if(img.indexOf('_complete') == -1){
				$(imgEle).attr('src', img + '_complete.' + ext);	
			}	
			
		}
		
	}
	
}

/* ensure branch completion is reset when post-test is failed maximum times and course is reset 062122 */
function resetBranch(){
	
	for(var i = 0; i != document.numberOfBranches; i++){
		
		var imgEle, img, ext
		$('#branch_btn_'+(i+1)).find('img').each(function(index, element) {
			imgEle = $(this);
			img = $(this).attr('src').split('_')[0] + '_' + $(this).attr('src').split('_')[1] + '_' +  (i+1);
			ext = $(this).attr('src').split('.')[1];
		});
		$(imgEle).attr('src', img + '.' + ext);
	}
	
}

function noBranch(){
	
	document.userBranch = document.numberOfBranches + 1;
	
	recordPercentComplete(1, document.subsectionCount, false);
		
	$(".branch_item").css("display","none");
		
	hideBranchScreen();
	$(".topic_select_even").last().removeClass("topic_select_first");
	assignAltNumbering();
}

function assignAltNumbering(){
	// mjb
	var altTopicNum = 0;
	$('.topic_index').find('.topic_select').each(function(index, element) {
		if ($(this).parent().css('display') == 'block'){
			altTopicNum ++;
			//alert(index);
			$(this).children('.heading_area_alt').text(altTopicNum);
		}
    });
}