/* functions to handle preliminary content tasks such as assigning responsive background images and setting up the assessment */
$( document ).ready(function() {
	/* write the media queries for background images of topic intros to the header tag */
	document.buildBgImgCSS = function ()
	{
		var topicCount = document.topicsCount + 1;
		var titlesCSS = "";
		for (var i = 1; i < topicCount; i++)
		{			
			//titlesCSS += ".topic_intro_grid_" + i + " { background-image:url('images/topic_title_" + i + "_480.jpg'); } ";			
			for (var ib = 1; ib < (document.wideTileArr[i - 1] + 1); ib++)
			{
				titlesCSS += ".menu_twelve_" + i + " { background-image:url('images/menu_topic_twelve_" + i +"_425.jpg'); } ";
			}
		}
		titlesCSS += "@media (min-width: 480px) { ";
		for (var j = 1; j < topicCount; j++)
		{
			/* height is the height of the image plus 62 */
			//titlesCSS += ".topic_intro_grid_" + j + " { background-image:url('images/topic_title_" + j + "_1008.jpg'); } ";			
			for (var jb = 1; jb < (document.wideTileArr[j - 1] + 1); jb++)
			{
				titlesCSS += ".menu_twelve_" + j + " { background-image:url('images/menu_topic_twelve_" + j +"_694.jpg'); } ";
			}
		}
		titlesCSS += " } ";
		titlesCSS += "@media (min-width: 768px) { ";
		for (var o = 1; o < topicCount; o++)
		{
			for (var oa = 1; oa < (document.wideTileArr[o - 1] + 1); oa++)
			{
				titlesCSS += ".menu_twelve_" + o + " { background-image:url('images/menu_topic_twelve_" + o +"_714.jpg'); } ";
			}
		}
		titlesCSS += " } ";
		titlesCSS += "@media (min-width: 1008px) { ";
		for (var k = 1; k < topicCount; k++)
		{
			/* height is the height of the image plus 62 */
			//titlesCSS += ".topic_intro_grid_" + k + " { background-image:url('images/topic_title_" + k + "_1366.jpg'); } ";
			for (var kb = 1; kb < (document.wideTileArr[k - 1] + 1); kb++)
			{
				titlesCSS += ".menu_twelve_" + k + " { background-image:url('images/menu_topic_twelve_" + k +"_960.jpg'); } ";
			}
		}		
		titlesCSS += " } ";
		titlesCSS += "@media (min-width: 1382px) { ";
		for (var m = 1; m < topicCount; m++)
		{
			/* height is the height of the image plus 62 */
			//titlesCSS += ".topic_intro_grid_" + m + " { background-image:url('images/topic_title_" + m + "_1980.jpg'); } ";			
		}		
		titlesCSS += " } ";
		$("<style>")
			.prop("type", "text/css")
			.html(titlesCSS)
			.appendTo("head");	
	};
	/* randomize the assessment questions */
	document.randomizeAssessment = function ()
	{		
		document.assessRandomQuesArr = [];		
		document.currentAssessQues = 0;		
		document.assessStartQuesArr = post_test_ques_dat['test_dat'][0].start_section_num.split(',');
		document.assessEndQuesArr = post_test_ques_dat['test_dat'][0].end_section_num.split(',');
		document.assessSelectQuesArr = post_test_ques_dat['test_dat'][0].select_ques.split(',');
		document.assessTotalQues = 0;	
		document.assessTopicHasQBank = post_test_ques_dat['test_dat'][0].topic_has_qbank.split(',');
		var testQuesTempArr = [];

		/* test out: if there is a pre-test in the course */
		if (document.pretestPresent == 1)
		{
			document.assessTopicHasQBank = post_test_ques_dat['test_dat'][0].topic_has_qbank.split(',');
			var testScore = actionsLMSGetScore();
			testScore = testScore == "" ? 0 : Number(testScore);
			/* test out: obtain the status of what topics have been tested out of */			
			var tempTPretestOutArr = document.topicsPretestedOut.split('');		
			/* test out: if all topics have been tested out of or the test was passed then reset the tested out topics but only locally */
			if ((document.topicsPretestedOut.substring(0,document.topicsCount - 2).indexOf('0') == -1) || (testScore >= document.assessPassmark))
			{
				for (var n = 0; n < document.assessSelectQuesArr.length; n++)
				{
					tempTPretestOutArr[n] = 0;
				}
			}
			for (var i = 0; i < document.assessSelectQuesArr.length; i++)
			{
				/* test out: if this topic was not tested out of then add its questions to the overall count */
				if (tempTPretestOutArr[i] == 0)
				{
					document.assessTotalQues = document.assessTotalQues + Number(document.assessSelectQuesArr[i]);				
				}
			}
		}
		else
		{
			for (var i = 0; i < document.assessSelectQuesArr.length; i++)
			{
				document.assessTotalQues = document.assessTotalQues + Number(document.assessSelectQuesArr[i]);
			}
		}	
		if (document.assessRandomize) {
			for (var j = 0; j < document.assessSelectQuesArr.length; j++)
			{
				/* test out: if there is a pre-test in the course */
				if (document.pretestPresent == 1)
				{				
					/* test out: if this topic has a question bank and the topic has not been tested out of then include this topics questions in the test */
					if ((document.assessTopicHasQBank[j] == "1") && (tempTPretestOutArr[j] == 0))
					{				
						while (testQuesTempArr.length != Number(document.assessSelectQuesArr[j])) {
							testQuesExists = false;
							randomTestQues = Math.floor(Math.random() * (Number(document.assessEndQuesArr[j]) - Number(document.assessStartQuesArr[j]) + 1)) + Number(document.assessStartQuesArr[j]);
							for (var k = 0; k < Number(document.assessSelectQuesArr[j]); k++) {						
								if (typeof testQuesTempArr[k] != 'undefined') {						
									if (testQuesTempArr[k] == randomTestQues) {							
										testQuesExists = true;
									}
								}						
							}
							if (!testQuesExists) {
								testQuesTempArr.push(randomTestQues)
								document.assessRandomQuesArr.push(randomTestQues);
							}
						}
						testQuesTempArr = [];
					}	
				}
				else
				{
					while (testQuesTempArr.length != Number(document.assessSelectQuesArr[j])) {
						testQuesExists = false;
						randomTestQues = Math.floor(Math.random() * (Number(document.assessEndQuesArr[j]) - Number(document.assessStartQuesArr[j]) + 1)) + Number(document.assessStartQuesArr[j]);
						for (var k = 0; k < Number(document.assessSelectQuesArr[j]); k++) {						
							if (typeof testQuesTempArr[k] != 'undefined') {						
								if (testQuesTempArr[k] == randomTestQues) {							
									testQuesExists = true;
								}
							}						
						}
						if (!testQuesExists) {
							testQuesTempArr.push(randomTestQues)
							document.assessRandomQuesArr.push(randomTestQues);
						}
					}
					testQuesTempArr = [];
				}
			}
		}
		else {
			for (var m = 0; m < document.assessSelectQuesArr.length; m++)
			{
				/* test out: if there is a pre-test in the course */
				if (document.pretestPresent == 1)
				{			
					if ((document.assessTopicHasQBank[m] == "1") && (tempTPretestOutArr[m] == 0))
					{
						for (var n = Number(document.assessStartQuesArr[m]); n < (Number(document.assessEndQuesArr[m]) + 1); n++) {
							document.assessRandomQuesArr.push(n);
						}
					}
				}
				else
				{
					if (document.assessTopicHasQBank[m] == "1" || document.assessTopicHasQBank[m] != 'undefined'){
						for (var n = Number(document.assessStartQuesArr[m]); n < (Number(document.assessEndQuesArr[m]) + 1); n++) {
							if(n != 0 || n != '0'){
								document.assessRandomQuesArr.push(n);
							}	
						}
					}	
				}
			}
		}
		if (document.assessRandomize) {
			document.assessRandomQuesArr = fisherYates(document.assessRandomQuesArr);
		}
		document.assessRandomQuesArr.push(document.testResultsSubsection);		
	}
	/* function to randomize an array based on Fisher Yates algorithm */
	function fisherYates(myArray) {
		var i = myArray.length;
        if (i == 0) return false;
        while (--i) {
            var j = Math.floor(Math.random() * (i + 1));
            var tempi = myArray[i];
            var tempj = myArray[j];
            myArray[i] = tempj;
            myArray[j] = tempi;
        }
		return myArray;
    }
	
	document.buildBgImgCSS();
});