var qtitleId="#quiztitle";var quserId="#quizcreator";var qchanId="#quizchannel";var preImageId="#preview_image";var cueCtlId="#sticker";var replayId=".replay";var playerId=".quiz";var questionId="#quizquestion";var resultId="#quizresult";var errorId="#quizerror";var qtrackerId="#qtracker";var skipId="#skip";var reviewAnswersId="#review-answers";var viewResultId="#view-results";var saveScoresId="#save-scores";var channelNameId="#channel-name";var choicenames=["a","b","c","d"];var QNSEP=":::";var CHSEP=";~;";var ZBASE=100;var ZCUE5=105;var ZCUE4=104;var ZCUE3=103;var active_challenge=null;function Challenge(a){this.challenge=a;if(this.challenge.challenge){this.challenge=this.challenge.challenge}this.answers={qns:[],ans:[]};this.timeStarted=new Date();this.questions=null;this.result=null;this.video=null}Challenge.prototype.setQuestions=function(a){this.questions=a;$j(qtrackerId).empty();var b;for(qx=0;qx<this.questions.length;qx++){if(this.questions[qx].question){this.questions[qx]=this.questions[qx].question}b=this.questions[qx].id;$j(qtrackerId).prepend('<li><div class="todo" qn="q'+b+'">&nbsp;</div></li>')}$j(skipId).show();$j(viewResultId).hide()};Challenge.prototype.display=function(){$j(preImageId).attr("src",this.challenge.ytimg);$j(qtitleId).text(this.challenge.title||"title unknown");$j(quserId).text(this.challenge.creator);$j(quserId).attr("href","/challenges/by/user"+this.challenge.user_id);$j(qchanId).text(this.challenge.channel_name);$j(qchanId).attr("href","/channels/"+Data.cleanPath(this.challenge.channel_name));if(typeof(curChannel)!="undefined"&&curChannel&&curChannel.name){$j(channelNameId).text(curChannel.name)}};Challenge.prototype.cue=function(b){if(!b){b=false}var a=this;this.video=new Video(this.challenge.ytid);this.video.setAutoPlay(b);this.video.onCluePlayed(function(){$j(cueCtlId).show().css({"z-index":ZCUE5});$j(cueCtlId).removeClass("play").addClass("replay");$j(cueCtlId).unbind("click").click(function(){$j(cueCtlId).css({"z-index":ZBASE}).hide();if(a.result){a.video.playBackFull()}else{a.video.playBackCtl()}})});a.video.setPlayBackCtl(a.challenge.seekto,a.challenge.playto,a.challenge.playback);$j(cueCtlId).show().css({"z-index":ZBASE});$j(cueCtlId).unbind("click").click(function(){a.video.embed()});if(b){$j(cueCtlId).click()}};Challenge.prototype.askQuestion=function(c){if(!this.questions||this.questions.length<0){return false}var e=this.questions[c];if(!e){return false}if(typeof(e.asking)=="undefined"){return false}if(c==0){$j(questionId).empty();$j(resultId).hide();$j(errorId).hide()}else{$j(".question").hide()}var h=e.choices.split(CHSEP);var f=TAG("h2",{},"Q:"+e.asking);var a="";var d;for(i=0;i<h.length;i++){d='<a href="#">'+Data.escape(h[i])+"</a>";a+=TAG("li",{"class":"choice",choice:i},d)}d=TAG("ul",{id:"ch"+e.id},a);var g=TAG("div",{"class":"question",id:"q"+e.id},f+d);$j(questionId).append(g);var b=this;$j("#ch"+e.id+" li a").each(function(j){$j(this).click(function(){var k=$j(this).parent().attr("choice");$j(this).parent().addClass("chosen");b.noteAnswer(e.id,c,k);return false})});$j(qtrackerId+" .todo:last").addClass("select")};Challenge.prototype.noteAnswer=function(a,b,c){if(b==0){$j(skipId).hide()}this.answers[a]=c;this.answers.qns.push(a);this.answers.ans.push(c);$j(qtrackerId+" .todo:last").removeClass("todo").removeClass("select").addClass("done");if(b+1<this.questions.length){this.askQuestion(b+1)}else{this.checkAnswers()}};Challenge.prototype.checkAnswers=function(){var b="/challenges/check?challenge_id="+this.challenge.id;var a=new Date();a=Math.round((a-this.timeStarted)/1000);var c="questions="+encodeURIComponent(this.answers.qns.join(";"));c+="&answers="+encodeURIComponent(this.answers.ans.join(";"));c+="&took="+a;if(!this.video.isEmbedded()){this.video.embed(true)}else{this.video.playBackFull()}Data.post(b,c,this.onResult)};Challenge.prototype.onResult=function(c){$j(".question").hide();$j("#quiznext").unbind("click").click(function(){var e={after:c.challenge_id};if(typeof(curChannel)!="undefined"&&curChannel&&curChannel.id){e.channel=curChannel.id}getChallenge(e);return false});$j("li.choice a").unbind("click").unbind("hover").click(function(){return false});if(!c||!c.success){$j(errorId).show();return false}var d="You scored: "+c.num_correct+" out of "+c.num_questions;d+=" ("+c.score+"%)";$j(resultId+" h2").text(d);ptmsg="You earned: "+c.points+" points";if(c.total_points){ptmsg+=" ("+c.total_points+" total)"}$j(".quizpoints").html(ptmsg);if(c.prompt_to_save){$j(saveScoresId).show()}else{$j(saveScoresId).hide()}$j(resultId).show();var b=c.results;for(rc=0;rc<b.length;rc++){var a="#q"+b[rc].qnid;if(!b[rc].correct){$j(a+" .chosen").addClass("wrong");$j(qtrackerId+" [qn=q"+b[rc].qnid+"]").addClass("wrong")}else{$j(qtrackerId+" [qn=q"+b[rc].qnid+"]").addClass("correct")}$j(a+" .choice[choice="+b[rc].expected+"]").addClass("correct")}$j(".q-tracker .done").css({cursor:"pointer"}).click(function(){$j(".q-tracker .done").removeClass("select");$j(this).addClass("select");var e=$j(this).attr("qn");$j(".question").hide();$j("#"+e).show();$j(viewResultId).show()});$j(reviewAnswersId).show().click(function(){$j(".q-tracker .done:last").click();$j("#view-results").show();return false});$j(viewResultId).click(function(){$j(".q-tracker .done").removeClass("select");$j(".question").hide();$j(resultId).show();$j(this).hide();return false})};function presentChallenge(e){var d=e.challenge;var a=e.questions;var b=new Challenge(d);var c=true;$j("#quizbox").show();b.display();if(active_challenge&&typeof(active_challenge)=="object"){active_challenge.video.clear();c=true}if(!$j("body").hasClass("home")){c=true}b.setQuestions(a);b.cue(c);b.askQuestion(0);active_challenge=b}function getChallenge(b){var a="/challenges/take?";var c=[];for(key in b){c.push(key+"="+b[key])}a+=c.join("&");Data.get(a,presentChallenge,Data.ContentType.JSON);try{if(pageTracker){pageTracker._trackPageview(a)}}catch(d){}}function popChallenge(d){if($j("#quizbox").hasClass("home")){$j("#quizbox").hide()}var b=getPageScrollTop();var c=Number(b[1])+165;var a=$j("#quizbox").parent().offset();$j("#quizbox").addClass("popup").css({"z-index":ZBASE}).css({top:c+"px",left:a.left+"px"}).show();$j("#quizbox .close").show();$j(".quiz_overlay").show();getChallenge({id:d})}function skipChallenge(){if(!active_challenge){return}getChallenge({skip:active_challenge.challenge.id})};