function hideById(id) {
	var e = document.getElementById(id);
	if (e != null) {
		e.style.display = 'none';
	}
}

function showById(id) {
	var e = document.getElementById(id);
	if (e != null) {
		e.style.display = 'block';
	}
}

function Answer(answer, score) {
	this.answer = answer;
	this.score = score;
}

function Question(question, answers) {
	this.question = question;
	this.answers = answers;
}

function Badge(minScore, maxScore, scoreBasedText, badgeCode) {
	this.minScore = minScore;
	this.maxScore = maxScore;
	this.scoreBasedText = scoreBasedText;
	this.badgeCode = badgeCode;
}

function Link(url, text) {
	this.url = url;
	this.text = text;
}

function Quiz() {
	this.questions = [];
	this.currentQuestion = 0;
	this.score = 0;
	this.totalScore = 0;
	this.badges = [];
	this.url = '';
	this.links = [];

	this.addQuestion = function(question) {
		var maxScore = 0;
		for (var i = 0 ; i < question.answers.length ; i++) {
			if (question.answers[i].score > maxScore) {
				maxScore = question.answers[i].score;
			}
		}
		this.totalScore += maxScore;
		this.questions[this.questions.length] = question;
	};
	this.addBadge = function(badge) {
		this.badges[this.badges.length] = badge;
	};
	this.setQuizUrl = function(url) {
		this.url = url;
	};
	this.addLink = function(url) {
		this.links[this.links.length] = url;
	};
}

var _quiz = new Quiz();
function setQuiz(quiz) {
	_quiz = quiz;
	updateTotalScore(quiz)
	updateScore(quiz);
	updateTotalProgress(quiz)
	updateProgress(quiz);
	showQuestion(quiz);
}
function getQuiz() {
	return _quiz;
}

function updateTotalScore(quiz) {
	$(".TotalScore").html(quiz.totalScore);
}

function updateScore(quiz) {
	var percent = Math.round(100 * quiz.score / quiz.totalScore);
	$(".Score").html('' + quiz.score);
	$(".ScorePercent").html('' + percent);
	$(".ScoreWidth").css("width", percent + '%');
	$(".ScoreHeight").css("height", percent + '%');
}

function updateTotalProgress(quiz) {
	$(".TotalProgress").html('' + quiz.questions.length);
}

function updateProgress(quiz) {
	var percent = Math.round(100 * quiz.currentQuestion / quiz.questions.length);
	$(".Progress").html('' + (quiz.currentQuestion + 1 > quiz.questions.length ? quiz.questions.length : quiz.currentQuestion + 1));
	$(".ProgressPercent").html('' + percent);
	$(".ProgressWidth").css("width", percent + '%');
	$(".ProgressHeight").css("height", percent + '%');
}

function showQuestion(quiz) {
	var questionIndex = quiz.currentQuestion + 1 >= quiz.questions.length ? quiz.questions.length - 1 : quiz.currentQuestion;

	var e = document.getElementById('Question');
	if (e != null) {
		e.innerHTML = quiz.questions[questionIndex].question;
	}
	var questionHTML = '<ol>';
	for (var i = 0 ; i < quiz.questions[questionIndex].answers.length ; i++) {
		questionHTML += '<li>' + '<a href="javascript: submitAnswer(' + quiz.currentQuestion + ',' + i + ');">' + quiz.questions[questionIndex].answers[i].answer + '</a></li>'
	}
	questionHTML += '</ol>';
	e = document.getElementById('Answers');
	if (e != null) {
		e.innerHTML = questionHTML;
	}
}

function submitAnswer(questionNumber, answerNumber) {
	var quiz = getQuiz();
	if (quiz.currentQuestion < quiz.questions.length) {
		quiz.score += quiz.questions[questionNumber].answers[answerNumber].score;
		updateScore(quiz);
		quiz.currentQuestion++;
		updateProgress(quiz);
		showQuestion(quiz);
		if (quiz.currentQuestion >= quiz.questions.length) {
			hideById('Questions');
			showBadge(quiz);
			showById('Results');
		}
	}
}

function showBadge(quiz) {
	var displayCode = '';
	var html = '';
	var scoreBasedText = '';
	for(var i = 0 ; i < quiz.badges.length ; i++) {
		if (quiz.score >= quiz.badges[i].minScore && quiz.score < quiz.badges[i].maxScore) {
			var badgeHtml = quiz.badges[i].badgeCode;
			badgeHtml = badgeHtml.replace('QuizUrlSubstitution', quiz.url);
			badgeHtml = badgeHtml.replace('QuizUrlSubstitution', quiz.url);
			badgeHtml = badgeHtml.replace('LinkSubstitution', getRandomLink(quiz));
			badgeHtml = badgeHtml.replace('ScoreSubstitution', '' + quiz.score);
			badgeHtml = badgeHtml.replace('TotalScoreSubstitution', '' + quiz.totalScore);
			badgeHtml = badgeHtml.replace('ScorePercentageSubstitution', '' + Math.round(100 * quiz.score / quiz.totalScore));
	
			displayCode += '<div class="Display">';
			displayCode += badgeHtml;
			displayCode += '</div>';

			html += '<textarea onclick="this.focus(); this.select();">';
			html += badgeHtml;
			html += '</textarea>';

			scoreBasedText = quiz.badges[i].scoreBasedText;
		}
	}
	var e = document.getElementById('Badge');
	if (e != null) {
		e.innerHTML = displayCode;
	}
	e = document.getElementById('BadgeCode');
	if (e != null) {
		e.innerHTML = html;
	}
	$(".ScoreBasedText").html('' + scoreBasedText);	
}

function getRandomLink(quiz) {
	var url = quiz.links[Math.floor(Math.random() * quiz.links.length)];
	return '<a href="' + url.url + '">' + url.text + '</a>';
}
