/* functions for language selection */

function langInit() {
	
	/* 20221130 Update to language selection and bookmark functionality */
	/* store language selected in suspend_data instead of student_preference.language 2025 start */
	var langSel = parent.document.scormAPI.LMSGetValue("cmi.suspend_data").split('*')[0];
	/* store language selected in suspend_data instead of student_preference.language 2025 end */
	
	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"); 
	}
	
	/* store language selected in suspend_data instead of student_preference.language 2025 start */
	if(langSel != "" && parent.document.switchLang == false){
	/* store language selected in suspend_data instead of student_preference.language 2025 end */
		
		parent.document.inCourse = true; /* 20230127 Fixed issue with course completing when closing on lang screen (SCORM Cloud) */ 
		/* store language selected in suspend_data instead of student_preference.language 2025 start */
		parent.document.langSelected = langSel;
		/* store language selected in suspend_data instead of student_preference.language 2025 end */
		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) */ 
			/* store language selected in suspend_data instead of student_preference.language 2025 start */
			parent.document.langSelected = langAvail[0];
			var suspendData = parent.document.scormAPI.LMSGetValue("cmi.suspend_data").split('*')[1];
			if ((suspendData != '') && (typeof suspendData != 'undefined')) {
				suspendData = langAvail[0] + '*' + suspendData;
			}
			else {
				suspendData = langAvail[0] + '*';
			}
			parent.document.scormAPI.LMSSetValue("cmi.suspend_data",suspendData);
			/* store language selected in suspend_data instead of student_preference.language 2025 end */
			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) */ 
			/* 20221130 Update to language selection and bookmark functionality */
			/* store language selected in suspend_data instead of student_preference.language 2025 start */
			parent.document.langSelected = lang;
			var suspendData = parent.document.scormAPI.LMSGetValue("cmi.suspend_data").split('*')[1];
			if ((suspendData != '') && (typeof suspendData != 'undefined')) {
				suspendData = lang + '*' + suspendData;
			}
			else {
				suspendData = lang + '*';
			}
			parent.document.scormAPI.LMSSetValue("cmi.suspend_data",suspendData);
			/* store language selected in suspend_data instead of student_preference.language 2025 end */
			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);	
}

