/* functions for language selection */

function langInit() {
	
	/* 20221130 Update to language selection and bookmark functionality */
	var langSel = parent.document.scormAPI.LMSGetValue("cmi.student_preference.language");
	
	var status = parent.document.scormAPI.LMSGetValue("cmi.core.lesson_status"); /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
	
	if(status == "completed" || status == "passed"){ /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
	}else{
		parent.document.scormAPI.LMSSetValue("cmi.core.lesson_status","incomplete"); 
	}
	
	if(langSel != "" && parent.document.switchLang == false){
		
		parent.document.inCourse = true; /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
		window.location.href = eval("applicationPath_" + langSel);
	}else{

		var langAvail = langAvailable.split(",");
		if (langAvail.length == 1) {
			parent.document.inCourse = true; /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
			parent.document.scormAPI.LMSSetValue("cmi.student_preference.language",langAvail[0]); /* 20221130 Update to language selection and bookmark functionality */
			window.location.href = eval("applicationPath_" + langAvail[0]);
		}else {
			parent.document.inCourse = false; /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
			$('.langContent').css('display','block');

			for (var i=0; i < langAvail.length; i++) {
				$('#'+langAvail[i]).css('display','block');
			}
		}	

		$(".button").click(function(){

			lang = $(this).attr("id");
			parent.document.inCourse = true; /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
			parent.document.scormAPI.LMSSetValue("cmi.student_preference.language",lang); /* 20221130 Update to language selection and bookmark functionality */	

			gotoCourse(lang)
		});

		/* 20221107 ensure tabbing through languages occurs in a loop */
		if (langAvail.length > 1) {
			$('#'+langAvail[0]).on('keydown', function(e) {
				if ((e.which == 9) && (e.shiftKey)) {
					e.preventDefault();
					$('#'+langAvail[langAvail.length-1]).focus();
				}
			});
			$('#'+langAvail[langAvail.length-1]).on('keydown', function(e) {
				if ((e.which == 9) && (!e.shiftKey)) {
					e.preventDefault();
					$('#'+langAvail[0]).focus();
				}
			});	
		}	
		$('#'+langAvail[0]).focus();
	}
}	


function gotoCourse(lang){

 	var path = eval("applicationPath_" + lang)
	window.location.replace(path);	
}

