', // id, question, progress, total count, answers, button
button: '{1}', // class, text
answer: '
{1}
', // id, text
answer_result: '', // result text correnct/incorrect
answer_hint: '
{0}
', // text
result: '
{0}
{1}
{4}
{2}{3}
', // result title text, long text, button
picture: '',
title_text: '{0}',
face: '',
result_image: '',
audio: '
',
share: 'Sdílej výsledek testu'
};
this.ze = [
2,
3,
4,
6,
7,
14,
16,
17,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
70,
100
];
this.url = 'https://aplikace.rozhlas.cz/kvizy/export/%id/kviz.json';
this.id = 0;
this.title = "";
this.faces = false;
this.random_answers = false;
this.random_questions = false;
this.countdown_type = "none";
this.coutndown_limit = 0;
this.points = false;
this.text = "";
this.picture = "";
this.questions_sort = [];
this.questions = [];
this.results = [];
this.countdown = null; // Countdown object
this.parent = null;
this.step = -1; // -1 front, 0 first question, 1.... - result
this.starttime = 0;
this.close_logged = false;
this.status = 'loaded'; // loaded, started, conpleted, restarted
this.player_lib = {
'jwplayer': 'https://www.rozhlas.cz/img/jwplayer/7/2/4/jwplayer.js',
'uniplayer': 'https://aplikace.rozhlas.cz/reportaze/sites/default/modules/cro_reportaze/dist/jquery.uniplayer.js',
'key': 'XcWx9RdKt0onC3AOOqe7dooKPvT7xgOkL0OOk/9yLD8='
};
}
/**
* init function, load quiz
* @param {DOM element} element
* @returns {undefined}
*/
Quiz.prototype.init = function (element) {
this.id = parseInt(element.text());
this.parent = element;
// this.parent.before(this.html.loader);
window.jwplayer = undefined;
$.fn.uniplayer_init = undefined;
this.load();
};
/**
* Load new Quiz from JSONP
* @returns {undefined}
*/
Quiz.prototype.load = function () {
var d = new Date();
var t = d.getTime();
$.ajax({
url: this.url.replace('%id', this.id),
dataType: 'jsonp',
cache: true,
jsonp: false
});
};
/**
* @param {Boolean} reset
* @returns {undefined}
*/
Quiz.prototype.start = function () {
// this.parent.prev().remove();
$('.quiz-wrapper').toggleClass('loaded', true);
this.step = -1;
this.status = 'loaded';
this.parent.empty();
this.appendFront();
this.log('load', this.title + ' [' + this.id + ']');
$('.quiz').show();
this.parent.children('.quiz-frame').show();
this.countdown = new Countdown(this);
};
/**
* restart quiz
* @returns {undefined}
*/
Quiz.prototype.restart = function () {
for (var i in this.questions) {
if (this.questions.hasOwnProperty(i)) {
this.questions[i].reset();
}
}
if (this.countdown) {
this.countdown.left = this.countdown.limit;
this.countdown.stats_left = this.countdown.stats_limit;
}
this.parent.children('.quiz-frame').slideUp(function () {
$(this).remove();
});
this.step = -1;
this.status = 'restarted';
this.log('retry', this.title + ' [' + this.id + ']');
this.nextStep();
};
Quiz.prototype.appendFront = function () {
this.parent.append(this.renderFront());
var that = this;
this.parent.find('.button').click(function () {
that.nextStep();
that.log('start', that.title + ' [' + that.id + ']');
that.parent.parent().parent().find('.image').slideUp();
$('.picture-quiz .img').slideUp();
});
/* workaround to prevent lightbox on image */
if ($.fn.jquery !== "1.6.4") {
this.parent.parent().parent().find('.image a').off('click');
}
this.parent.parent().parent().find('.image a').unbind('click');
this.parent.parent().parent().find('.image a').attr('href', 'javascript:void(0)');
if ($.fn.jquery === "1.6.4") {
this.parent.parent().parent().find('.image').click(function () {
that.nextStep();
that.log('start', that.title + ' [' + that.id + ']');
that.parent.parent().parent().find('.image').slideUp();
$('.picture-quiz .img').slideUp();
});
} else {
this.parent.parent().parent().find('.image').on('click', function () {
that.nextStep();
that.log('start', that.title + ' [' + that.id + ']');
that.parent.parent().parent().find('.image').slideUp();
$('.picture-quiz .img').slideUp();
});
}
};
Quiz.prototype.nextStep = function () {
this.parent.children('.quiz-frame').slideUp(function () {
$(this).remove();
});
this.showNextStep();
};
Quiz.prototype.showNextStep = function () {
this.step += 1;
if (this.step === 0 && this.status === 'loaded') {
this.starttime = this.now();
}
if (this.step >= this.questions.length) {
this.appendResult();
this.parent.children('.quiz-frame').slideDown();
this.status = 'completed';
this.logTime(false);
} else {
this.questions[this.step].appendQuestion();
this.parent.children('.quiz-frame').slideDown();
this.status = 'started';
this.countdown.nextFrame();
}
this.updateProgressBar();
};
/**
* prepare html element
* @returns {String} out
*/
Quiz.prototype.renderFront = function () {
var out = "";
var button = this.html.button.format('start', this.strings.begin);
out += this.html.front_simple.format(button);
// if (this.picture !== null) {
// out += this.html.front.format(this.picture,this.title,this.text, button);
// } else {
// out += this.html.front_nopic.format(this.title,this.text, button);
// }
return out;
};
/**
*
* @returns {undefined}
*/
Quiz.prototype.appendResult = function () {
var result = this.renderResult()
this.parent.append(result.element);
if (this.countdown && this.countdown.type === "all") {
this.countdown.init(true);
}
var that = this;
this.parent.find('.button').click(function () {
that.restart();
});
var count = this.getQuestionCounts();
this.log('completed-correct', this.title + ' [' + this.id + ']', count.correct);
};
/**
*
* @returns {_L13.Quiz.prototype@arr;results@call;render}
*/
Quiz.prototype.renderResult = function () {
var res = this.getQuestionCounts();
if (this.results.length === 1) {
return this.results[0].render(res.correct, res.total, res.timedout);
} else {
for (var i in this.results) {
if (this.results.hasOwnProperty(i)) {
var r = this.results[i];
if ((r.from <= res.correct) && (r.to >= res.correct)) {
return this.results[i].render(res.correct, res.total, res.timedout);
}
}
}
}
};
/**
*
* @param {Boolean} type - correct/incorrect
* @returns {String}
*/
Quiz.prototype.renderFace = function (type) {
if (!this.faces)
return null;
var rnd = Math.floor(Math.random() * 3);
if (type) {
return this.html.face.format('correct', "face-" + rnd);
} else {
return this.html.face.format('incorrect', "face-" + rnd);
}
};
/**
* updates progressBar width
* @returns {undefined} none
*/
Quiz.prototype.updateProgressBar = function () {
var qc = this.getQuestionCounts();
var w = Math.floor((qc.answered / qc.total) * this.strings.barwidth);
this.parent.find('.progressbar').animate({width: w});
};
/**
* Process JSON input data
* @param {Araay} data
* @returns {undefined}
*/
Quiz.prototype.processData = function (data) {
var q = data.nodes[0].node;
this.title = q.title;
this.faces = q.faces === '1' || false;
this.random_answers = q.random_answers || false;
this.random_questions = q.random_questions || false;
this.picture = q.picture || null;
this.text = q.text;
this.countdown_type = q.countdown_type || "none";
this.countdown_limit = q.countdown_limit || 0;
this.questions_sort = JSON.parse(q.questions_sort);
var q_obj = {};
var audio = false;
for (var i in q._questions.nodes) {
if (q._questions.nodes.hasOwnProperty(i)) {
var e = q._questions.nodes[i].node;
if (e !== undefined) {
var qu = new Question(e.id, this);
qu.text = e.text;
qu.picture = e.picture;
if (e.picture_data !== null) {
$('.quiz-before').addClass('picture-quiz');
qu.picture_data = e.picture_data;
var ratio = qu.picture_data.width / qu.picture_data.height;
if (ratio > 1) {
qu.picture_data.orientation = 'horizontal';
} else {
qu.picture_data.orientation = 'vertical';
}
}
if (e.audio !== null) {
qu.audio = e.audio;
audio = true;
}
qu.setAnswers(e, q.random_answers);
q_obj[qu.id] = qu;
}
}
}
if (audio) {
this.initPlayer();
}
for (var i = 0; i < this.questions_sort.length; i++) {
this.questions[i] = q_obj[this.questions_sort[i]];
}
for (var i in q._results.nodes) {
if (q._results.nodes.hasOwnProperty(i)) {
var r = q._results.nodes[i].node;
if (e !== undefined) {
var re = new Result(r.id, this);
re.setVals(r.from, r.to, r.text, r.title);
this.results.push(re);
}
}
}
if (parseInt(this.random_questions) === 1) {
this.questions = shuffle(this.questions);
}
this.start();
};
/**
*
* @returns {_L13.Quiz.questions.length|Number} total nubmer of questions
*/
Quiz.prototype.getTotal = function () {
return this.questions.length;
};
/**
*
* @returns {_L13.Quiz.prototype.getQuestionResults.Anonym$1} array of total, answered, correct, incorrect questions/answers
*/
Quiz.prototype.getQuestionCounts = function () {
var total = 0;
var answered = 0;
var correct = 0;
var incorrect = 0;
var timedout = 0;
for (var i in this.questions) {
if (this.questions.hasOwnProperty(i)) {
total += 1;
if (this.questions[i].getResult() > -1) {
answered += 1;
var res = parseInt(this.questions[i].getResult());
if (this.points) {
correct += res;
} else {
if (res === 1) {
correct += 1;
} else if (res === TIMEDOUT) {
timedout += 1;
} else {
incorrect += 1;
}
}
}
}
}
return {total: total, answered: answered, correct: correct, incorrect: incorrect, timedout: timedout};
};
/**
* load player scripts and initialise uniplayer
* @returns {undefined}
*/
Quiz.prototype.initPlayer = function () {
var self = this;
if (typeof jwplayer !== 'undefined' && typeof jwplayer.api !== 'undefined') {
$('.uniplayer:not(.uniplayer-audio)').uniplayer();
} else {
$.cachedScript(self.player_lib.jwplayer, {
complete: function () {
window.jwplayer.key = "XcWx9RdKt0onC3AOOqe7dooKPvT7xgOkL0OOk/9yLD8=";
if (!$.isFunction($.fn.uniplayer_init)) {
$.cachedScript(self.player_lib.uniplayer, {
complete: function () {
$('.uniplayer').uniplayer();
}
});
} else {
$('.uniplayer').uniplayer();
}
}
});
}
}
/**
*
* @returns {Number} current timestamp
*/
Quiz.prototype.now = function () {
return parseInt(new Date().getTime() / 1000);
};
/**
*
* @returns {Number} timediff in sec
*/
Quiz.prototype.runningTime = function () {
return (this.now() - this.starttime);
};
/**
*
* @param {Boolean} close if quiz is finished or window closed
* @returns {undefined} none
*/
Quiz.prototype.logTime = function (close) {
if (!close) {
if (this.starttime > 0 && !this.close_logged) {
this.close_logged = true;
this.log(this.status + '-time', this.title + ' [' + this.id + ']', this.runningTime());
}
} else {
if (this.starttime > 0) {
if (this.status !== 'completed') {
this.log('closed-time', this.title + ' [' + this.id + ']', this.runningTime());
this.log('closed-answers', this.title + ' [' + this.id + ']', this.step + 1);
}
this.starttime = -1; // ochrana aby sa to nezavolalo 2x
} else if (this.starttime === 0) {
this.log('closed-nostart', this.title + ' [' + this.id + ']');
}
}
};
/**
* Log to google analytics
* @param {String} event
* @param {String} id
* @param {Number} value
* @returns {undefined}
*/
Quiz.prototype.log = function (event, id, value) {
try {
if (value !== null) {
if (window._gaq !== undefined) {
_gaq.push(['_trackEvent', 'quiz', event, id, value]);
} else {
ga('send', 'event', 'quiz', event, id, value);
}
} else {
if (window._gaq !== undefined) {
_gaq.push(['_trackEvent', 'quiz', event, id]);
} else {
ga('send', 'event', 'quiz', event, id);
}
}
} catch (err) {
console.log(err);
console.log(['_trackEvent', 'quiz', event, id, value]);
}
};
/**
* Question class
* @param {Number} id NID of question
* @param {Quiz} quiz
* @returns {_L1.Question}
*/
function Question(id, quiz) {
this.quiz = quiz;
this.id = id;
this.text = "";
this.picture = null;
this.picture_data = null;
this.audio = null;
this.answers_sort = [];
this.answers = [];
this.answered = -1;
this.selected = {
id: 0,
index: -1
};
}
/**
*
* @returns {undefined}
*/
Question.prototype.appendQuestion = function () {
var q = this.quiz.parent;
var question = this;
q.append(this.render());
if (q.find('.audio').length > 0) {
q.find('.uniplayer').uniplayer();
jwplayer().setVolume(100);
// jwplayer().play();
}
// console.log('renderQuestion');
q.find('.answer input').click(function () {
question.select(this);
});
q.find('.answer-text').click(function () {
if (!$(this).parent().hasClass('disabled')) {
var el = $(this).parent().children('input');
el.prop('checked', true);
el.trigger('click');
}
});
q.find('.button').addClass('disabled');
q.find('.button').click(function () {
if (!$(this).hasClass('disabled')) {
question.quiz.nextStep();
}
});
};
/**
* actions after click on answer element
* @param {type} element
* @returns {undefined}
*/
Question.prototype.select = function (element, last) {
var question = this;
var q = this.quiz.parent;
question.quiz.countdown.pause();
q.find('input').attr('disabled', 'disabled');
q.find('.answer').addClass('disabled');
q.find('.button').removeClass('disabled');
var answer = -1;
if (element === false) {
answer = question.submitAnswer(false);
} else {
answer = question.submitAnswer($(element).val());
}
if (!this.quiz.points) {
if (parseInt(answer) === 1) {
$(element).parent().addClass('correct');
$(element).parent().append(question.quiz.html.answer_result.format(question.quiz.strings.correct));
question.appendHint($(element), question.selected.index);
var face = $(question.quiz.renderFace(true));
} else {
$(element).parent().addClass('incorrect');
$(element).parent().append(question.quiz.html.answer_result.format(question.quiz.strings.incorrect));
if (element === false) {} else {
var correct = question.getCorrectAnswer();
var c = q.find('input[value="' + correct.id + '"]');
c.parent().addClass('correct');
var hint_index = question.selected.index;
question.appendHint(c, hint_index);
}
var face = $(question.quiz.renderFace(false));
if (this.quiz.countdown.type === "turbo") {
last = true;
}
}
face.hide();
q.children().append(face);
if (this.quiz.countdown.type !== "none" && this.quiz.faces) {
var quiz = this.quiz;
var c = quiz.countdown.element;
var f = face;
c.hide('slide', {
direction: 'up',
easing: 'easeOutQuint'
});
setTimeout(function () {
f.show('slide', {
direction: 'down',
easing: 'easeOutQuint'
});
}, 1000);
} else {
face.show('slide', {
direction: 'down',
easing: 'easeOutQuint'
});
}
}
q.find('input').attr('disabled', 'disabled');
q.find('.answer').addClass('disabled');
if (last) {
this.quiz.step = this.quiz.getQuestionCounts().total - 1;
q.find('.button').text(this.quiz.strings.results);
q.find('.button').removeClass('next');
q.find('.button').addClass('results');
}
this.autoNext();
q.find('.button').removeClass('disabled');
question.quiz.updateProgressBar();
};
Question.prototype.autoNext = function () {
if (this.quiz.step <= this.quiz.questions.length && this.text.length == 0) {
if (this.quiz.countdown.type !== "none" && (this.quiz.countdown.type === "turbo" || this.quiz.countdown.type === "single")) {
var timeout = 1500;
if (this.quiz.faces) {
timeout += 1000;
}
var button = this.quiz.parent.find('.button');
setTimeout(function () {
button.trigger('click');
}, timeout);
}
}
};
/**
*
* @returns {String} html string for question
*/
Question.prototype.render = function () {
var q = this.quiz;
var text = q.html.title_text.format(this.text) || '';
var counts = q.getQuestionCounts();
var button = "";
if (q.step + 1 < counts.total) {
button += q.html.button.format('next', q.strings.next);
} else {
button += q.html.button.format('results', q.strings.results);
}
if (this.picture !== null) {
text += q.html.picture.format(this.picture.src, 'question');
}
if (this.audio !== null) {
text += q.html.audio.format(this.audio, 'question', 'true');
}
return q.html.question.format(this.id, text, q.step + 1, counts.total, this.renderAnswers(), button);
};
/**
*
* @returns {String} html string of answers
*/
Question.prototype.renderAnswers = function () {
var out = "";
for (var i in this.answers) {
if (this.answers.hasOwnProperty(i)) {
out += this.answers[i].render();
}
}
return out;
};
/**
*
* @param {type} element
* @param {type} index
* @returns {undefined}
*/
Question.prototype.appendHint = function (element, index) {
if (this.answers[index].result !== null) {
var hint = $(this.quiz.html.answer_hint.format(htmlDecode(this.answers[index].result)));
hint.hide();
element.parent().parent().after(hint);
hint.slideDown();
}
};
/**
*
* @param {Array} e - data from JSON
* @param {Boolean} randomize - if answers should be shuffled
* @returns {undefined}
*/
Question.prototype.setAnswers = function (e, randomize) {
var a_obj = {};
var data = e._answers.nodes;
this.answers_sort = JSON.parse(e.answers_sort);
var audio = false;
for (var i in data) {
if (data.hasOwnProperty(i)) {
var a = data[i].node;
if (a !== undefined) {
var an = new Answer(a.id, this.quiz);
if (a.audio !== null) {
audio = true;
}
an.setVals(a.text, a.correct, a.points, a.picture, a.picture_data, a.audio, a.result);
a_obj[an.id] = an;
}
}
}
if (audio) {
this.quiz.initPlayer();
}
for (var i = 0; i < this.answers_sort.length; i++) {
this.answers[i] = a_obj[this.answers_sort[i]];
}
if (parseInt(randomize) === 1) {
this.answers = shuffle(this.answers);
}
};
/**
* Gets if answer is correct or false and sets values in question, if false force false answer without ID
* @param {Integer} answer ID
* @returns {Integer|_L1.Question.prototype.submitAnswer@arr;answers.correct} 0/1 incorrect/correct
*/
Question.prototype.submitAnswer = function (answer) {
if (answer !== false) {
for (var i in this.answers) {
if (this.answers.hasOwnProperty(i)) {
var a = this.answers[i];
if (parseInt(a.id) === parseInt(answer)) {
this.selected = {
id: a.id,
index: i
};
if (this.quiz.points) {
this.answered = a.points;
} else {
this.answered = a.correct;
}
break;
}
}
}
this.quiz.log('answer', this.text + ' [' + this.id + '] > ' + this.answers[this.selected.index].text + ' [' + answer + ']');
if (this.quiz.points) {
this.quiz.log('question-points', this.text + ' [' + this.id + ']');
} else {
if (parseInt(this.answered) === 1) {
this.quiz.log('question-correct', this.text + ' [' + this.id + ']');
} else {
this.quiz.log('question-incorrect', this.text + ' [' + this.id + ']');
}
}
} else {
this.selected = {
id: 0,
index: -1
};
this.answered = TIMEDOUT; //timedout
this.quiz.log('answer', this.text + ' [' + this.id + '] > ' + "Timeout" + ' [' + answer + ']');
this.quiz.log('question-timedout', this.text + ' [' + this.id + ']');
}
return this.answered;
};
/**
* return index of correct answer for question
* @returns {Number, Number} index of correct answer, if no correct answer return -1
*/
Question.prototype.getCorrectAnswer = function () {
for (var i in this.answers) {
if (this.answers.hasOwnProperty(i)) {
if (parseInt(this.answers[i].correct) === 1)
return {index: i, id: this.answers[i].id};
}
}
return {index: -1, id: -1};
};
/**
* @returns {_L13.Question.anwers|Number} -1 if not answered, 0 if false, 1 if correct
*/
Question.prototype.getResult = function () {
return this.answered;
};
/**
* clears Question result/answer
* @returns {undefined}
*/
Question.prototype.reset = function () {
this.answered = -1;
this.selected = {
id: 0,
index: -1
};
};
/**
* Answer class
* @param {type} id NID of answer
* @param {Quiz} quiz
* @returns {_L1.Answer}
*/
function Answer(id, quiz) {
this.quiz = quiz;
this.id = id;
this.text = "";
this.picture = null;
this.picture_data = null;
this.audio = null;
this.correct = 0;
this.points = null;
this.result = "";
}
/**
* Set extra init values for Answer
* @param {String} text
* @param {Number} correct [0,1]
* @param {Number} points
* @param {String} result
* @returns {undefined}
*/
Answer.prototype.setVals = function (text, correct, points, picture, picture_data, audio, result) {
this.text = text;
this.correct = correct;
this.points = points;
this.picture = picture;
this.audio = audio;
if (this.points !== null) {
this.quiz.points = true;
this.points = Number(this.points);
}
if (picture_data !== null) {
$('.quiz-before').addClass('picture-quiz');
this.picture_data = picture_data;
var ratio = this.picture_data.width / this.picture_data.height;
if (ratio > 1) {
this.picture_data.orientation = 'horizontal';
} else {
this.picture_data.orientation = 'vertical';
}
}
this.result = result || null;
};
/**
*
* @returns {_L13.Answer.prototype@pro;quiz@pro;html@call;format}
*/
Answer.prototype.render = function () {
var text = this.text || '';
var cl = '';
if (this.picture !== null) {
text += this.quiz.html.picture.format(this.picture.src, 'answer');
cl = this.picture_data.orientation;
}
if (this.audio !== null) {
text += this.quiz.html.audio.format(this.audio, 'answer', 'false');
}
return this.quiz.html.answer.format(this.id, text, cl);
};
/**
* Result class
* @param {type} id NID of result
* @param {Quiz} quiz
* @returns {_L1.Result}
*/
function Result(id, quiz) {
this.quiz = quiz;
this.id = id;
this.from = 0;
this.to = 0;
this.texts = [];
this.title = "";
}
/**
* Extra values for Result
* @param {Number} from
* @param {Number} to
* @param {String} text
* @param {String} title
* @returns {undefined}
*/
Result.prototype.setVals = function (from, to, text, title) {
this.from = from;
this.to = to;
if (text === null) {
text = "";
}
this.texts = text.split("[;]");
if (this.texts[this.texts.length - 1] === "") {
this.texts.pop();
}
this.title = title;
};
/**
*
* @param {DOM Element} element
* @returns {undefined}
*/
Result.prototype.appendTo = function (element) {
var counts = this.quiz.getQuestionCounts();
var el = this.render(counts.correct, counts.total, counts.timedout);
element.append(el);
var that = this;
element.find('.button').click(function () {
that.quiz.restart();
});
};
Result.prototype.render = function (correct, total, timedout) {
var out = "";
if (this.title !== "") {
var title = this.title + " ";
} else {
var title = "";
}
var button = this.quiz.html.button.format('retry', this.quiz.strings.retry);
var z = 'z';
if ($.browser.msie) {
for (var i in this.quiz.ze) {
if (this.quiz.ze.hasOwnProperty(i)) {
if (this.quiz.ze[i] === total) {
z = 'ze';
break;
}
}
}
} else {
if (this.quiz.ze.indexOf(total) >= 0) {
z = 'ze';
}
}
var fireworks = '';
var ratio = parseFloat(correct) / parseFloat(total);
if (this.quiz.faces) {
if (ratio < 0.34) {
fireworks = this.quiz.html.result_image.format('result-worst');
}
if (ratio > 0.66) {
fireworks = this.quiz.html.result_image.format('result-best');
}
if (ratio >= 0.34 && ratio <= 0.66) {
fireworks = this.quiz.html.result_image.format('result-average');
}
}
var text_index = 0;
if (this.quiz.countdown_type !== "none") {
if (this.quiz.countdown_type === "turbo") {
if (timedout > 0) {
text_index = 2;
} else if (total - correct > 0) {
text_index = 1;
} else {
text_index = 0;
}
} else {
var text_index = this.quiz.countdown.getResultIndex(this.texts);
}
}
if (this.quiz.points) {
title = this.quiz.strings.result_title_simple.format(title);
} else {
title = this.quiz.strings.result_title.format(title, correct, total, z);
}
var t = (this.texts[text_index])
? this.texts[text_index]
: "";
var url = location.href + '?r=' + text_index + '&t=' + correct;
var cltext = $(t).text();
var cltitle = $('' + title + '').text();
var share = this.quiz.html.share.format(url, cltitle, cltext);
out += this.quiz.html.result.format(title, t, button, fireworks, share);
this.quiz.log('result', title + ' [' + this.id + ']');
if (this.texts[text_index] === '') {
var ht = $(out);
$(ht).find('.result-text').remove();
return {element: ht, title: cltitle, text: cltext, url: url};
}
return {element: out, title: cltitle, text: cltext, url: url};
};
/**
* Object Countdown
* @param {type} limit
* @param {type} quiz
* @returns {cro_kvizy_L17.Countdown}
*/
function Countdown(quiz) {
this.quiz = quiz;
this.type = quiz.countdown_type; // none, all, single, turbo
this.limit = quiz.countdown_limit * 1000;
this.left = quiz.countdown_limit * 1000;
this.stats_left = 0;
this.stats_limit = this.limit;
this.scripts_loaded = 0;
if (this.type !== "none") {
// this.pulsing = false;
if (this.type === "single" || this.type === "turbo") {
this.stats_limit = this.quiz.getQuestionCounts().total * this.limit;
}
this.active = false;
this.element = null;
this.knob = null;
this.html = '';
var countdown = this;
if ($.fn.knob) {
this.init();
} else {
$.getScript('https://aplikace.rozhlas.cz/kvizy/sites/all/libraries/cro_kvizy/jquery.knob.min.js', function () {
this.scripts_loaded++;
if (this.scripts_loaded === 2) {
countdown.init();
}
});
$.getScript('https://aplikace.rozhlas.cz/kvizy/sites/all/libraries/cro_kvizy/jquery.pulse.min.js', function () {
this.scripts_loaded++;
if (this.scripts_loaded === 2) {
countdown.init();
}
});
}
}
}
Countdown.prototype.init = function (result) {
result = (result)
? true
: false;
if (this.element) { // this.element.children().remove();
}
this.element = $(this.html);
this.knob = this.element.find('.knob');
this.update(this.left);
if (!result) {
this.quiz.parent.find('.question:last .answers').prepend(this.element);
} else {
this.quiz.parent.find('.result-text').append(this.element);
}
this.knob.attr('data-width', this.element.css('width').replace('px', ''));
this.knob.attr('data-height', this.element.css('height').replace('px', ''));
this.knob.attr('data-fgColor', rgb2hex(this.element.css('color')));
this.knob.attr('data-fgColorStart', rgb2hex(this.element.css('color')));
this.knob.attr('data-fgColorEnd', rgb2hex(this.element.css('background-color')));
this.knob.attr('data-bgColor', '#eae7e0');
this.element.css('background-color', 'transparent');
this.setKnob();
this.knob.css('font-size', this.element.css('font-size'));
};
Countdown.prototype.nextFrame = function () {
if (this.type === "none") {
return null;
}
if (this.type !== "all") {
this.left = this.limit;
}
this.init();
this.start();
};
Countdown.prototype.start = function () {
if (this.type === "none") {
return null;
}
var that = this;
this.element.everyTime(50, 'countdown-' + this.quiz.id, function () {
that.everyTime();
});
this.active = true;
this.element.stop();
var input = this.element.find('input');
var color = input.css('color');
// input.css('color', '#ffffff');
input.css('opacity', '1.0');
this.element.css('background-color', color);
input.animate({
'color': color
}, 1000);
this.element.animate({
'background-color': 'transparent'
}, {
'duration': 1000,
'easing': 'linear'
});
this.element.find('.knob').stop();
};
Countdown.prototype.stop = function () {
if (this.type === "none") {
return null;
}
if (this.type === "single" || this.type === "turbo") {
this.stats_left += this.left;
} else {
this.stats_left = this.left;
}
this.element.stopTime('countdown-' + this.quiz.id, null);
this.element.stop();
var input = this.element.find('input');
var color = input.css('color');
// input.css('color', '#ffffff');
input.css('opacity', '1.0');
this.element.css('background-color', color);
input.animate({
'color': color
}, 1000);
this.element.animate({
'background-color': 'transparent'
}, {
'duration': 1000,
'easing': 'linear'
});
this.element.find('.knob').stop();
this.active = false;
};
Countdown.prototype.pause = function () {
this.stop();
};
Countdown.prototype.resume = function () {
if (this.type === "none") {
return null;
}
if (this.type === "single" || this.type === "turbo") {
this.left = this.limit;
}
this.update(this.left);
this.start();
};
Countdown.prototype.everyTime = function () {
if (this.type === "none") {
return null;
}
this.left -= 50;
if (this.left <= 0 && this.type !== "all") {
this.left = 0;
this.timedout();
}
if (this.left === 0 && this.type === "all") {
this.stop();
this.element.remove();
this.nextFrame();
}
if (this.left % 1000 === 300) {
this.element.find('input').css('opacity', '0.5');
} else if (this.left % 1000 === 0) {
this.element.find('input').css('opacity', '1.0');
}
this.update(this.left);
};
Countdown.prototype.timedout = function () {
if (this.type === "none") {
return null;
}
var step = this.quiz.step;
var last = false;
if (this.type === "turbo") {
last = true;
}
this.stop();
this.quiz.questions[step].select(false, last, true);
};
/**
* update rendering
* @param {type} seconds
* @returns {undefined}
*/
Countdown.prototype.update = function (ms) { // this.element.find('.knob').attr('value', ms);
this.knob.val(ms).trigger('change');
};
Countdown.prototype.getResultIndex = function (array) {
if (this.type === "all") {
var step = this.stats_limit / (array.length - 1);
if (this.stats_left < 0) {
return array.length - 1;
}
var index = Math.floor((this.stats_limit - this.stats_left) / step);
return index;
} else {
return 0;
}
};
Countdown.prototype.setKnob = function () {
var knob = this.knob;
var cursor = false;
if (this.left <= 0) {
cursor = 30;
}
knob.knob({
'fgColorStart': knob.attr('data-fgColorStart'),
'fgColorEnd': knob.attr('data-fgColorEnd'),
'countdown': this,
'min': 0,
'max': this.limit,
'thickness': 0.4,
'readOnly': true,
'cursor': cursor,
'format': function (value) {
var sign = "";
if (value <= 0) {
value = Math.abs(this.countdown.left);
sign = "-";
}
var totalSec = Math.ceil(Math.abs(value) / 1000);
var minutes = parseInt(totalSec / 60) % 60;
var seconds = totalSec % 60;
var sep = ":";
// if (value % 1000 > 300) {
// sep = " ";
// }
var val = sign + (minutes < 10
? "0" + minutes
: minutes) + sep + (seconds < 10
? "0" + seconds
: seconds);
return val;
},
'parse': function (value) {
var v = parseFloat(value);
v = v % this.max;
// if (this.countdown.left < 0) {
// this.min = this.max * (-1);
// this.max = 0;
// }
return v;
},
'draw': function () {
var v = this.v;
if (this.o.countdown.left >= 0) {
this.cv = this.o.max - this.o.countdown.left;
} else {
this.cv = this.o.countdown.left % this.o.max * (-1);
}
var lwmax = (this.xy * this.o.thickness); // max line width
var lwmin = (lwmax * 0.7); // min line width
var lw = lwmax - lwmin;
var radmax = this.xy - lwmax / 2;
var radmin = radmax - ((lw) / 2);
this.lineWidth = lwmin;
this.radius = radmin;
if (v < 5000) {
var stp = (v % 1000) / 1000;
this.lineWidth = lwmin + lw * stp;
this.radius = radmin + (radmax - radmin) * stp;
this.o.fgColor = this.o.fgColorEnd;
this.$.css({'color': this.o.fgColorEnd});
}
if (v <= 6000 && v >= 5000) {
var v2 = v - 5000;
var ce = colorParse(this.o.fgColorStart); //Start color
var cs = colorParse(this.o.fgColorEnd); //End color
var ends = new Array(new Color(cs[0], cs[1], cs[2]), new Color(ce[0], ce[1], ce[2]));
var steps = 1000 / this.o.step;
var step = new Array(3);
var color = mixPalette();
this.o.fgColor = color;
this.$.css({'color': color});
}
function Color(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.coll = new Array(r, g, b);
this.text = cText(this.coll);
}
function colorParse(c) {
c = c.toUpperCase();
var col = c.replace(/[\#\(\)]*/i, '');
var num = new Array(col.substr(0, 2), col.substr(2, 2), col.substr(4, 2));
var ret = new Array(parseInt(num[0], 16), parseInt(num[1], 16), parseInt(num[2], 16));
return (ret);
}
function stepCalc() {
step[0] = (ends[1].r - ends[0].r) / steps;
step[1] = (ends[1].g - ends[0].g) / steps;
step[2] = (ends[1].b - ends[0].b) / steps;
}
function mixPalette() {
stepCalc();
var r = (ends[0].r + (step[0] * v2));
var g = (ends[0].g + (step[1] * v2));
var b = (ends[0].b + (step[2] * v2));
var color = new Color(r, g, b);
return color.text;
//console.log(palette[i]);
}
function cText(c) {
var result = '';
for (k = 0; k < 3; k++) {
var val = Math.round(c[k] / 1);
var piece = val.toString(16);
if (piece.length < 2) {
piece = '0' + piece;
}
result = result + piece;
}
result = '#' + result.toUpperCase();
return result;
}
}
});
};
/**
* Shuffles elements in array
* @param {Array} array
* @returns {@var;temporaryValue} shuffled array
*/
function shuffle(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) { // Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
/**
* start the process
*/
$(document).ready(function () {
$.getScript('https://aplikace.rozhlas.cz/kvizy/sites/all/libraries/cro_kvizy/jquery-ui.min.js');
if ($('.quiz-wrapper').length > 0) {
$('head').append('');
}
if (window.location.hostname.indexOf('radiojunior') >= 0 && $('.quiz-wrapper .quiz').length > 0) {
$('.quiz-wrapper').addClass('radiojunior');
$('.quiz-wrapper').before('');
$('.box-std .inner').after('');
$('.quiz-before').append('
' + $('.box-highlight').text() + '
');
$('.box-highlight').remove();
$('.quiz-before').append($('.box-img .img'));
$('.box-img').remove();
} else {
$('.quiz-wrapper').width($('.quiz-wrapper').parent().width() - 40);
}
$('.quiz-wrapper .quiz').each(function () {
var id = parseInt($(this).text());
if (id > 0) {
window.quizes[id] = new Quiz();
window.quizes[id].init($(this));
}
});
});
if ($.fn.jquery !== "1.6.4") {
$(window).on('beforeunload', function (e) {
for (q in window.quizes) {
window.quizes[q].logTime(true);
}
});
}
$(window).unload(function () {
for (q in window.quizes) {
window.quizes[q].logTime(true);
}
});
$.cachedScript = function (url, options) {
// Allow user to set any option except for dataType, cache, and url
options = $.extend(options || {}, {
dataType: "script",
cache: true,
url: url
});
// Use $.ajax() since it is more flexible than $.getScript
// Return the jqXHR object so we can chain callbacks
return jQuery.ajax(options);
};
}(jQuery));
function jsonp_kviz(data) {
var id = parseInt(data.nodes[0].node.nid);
window.quizes[id].processData(data);
}
function htmlDecode(input) {
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0
? ""
: e.childNodes[0].nodeValue;
}
var hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
function hex(x) {
return isNaN(x)
? "00"
: hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
function loadScript(url, callback) {
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
}