﻿$(document).ready(function () {
	$.each($('.divQuizOuter'), function (key, value) {
		if ($(this).hasClass('quizAstma')) { resetQuiz($(this), initQuizAstma(), initQuizAstmaTxt()); }
		if ($(this).hasClass('quizAllergi')) { resetQuiz($(this), initQuizAllergi(), initQuizAllergiTxt()); }
		if ($(this).hasClass('quizDiabetes1')) { resetQuiz($(this), initQuizDiabetes1(), initQuizDiabetes1Txt()); }
		if ($(this).hasClass('quizDiabetes2')) { resetQuiz($(this), initQuizDiabetes2(), initQuizDiabetes2Txt()); }
		if ($(this).hasClass('quizMedicin')) { resetQuiz($(this), initQuizMedicin(), initQuizMedicinTxt()); }
		if ($(this).hasClass('quizForhoejetBlodtryk')) { resetQuiz($(this), initQuizForhoejetBlodtryk(), initQuizForhoejetBlodtrykTxt()); }
		if ($(this).hasClass('quizLus')) { resetQuiz($(this), initQuizLus(), initQuizLusTxt()); }
	});
});

function resetQuiz(iDiv, iQuestions, iCollTxt, iRndNum)
{
	setAnsCount(iDiv, 1, 0);
	setAnsCount(iDiv, 2, 0);

	var aRndNum = iRndNum == null ? randomNum() : iRndNum;

	var aTbl = iDiv.find('table');
	aTbl.data('collAnswers', iQuestions);
	aTbl.data('collTxt', iCollTxt);
	aTbl.data('inlineAnchorID', aRndNum);

	if (iDiv.find('.quizDisplay').length != 0 && iDiv.find('.quizDisplay').data('events') == null) {
		iDiv.before(createInlineAnchor('quizAnc_' + aRndNum + '_1'));
		iDiv.find('.quizDisplay').bind('click', function () { // QUIZ IS HIDDEN UNTIL ACTIVATED
			aTbl.toggle(true);
			iDiv.find('.quizDisplay').toggle(false);
		});
	}

	if ($(aTbl).find('TR').length > 2) { // RESET QUESTIONS TABLE
		$(aTbl).find('TR').each(function (iIndex) {
			if (iIndex > 1) { $(this).remove(); }
		});
		location.href = '#quizAnc_' + aRndNum + '_1';
	}

	createDynamicTable(aTbl, iQuestions.length, 4, iQuestions); // REBUILD QUESTION TABLE
}
function createDynamicTable(tbody, rows, cols, iQuestions) {
	var setBG = false;
	if (tbody == null || tbody.length < 1) return;
	for (var r = 0; r < rows; r++) {
		var trow = $("<tr>");
		for (var c = 0; c < cols; c++) {
			var colCls = 'tdQuizCol' + c; // BG color SWITCH
			colCls += (c > 1 && setBG) ? ' tdQuizBGclr' : '';
			$("<td>")
				.addClass(colCls) //ClassName
				.html(getQuestion(r, c, iQuestions)) // QUESTION
				.appendTo(trow);
			if (c == 3) { setBG = !setBG; }
		}
		trow.appendTo(tbody);
	}
}
function getQuestion(iRow, iCol, iQuestions) {
	switch (iCol) {
		case 1:
			return createRadio(iRow, 2) // ANSWER BUTTONS
			break;
		case 2:
			return iQuestions[iRow].ques // QUESTION
			break;
		case 3:
			return $('<div id=\"div' + +iRow + '\">') // ANSWER DIV
			break;
		default:
			return createRadio(iRow, 1)
	}
}

function calcAnswer(iObj) {

	if ($(iObj).is(':disabled') == false) {

		$aAnswers = $(iObj).closest('TABLE').data('collAnswers');

		var iIndex = $(iObj).data('aIndex');
		$(iObj).attr('checked', true);

		var aAns = $aAnswers[iIndex].ans == $(iObj).val() ? 'Korrekt' : 'Forkert'; // COMPARE ANSWER
		var bgHighLight = $aAnswers[iIndex].ans == $(iObj).val() ? 'color:#329664;' : 'color:#d00;';

		$topDiv = $(iObj).closest('.divQuizOuter');

		if ($aAnswers[iIndex].ans == $(iObj).val()) {
			setAnsCount($topDiv, 1, parseInt(getAnsCount($topDiv, 1)) + 1);
		} else {
			setAnsCount($topDiv, 2, parseInt(getAnsCount($topDiv, 2)) + 1);
		}

		$TR = $(iObj).closest('TR');
		$TR.children('.tdQuizCol0').removeClass('tdQuizCol0').addClass('tdQuizColNeutral');
		$TR.children('.tdQuizCol1').removeClass('tdQuizCol1').addClass('tdQuizColNeutral');
		$TR.each(function () { $(this).find('label').attr('disabled', 'disabled') });
		$TR.each(function () { $(this).find('input').attr('disabled', 'disabled') });

		$TR.find('div').html('<div style=\"' + bgHighLight + '\"><b>Dit svar er ' + aAns + '</b></div><p>' + $aAnswers[iIndex].ansSummary + '</p>');
		if ((getAnsCount($topDiv, 1) + getAnsCount($topDiv, 2)) == $aAnswers.length) { setSummary($topDiv, $aAnswers, $(iObj).closest('TABLE').data('collTxt'), $(iObj).closest('TABLE').data('inlineAnchorID')); } // SUMMARY
	}
}
function setCurrentRadio(iObj) { calcAnswer(iObj); }

function setAnsCount(iDiv, iIndex, iVal) { iDiv.find('input:hidden:nth-child(' + iIndex + ')').val(iVal); }
function getAnsCount(iDiv, iIndex) { return parseInt(iDiv.find('input:hidden:nth-child(' + iIndex + ')').val()); }

function createRadio(iRow, iInt) {
	var aLbl = $('<label>')
	var aTxt = iInt == 1 ? 'Rigtigt' : 'Forkert';
	var aVal = iInt == 1 ? '1' : '0';

	var radio = $('<input>').attr({ type: 'checkbox', value: aVal, title: aTxt });
	radio.click(function () { setCurrentRadio(this); });
	radio.data('aIndex', iRow);
	aLbl.append(radio);
	aLbl.click(function () { setCurrentRadio(radio); })
	aLbl.attr('class', 'rdoQuizOuter');

	return aLbl;
}
function setSummary(iDiv, ansColl, collTxt, iRndNum) {

	var aStr = getAnsCount(iDiv, 1) == 1 ? getAnsCount(iDiv, 1) + ' rigtig' : getAnsCount(iDiv, 1) + ' rigtige';

	var splitTotal = parseInt(ansColl.length / 2);
	var txtSum = parseInt(aStr) > splitTotal ? 'Flot! ' : ''; //Mere end ' + splitTotal + ' rigtige: //Under ' + splitTotal + ' rigtige: 
	var txtSum2 = parseInt(aStr) > splitTotal ? collTxt[1].txt + '<br /><br />' : collTxt[2].txt + '<br /><br />';

	var aTbl = $(iDiv).find('table');
	var trow = $('<tr>');

	$('<td colspan="4">')
		.html('<a class="displayBlock" name="quizAnc_' + iRndNum + '_2"><img src="/Content/Images/blank.gif" width="1" height="1" alt="" /></a><div class="SpaceBtm"><b>' + txtSum + 'Du fik ' + aStr + ' ud af ' + ansColl.length + ' spørgsmål. (' + (getAnsCount(iDiv, 1) / (ansColl.length) * 100).toFixed(0) + '%)</b></div><div class="SpaceBtm">' + txtSum2 + collTxt[0].txt + '</div>') //<input type="button" onclick="resetQuiz(' + iDiv.id + ', ansColl);" value="Start quizzen forfra og prøv igen" />
		.append($('<input>').attr({ type: 'button', name: 'btnReset', value: 'Start quizzen forfra og prøv igen', id: 'btnReset', title: 'Start quizzen forfra og prøv igen' }).click(function () { resetQuiz($(iDiv), ansColl, collTxt, iRndNum); }))
		.appendTo(trow);
	trow.appendTo(aTbl);

	location.href = '#quizAnc_' + iRndNum + '_2';
}
function createInlineAnchor(iName)
{ //<a class="ancQuiz displayBlock" name="dfgsdg234234"><img src="/Content/Images/blank.gif" width="1" height="1" alt="" /></a>
	return $("<a>").addClass('displayBlock').attr('name', iName)
		.append($("<img>").attr({ src: '/Content/Images/blank.gif', width: 1, height: 1, alt: '' }))
}
