var edit_mode=false;function answer_question(a){if(EDIT_MODE){return}answer_id=a.getProperty("rel");if(answer_id){location.replace("/quiz/process-answer/?a="+answer_id)}}function addAnswer(){if($$(".questionAnswers").length==5){alert("5 answers is the limit.");return false}var a=$("cloneMe").clone();a.inject($("newQHere"),"after");new Element("br").addClass("clear-both").inject(a,"before")}function deleteQuiz(a){conf=confirm("Delete!?? Are you sure?  This cannot be undone!");if(conf){document.location=a}}window.addEvent("domready",function(){$(document.body).getElements(".js-answer-btn").each(function(a){a.addEvent("click",function(){answer_question(this)})})});function startQuizWizard(a){$("submitBtn").removeClass("dn");$("quizForm").addEvent("submit",function(c){event=Event(c).stop();var b=false;switch(a){case 1:var b=submitStepOne();break;case 2:var b=submitStepTwo();break;case 3:var b=submitStepThree();break}if(b){$("quizForm").submit()}else{return false}})}function submitStepOne(){var a=false;var b=$("quizTitle").value;if(b.trim()==""){a="You need a title.\n"}var b=$("quizDescription").value;if(b.trim()==""){a+="You need a description.\n"}if($("single-image-widget").getElement("input").value==""){a+="You need an image.\n"}if(a){alert(a);return false}else{return true}}function submitStepTwo(){var a=0;$(document.body).getElements(".js-objects").each(function(b){if(b.getProperty("checked")==true){a++}});if(a<5){alert("Please pick at least 5");return false}else{return true}}function submitStepThree(){var a=0;var c=0;$(document.body).getElements(".questionAnswers").each(function(d){var e=d.value;if(e.trim()!=""){c++}});var b=$("quizQuestion").value;if(b.trim()!=""){a++}if(a==0||c<2){alert("Make sure you have one question and at least 2 answers.");return false}else{return true}}function makeStringPostSafe(a){return a.replace(/;/g,"%3B").replace(/&/g,"%26")}var EDIT_MODE=false;EditQuiz=new Class({initialize:function(){if(EDIT_MODE){return}EDIT_MODE=true;this.matchManagerReady=false;vinesLoadingMask(true);this.quizIsPublished=false;if($("quiz_is_published").value==1){this.quizIsPublished=true}this.questions=$("js-questions");this.menu=$("js-content-objects-nav");this.accordion=new Accordion($("js-questions"),"span.js-question-span","div.js-answer-container",{opacity:false,onActive:function(c,b){c.setStyle("color","#41464D");if(Browser.Engine.trident){$$(".js-objects-wrapper").each(function(d){d.setStyle("visibility","hidden")});b.getElement(".js-objects-wrapper").setStyle("visibility","visible")}var a=this.get_question(c);a.getElement(".js-question-span").addClass("dn");a.getElement(".js-question-input").removeClass("dn");this.currentAnswerId=a.getElement(".js-answers-ul").getFirst().getProperty("rel");this.select_answer()}.bind(this),onBackground:function(b,a){b.setStyle("color","#528CE0");var c=b.getParent();c.getElement(".js-objects-wrapper").setStyles({position:"relative",top:"0"});c.getElement(".js-question-span").removeClass("dn");c.getElement(".js-question-input").addClass("dn")}.bind(this)});this.questions.getElements("li.js-question-container").each(function(a){this.add_question_details(a)}.bind(this));this.questions.getElements("li.js-answer-li").each(function(a){this.add_answer_details(a)}.bind(this));this.match_manager();this.menu.getElements(".js-description").each(function(a){this.setToggleDescription(a)}.bind(this));this.menu.getElements(".js-delete-object").each(function(a){this.manage_delete_object(a)}.bind(this));this.init_search();this.validate_questions();this.validate_descriptions();this.sortQuestions=new Sortables("js-questions",{handle:".drag-answer",opacity:"0.6",clone:true});this.questions.getElements(".js-add-answer").each(function(a){this.add_event_answer_button(a)}.bind(this));$("js-add-question").removeClass("dn").addEvent("click",function(a){this.create_questions(a)}.bind(this));$(document.body).getElements(".js-save").each(function(a){a.removeClass("dn").addEvent("click",function(b){new Event(b).stop();this.save()}.bind(this))}.bind(this));$(document.body).getElements(".js-publish").each(function(a){a.removeClass("dn").addEvent("click",function(b){new Event(b).stop();this.save(true)}.bind(this))}.bind(this));vinesLoadingMask(false)},setToggleDescription:function(a){a.removeEvent("click");a.addEvent("click",function(d){Event(d).stop();var f=a.getParents();var c=false;f.each(function(e){if(!c&&e.getTag()=="tr"){c=e}});var b=c.getElement("textarea");b.toggleClass("dn")})},create_questions:function(h){event=Event(h).stop();var j="new_"+Math.floor(Math.random()*2000+1);var d=$("js-questions");if(d.getChildren().length==20){alert("20 questions is the limit, bub.");return}var m=new Element("span").addClass("js-question-span").set("html","Write a question....");var l=new Element("input").setProperty("type","text").addClass("js-question-input");var n=new Element("li").addClass("js-question-container").setProperty("id",j).inject(d).adopt(m,l);str="";str+='<span class="js-needs-editing f-11 red dn">Needs editing.</span><br>';str+='<div class="js-answer-container" style="border-top: medium none; border-bottom: medium none; overflow: hidden; padding-top: 0px; padding-bottom: 0px; height: 0px;">';str+='    <div class="fl" style="width:400px">';str+='    <ul class="trivia js-answers-ul"></ul>';str+='    <span class="fr mgray f-11 js-add-answer">Add Another Answer</span>';str+="</div>";str+='<div class="js-objects-wrapper fl">';str+='        <div class="corner menu-top">';str+="            <span>Click to Match to Answer</span>";str+="        </div>";str+='        <div class="js-content-objects"></div>';str+="    </div>";str+='    <br class="clear-both">';str+="</div>";n.innerHTML=n.innerHTML+str;var f=d.getChildren().length;var c=n.getElement(".js-question-span");var k=n.getElement(".js-answer-container");var o=this.create_answer(n);this.add_answer_details(o);var o=this.create_answer(n);this.add_answer_details(o);var o=this.create_answer(n);this.add_answer_details(o);$("js-every-quiz-ref").getChildren().each(function(a){var e=a.clone();e.id=a.id;e.inject(n.getElement(".js-content-objects"));this.make_matchable(e)}.bind(this));this.accordion.addSection(c,k,f-1);var m=n.getElement(".js-question-span");var l=n.getElement(".js-question-input");var b=n.getElement(".js-answer-container");l.inject(b,"before");n.getElement(".js-needs-editing").inject(b,"before");var g=f-1;this.accordion.display(g);this.add_question_details(n);this.add_event_answer_button(n.getElement(".js-add-answer"));this.validate_question(n)},add_event_answer_button:function(a){a.addEvent("click",function(b){this.create_answer(a)}.bind(this))},create_answer:function(b){var g="new_"+Math.floor(Math.random()*2000+1);var d=this.get_question(b);var e=d.getElement("ul.js-answers-ul");if(e.getChildren().length==10){alert("10 answers is the limit, bub.");return}var h=new Element("li").setProperty("rel",g).addClass("js-answer-li").inject(e);var f=new Element("input").setProperty("type","text").setProperty("id",g).inject(h,"bottom");var a=new Element("div").setProperty("id","answer_objects"+g).addClass("w-300").inject(h,"bottom");var i=new Element("br").addClass("clear-both").inject(h,"bottom");this.add_answer_details(h);var d=this.get_question(b);var c=d.getElement(".js-answer-container");c.setStyle("height",c.innerHeight);return h},select_answer:function(){if(!this.currentAnswerId){return}var a=$(this.currentAnswerId);var c=a.getParent();var b=c.getParent();b.getChildren().each(function(d){if(d!=c){d.setStyle("opacity",".5")}else{d.setStyle("opacity","1")}})},match_manager:function(){$$("img.matchable").each(function(a){this.make_matchable(a)}.bind(this))},make_matchable:function(a){a.addEvent("click",function(f){new Event(f).stop();var d=this.get_question(a);if(a.getParent().hasClass("js-content-objects")){if(this.currentAnswerId){var c=d.getElement("#answer_objects"+this.currentAnswerId);a.inject(c)}}else{a.inject(d.getElement(".js-content-objects"))}var b=d.getElement(".js-answer-container");b.setStyle("height",b.innerHeight);this.validate_matched(d)}.bind(this))},manage_delete_object:function(b){var c=b.getParent("tr");var a=c.getElement("textarea");var d=a.id.replace("desc","");b.addEvent("click",function(){conf=confirm("Delete this?  Are you sure, you cannot undo this.");if(!conf){return}c.destroy();$$("img.matchable").each(function(e){if(e.id==d){e.destroy()}});this.validate_questions()}.bind(this))},add_answer_details:function(a){if(a.getElement(".js-delete")){a.getElement(".js-delete").destroy()}a.addEvent("mouseover",function(){var k=a.getCoordinates();var h=a.getParent().getCoordinates();var j=k.top-h.top;var f=this.get_question(a);var i=f.getElement(".js-objects-wrapper");var e=f.getElement(".js-answer-container");var g=e.getCoordinates().width-400;i.setStyles({top:j,position:"relative",width:g});if(e.getCoordinates().bottom<i.getCoordinates().bottom){var d=j+i.getCoordinates().height;e.setStyle("height",d)}else{e.setStyle("height",e.innerHeight)}this.currentAnswerId=a.getProperty("rel");this.select_answer()}.bind(this));new Element("span").set("html","delete").inject(a,"top").addClass("js-delete f-10 mgray").setStyle("float","right").addEvent("click",function(f,d){new Event(f).stop();conf=confirm("Remove? Cannot be undone.");if(!conf){return}this.reset_content_objects(d);d.remove()}.bindWithEvent(this,a));var c=this.get_question(a);var b=a.getElement("input");b.addEvent("change",function(){this.validate_question(c)}.bind(this))},add_question_details:function(a){if(a.getElement(".js-delete")){a.getElement(".js-delete").destroy()}if(a.getElement(".drag-answer")){a.getElement(".drag-answer").destroy()}new Element("span").set("html","delete -&nbsp;").inject(a,"top").addClass("js-delete f-10 mgray").setStyle("float","right").addEvent("click",function(g,f){new Event(g).stop();conf=confirm("Remove? Cannot be undone.");if(!conf){return}f.remove()}.bindWithEvent(this,a));new Element("span").set("html"," drag ").addClass("drag-answer f-10 mgray").setStyles({"float":"right"}).inject(a,"top");var c=a;var b=a.getElement("input.js-question-input");var d=a.getElement("span.js-question-span");b.addEvent("change",function(){this.validate_question(c)}.bind(this)).addEvent("keyup",function(){d.set("html",b.value)})},get_question:function(a){if(a.hasClass("js-question-container")){return a}var c=a.getParents();var b=false;c.each(function(d){if(d.hasClass("js-question-container")){b=d}});return b},reset_content_objects:function(c){var a=this.get_question(c);var b=a.getElement(".js-content-objects");c.getElements(".matchable").each(function(d){d.inject(b)});this.validate_matched(a)},validate_descriptions:function(){$(document.body).getElements(".js-textarea").each(function(a){this.validate_desc(a)}.bind(this))},validate_desc:function(a){var e=$(a).getParents();var b=false;e.each(function(f){if(!b&&f.getTag()=="tr"){b=f}});var c=b.getElement(".js-needs-editing");if(!c){clog("needsEdit desc fail");return}var d=a.value;if(d.trim()==""){c.removeClass("dn")}else{c.addClass("dn")}a.addEvent("change",function(){this.validate_desc(a)}.bind(this))},validate_matched:function(a){var c=a.getElement(".js-objects-wrapper");var b=a.getElement(".js-content-objects");b.getChildren().length>0?c.removeClass("dn"):c.addClass("dn");this.validate_question(a)},validate_questions:function(){this.questions.getChildren().each(function(a){this.validate_matched(a)}.bind(this))},validate_question:function(b){var c=false;b.getElements("li.js-answer-li").each(function(e){var g=e.getProperty("rel");var f=e.getElement("#answer_objects"+g);if(!f.getFirst()){c=true}});b.getElements("input").each(function(e){var f=e.value;if(f.trim()==""){needs_edting=true}});var d=b.getElement("input.js-question-input").value;if(d.trim()==""){c=true}var a=b.getElement(".js-needs-editing");c?a.removeClass("dn"):a.addClass("dn")},init_search:function(){var b=$("js-content-object-search");var a=b.getProperty("rel");new VinesSearchAC.Base(b,"/autosearch/?ct_ids="+a,{callBackEvent:"choiceSelected"});b.addEvent("choiceSelected",function(l,h){this.dupe=false;$("js-every-quiz-ref").getElements("img").each(function(e){if(e.id==l.object_id){alert("Oops, you already have "+l.name);this.dupe=true}}.bind(this));if(this.dupe){return false}var g=$("js-content-objects-nav").getElements("tr");var i=g[1].clone();i.getElement("a").set("html",l.name);i.getElement("img").src=l.image_url;var j=i.getElement(".js-description");var d=i.getElement(".js-delete-object");var k=i.getElement("textarea");k.id="desc"+l.object_id;k.value="";i.inject(g[0],"after");this.validate_desc(k);this.setToggleDescription(j);var c=l.image_url;c=c.replace(/_tiny./gi,"_small.");var f=new Element("img").setProperties({title:l.name,src:c}).addClass("matchable").setProperty("id",l.object_id).inject($("js-every-quiz-ref"));$(document.body).getElements(".js-content-objects").each(function(m){var n=f.clone();n.inject(m).setProperty("id",l.object_id);this.make_matchable(n);var e=this.get_question(m);this.validate_matched(e)}.bind(this));this.manage_delete_object(d)}.bind(this))},save:function(b){vinesLoadingMask(true);publish=false;this.unpublish=false;if(b){if(this.quizIsPublished){publish=false;this.unpublish=true}else{publish=true}}needsHelp=false;$(document.body).getElements(".js-needs-editing").each(function(g){if(!g.hasClass("dn")){needsHelp=true}});if(needsHelp&&publish){alert("Whoops!  Cannot publish yet.  There are things that still need editing!");vinesLoadingMask(false);return false}else{if(needsHelp&&this.quizIsPublished){var a=confirm("Saving now is ok, but your quiz will be unpublished until you fix all places where you need editing.");if(!a){return false}publish=false}}var d=[];$("js-questions").getChildren().each(function(h){var g=h.id;var i=h.getElement("input").value;this.answers=[];$(h).getElements(".js-answer-li").each(function(l){var j=l.getElement("input");var k=[];l.getElements(".matchable").each(function(m){k.include(m.id)});this.answers.include({answer_id:j.id,title:makeStringPostSafe(j.value),content_objects:k})}.bind(this));d.include({question_id:g,title:makeStringPostSafe(i),answers:this.answers})}.bind(this));var c=[];$("js-every-quiz-ref").getChildren().each(function(g){var h=g.id;var i=$("desc"+h).value;c.include({object_id:h,desc:makeStringPostSafe(i)})}.bind(this));if($("single-image-widget")){var e=$("single-image-widget").getElement("input").value}postdata={image_id:e,quiz_id:$("quiz_id").value,quiz_title:$("quiz_title").value,questions:d,content_objects:c,publish:publish};clog(postdata);var f=new Request.JSON({url:"/quiz/save/",method:"post",onSuccess:function(g){vinesLoadingMask(false);if(this.unpublish){var j=false;var k="Unpublished";var i="<a href='.'>Make more edits?</a>"}else{if(g.published){var j=false;var k="Published!";var i="<a href='.'>Make more edits</a> OR <a href='"+g.url+"'>take your quiz.</a>"}else{var j=false;var k="Saved";var i="Awesome your edits have been saved!.\n<a href='.'>Make more edits</a>"}}var h=new LaunchPad.Base({closeIcon:j,titleTextStr:k,className:"friend-box"}).insert(i)}.bind(this),onFailure:function(){vinesLoadingMask(false)}}).send(postdata)}});function edit_quiz(){var a=new EditQuiz()};