var MAXQNS=10;var pickVideo=null;var vidchoose={};var ChallengeMaker=new function(){this.video=null;this.initialize=function(){$j("#askqns .hidable").hide();$j("#snippetsearch").submit(function(){var b=null;if($j("#sqt").length>0){b=$j("#sqt")[0].value}if(!b||b.length<=0){return false}var a=null;if(a=b.match(/youtube.com\/watch\?v=([A-Za-z0-9_-]+)/)){b=a[1]}ytSearch(b,ytSearchChoose,"/ytsnippet/");ytSetSearchState("busy");return false});$j(".ctl").click(function(){pressCtl(this)});$j("#challengeform").submit(function(){var b=($j("#scenetitle")[0].value.length!=0);if(!b){notify("Please provide a title");return false}var a=($j("#channel_name")[0].value.length!=0);if(!a){notify("Please choose a channel");return false}$j("#createbusy").show();Data.post("/challenges/create",this,onChallengeCreate);return false});$j(".preview_cancel").click(function(){$j("#previewscene").hide();$j("#playbackctl").show();pickVideo.playBackFull()});setupGuess();setupChoices()};this.show=function(){$j("#sqt").focus()}};function setupGuess(){$j("#guesswhat li").hover(function(){$j(this).addClass("sel")},function(){$j(this).removeClass("sel")});$j("#doguess").click(function(){$j(this).after('<div id="guessoverlay"/>');$j("#guesswhat").show();$j("#guessoverlay").click(function(){$j("#guesswhat").hide();$j("#guessoverlay").remove();$j("#mainmenu").parent().focus()});return false});$j("#guesswhat li").click(function(){readyQuestion(this);return false})}function setupChoices(){$j("#enterchoices").submit(function(){var a=$j("#qnchoice")[0].value;if(a.length<=0){return false}$j("#choicesentered").append('<li class="choiceentry"><div class="remove" onclick="removeChoice(this); return false;"></div><div class="choicetxt" onclick="choiceAnswer(this);return false;"></div></li>');$j("#choicesentered .choicetxt:last").text(a);readyChoiceEntry();ifDoneQuestion();return false})}function ytSetSearchState(a){if(a=="busy"){$j("#ytchoosevid").hide();$j("#choosebusy").show();$j("#ytchoosevid").show()}else{if(a=="ready"){$j("#donechoose").hide();$j("#ytchoosevid").hide();$j("#choosebusy").hide();$j("#chooseagain").show();$j("#vidchoices").show()}else{if(a=="reset"){if($j("#sqt").length>0){$j("#sqt")[0].value=""}$j("#resultinfo2").empty();$j("#chooseagain").hide();$j("#ytchoosevid").show();$j("#vidchoices").hide();$j("#sqt").focus()}else{if(a=="done"){$j("#choosevid .hidable").hide();$j("#donechoose").show()}else{if(a=="error"){$j("#choosebusy").hide()}}}}}}function ytSearchChoose(a){$j("#vidchoices").empty();ytSetSearchState("ready");if(!a||!a.feed){$j("#vidchoices").append(TAG("div",{id:"searcherror","class":"error"},"Error getting YouTube search results"));return false}else{var b=a.feed;var c=ytParseIntoList(b,"vidchoose_link",vidchoose);if(!c){$j("#vidchoices").append(TAG("div",{"class":"notice"},"No embeddable videos matching your search terms"));return false}$j("#vidchoices").append(c);$j("#resultinfo2").html(ytResultInfo(b,"twolines"));$j(".vidchoose_link").click(function(){chooseVideo(vidchoose[this.id]);return false})}adjustOurHeight()}function chooseAnother(){ytSetSearchState("ready");$j("#choosevid").removeClass("done");$j("#choosevid").addClass("active");$j("#pickscene").removeClass("active").removeClass("complete");$j("#pickscene").addClass("next");$j("#askqns").removeClass("active").removeClass("next");$j("#askqns").addClass("minim");$j("#pickctl").hide();$j("#playbackctl").hide();$j("#nowpick").hide();$j("#rechoose").hide();$j("#askqns .hidable").hide();adjustOurHeight()}function resetCreateForm(){chooseAnother();ytSetSearchState("reset");if(pickVideo){pickVideo.clear();pickVideo=null}clearVideoProps();$j("#sceneplay").appendTo("#sceneplay_pickbox");$j("#pickscene .hidable").hide();$j("#recstop").removeClass("stop").removeClass("recording").addClass("record");$j("#recstatus").text("Press to start recording");pressCtl("audvid");readyQuestion();removeQuestion();$j("#scenetags")[0].value="";$j("#seekto")[0].value="0";$j("#playto")[0].value="30";$j("#qnlist").empty();$j("#doguess").hide();$j("#createbusy").hide();adjustOurHeight()}function ytSearch(b,f,a){var e={};var d=(f||ytSearchCallback);var c="vq="+encodeURIComponent(b);Data.post("/ytfind",c,d)}function ytParseIntoList(a,b,e){var g=Data.escape(a.title.$t.split(": ")[1]);var h=a.openSearch$totalResults.$t;var f="";var c=null;if(!a||h==0||!a.entry){return false}for(vx=0;vx<a.entry.length;vx++){if(!a||!a.entry||!a.entry[vx]){continue}var d=a.entry[vx];var i;c=d.id.$t.split("/").pop();e[c]=i={ytid:c,title:d.title.$t,description:d.media$group.media$description.$t,imgurl:d.media$group.media$thumbnail[0].url,imgw:d.media$group.media$thumbnail[0].width,imgh:d.media$group.media$thumbnail[0].height};f+=vidEntry("r"+vx,i,b,false)}return f}function ytResultInfo(a,e){var c=Data.escape(a.title.$t.split(": ")[1]);var b=a.openSearch$totalResults.$t;var d=TAG("span",{"class":"b"},'"'+c+'"')+(e?"<br/>Q":": q")+TAG("span",{},"uery results "+a.openSearch$startIndex.$t+"-"+a.entry.length+" of about "+b);return d}function vidEntry(e,b,a,g,f){var d="";var c=desc_div=cmtrn_div="";if(f){cmtrn_div=f}if(g){c=g}if(!g){desc_div=TAG("div",{"class":"viddesc"},Data.truncate(b.description,78-(b.title.length>37?42:0)))}d=TAG("div",{id:e,"class":"videntry"},TAG("div",{"class":"vidimg"},TAG("IMG",{id:"i"+e,width:"60",src:b.imgurl}))+TAG("div",{"class":"vidtxt"},c+TAG("div",{"class":"vidtitle"},TAG("a",{href:"#",id:b.ytid,"class":a},Data.truncate(b.title,72)))+desc_div)+cmtrn_div);return d}function chooseVideo(a){var b=typeof(a.ytid)=="undefined"?a:a.ytid;pickVideo=new Video(b);pickVideo.setPlayerElement("pickplayer");pickVideo.setPlayBackFull();pickVideo.setSize(320,268);pickVideo.setAutoPlay(true);pickVideo.embed();$j("#snipvid")[0].value=a.ytid;$j("#sniptitle")[0].value=a.title;$j("#pickplayer").focus();$j("#nowpick").show();$j("#vidcap")[0].src=a.imgurl;$j("#vidcap").parent().addClass("thumbsel");$j("#snipimg")[0].value=a.imgurl}function clearVideoProps(){if(pickVideo){pickVideo.resetDisplay()}}function previewScene(){var a=$j("#seekto")[0].value;var c=$j("#playback")[0].value;var b=$j("#playto")[0].value;$j("#playbackctl").hide();$j("#previewscene").show();pickVideo.onCluePlayed(function(){clearVideoProps();$j("#challenge_av").text("");$j("#previewscene").hide();$j("#playbackctl").show();$j("#rechoose").show()});pickVideo.setPlayBackCtl(a,b,c);pickVideo.playBackCtl(true)}function readyPickScene(){$j("#pickctl").show();$j("#playbackctl").hide();$j("#previewscene").hide();$j("#nowpick").hide();$j("#rechoose").show();clearVideoProps();$j("#pickscene").removeClass("complete").addClass("active");$j("#askqns").removeClass("active").addClass("next")}function pickChallenge(){ytSetSearchState("done");$j("#choosevid").removeClass("active");$j("#choosevid").addClass("done");$j("#pickscene").removeClass("next").removeClass("complete");$j("#pickscene").addClass("active");$j("#askqns").removeClass("minim").removeClass("active");$j("#askqns").addClass("next");readyPickScene();$j("#doguess").show();adjustOurHeight()}function recStop(a){$j("#playto")[0].value=pickVideo.getCurrentTime();$j(a).removeClass("stop").addClass("record");$j("#recstatus").text("Press to start recording");$j("#pickctl").hide();$j("#playbackctl").show();$j("#pickscene").removeClass("active").addClass("complete");$j("#askqns").removeClass("next").addClass("active");adjustOurHeight()}function pressCtl(b){var a=b.id;switch(a){case"recstop":if($j(b).hasClass("record")){if(pickVideo.getCurrentState!=1){pickVideo.play()}$j("#seekto")[0].value=pickVideo.getCurrentTime();$j(b).removeClass("record");$j(b).addClass("recording");$j("#recstatus").text("Recording... 5 sec minimum");to_minrec=setTimeout(function(){$j("#"+a).removeClass("recording").addClass("stop");$j("#recstatus").text("Press to stop recording");to_minrec=null},5000)}else{if($j(b).hasClass("stop")){recStop(b)}}break;case"stop":break;case"vidonly":case"audonly":case"audvid":$j(".pbav").removeClass("greenbg");$j(b).addClass("greenbg");$j("#playback")[0].value=a;break}}function readyQuestion(b){var a;if(b){a=$j(b).attr("guess");$j("#theqn").attr("which",a);$j("#theqn").text($j(b).attr("qn"));$j("#theqnremove").show();$j("#doguess").hide();$j("#guessoverlay").click();$j("#challengeform").hide();if($j(b).attr("yourown")=="yes"){$j("#customqntxt")[0].value="";$j("#customqn").show();$j("#customqntxt").focus();$j("#customqn").submit(function(){var c=$j("#customqntxt")[0].value;c=c.replace(QNSEP,"");c=c.replace(CHSEP,"");$j("#theqn").text(Data.escape(c));$j("#theqn").attr("yourown","yes");$j("#customqn").hide();readyChoiceEntry();$j("#qnchoice").focus();return false})}else{$j(b).hide();readyChoiceEntry();$j("#qnchoice").focus()}}else{a=$j("#theqn").attr("which");$j("#theqn").text("");$j("#theqnremove").hide();$j("#doguess").show();$j('#guesswhat li[guess="'+a+'"]').show();readyChoiceEntry("remove");if($j(".qnpanel").length>0){$j("#challengeform").show()}}}function removeChoice(a){$j(a).parent().remove();if($j("#choicesentered .choiceentry").length<4){$j("#donechoices").hide();readyChoiceEntry()}}function readyChoiceEntry(a){if(!a){$j("#addchoices").show();$j("#enterchoices").show();$j("#qnchoice")[0].value="";$j("#qnchoice").focus()}else{$j("#choicesentered").empty();$j("#donechoices").hide();$j("#enterchoices").hide();$j("#addchoices").hide()}adjustOurHeight()}function ifDoneQuestion(){if($j("#choicesentered .choiceentry").length>=4){$j("#enterchoices").hide();if($j("#choicesentered .correctchoice").length<1){$j("#donechoices").show()}else{$j("#donechoices").hide();addQuestion();return true}}return false}function choiceAnswer(a){$j("#choicesentered .choicetxt").removeClass("correctchoice");$j(a).addClass("correctchoice");ifDoneQuestion()}function addQuestion(){var a=$j("#theqn").attr("which");var g=$j("#theqn").attr("yourown");var d=$j(".qnpanel").length;var i='<div id="q'+d+'" class="qnpanel"><div class="qntxt">'+$j("#theqn").text()+'</div><div class="remove" onclick="removeQuestion('+a+", "+d+')"></div><div style="clear:both;"/></div>';$j("#qnlist").append(i);var j=[];var b="";var h=0;var k=-1;var e=false;var c="";$j("#choicesentered .choiceentry").each(function(l){e=$j(this).children(".choicetxt").hasClass("correctchoice");c=Data.escape($j(this).children(".choicetxt").text());if(e){k=h}j.push(c);b+='<div class="qnachoice'+(e?" correctchoice":"")+'">('+choicenames[h]+") "+c+"</div>";h+=1});var f=(g=="yes"?$j("#theqn").text():a);f+=QNSEP+j.join(CHSEP)+QNSEP+k;f=encodeURIComponent(f);$j("#challengeform").append('<input id="qc'+d+'" class="qnchform" type="hidden" name="questions['+d+']" value="'+f+'"/>');$j("#q"+d).append(b);$j("#choicesentered").empty();$j("#askqns .hidable").hide();if(d<MAXQNS){$j("#doguess").show()}$j("#doguess").text("Add another question");$j("#doguess").removeClass("button").removeClass("bluebg").addClass("optionlink");$j("#challengeform").show();adjustOurHeight()}function removeQuestion(b,a){if(!b){$j(".qnpanel").remove();$j(".qnchform").remove();$j("#guesswhat li").show()}else{if(typeof(a)==="undefined"||a===null){a=b}$j("#q"+a).remove();$j("#qc"+a).remove();$j('#guesswhat li[guess="'+b+'"]').show()}if($j(".qnpanel").length<=0){$j("#challengeform").hide();$j("#doguess").text("Add a question");$j("#doguess").removeClass("optionlink").addClass("button").addClass("bluebg")}}function onChallengeCreate(a){$j("#createbusy").hide();if(!a||!a.success){notify(a.message||"Problem creating challenge")}else{var b=a.challenge_id;notify("Your challenge has been submitted.");window.location.href=a.redirect_url}}function notify(a){$j("#challenge_error").text(a).show().fadeOut(7000)}function adjustOurHeight(){return false};