(function(b){AT.SurveyChoice=AT.Model.create(),AT.Survey=AT.Model.create({init:function(a){this.updateData(a||{survey:{choices:[]}})},config:{json_root:"survey",contains_models:{choices:[AT.SurveyChoice]}},instance:{vote:function(a,c){this.waitingForServer||(this.waitingForServer=!0,b.ajax({url:AT.config.proxy+"/surveys/"+this.get("id")+"/vote.json",dataType:"json",type:"POST",data:{position:a},success:this.ajaxSuccessCallback(c),error:this.ajaxErrorCallback(c),statusCode:{405:function(){b("#survey_"+id).html("<p>We're very sorry, but the survey system is undergoing maintanence. Please try back in a few minutes.<p>")}}}))}},klass:{retrieve:function(a,c){var d=new AT.Survey;b.ajax({url:AT.config.proxy+"/surveys/"+a+".json",dataType:"json",type:"GET",success:d.ajaxSuccessCallback(c),error:d.ajaxErrorCallback(c),statusCode:{503:function(){b("#survey_"+a).html("<p>We're very sorry, but the survey system is undergoing maintanence. Please try back in a few minutes.<p>")}}})}}}),AT.SurveyController=function(a,c,d){this.wrapper=b(c),d&&(this.opts=d),this.dom=new AT.SurveyController.Dom(this),AT.Survey.retrieve(a,{success:_(function(a){this.survey=a,this.render()}).bind(this),error:_(function(a){this.wrapper.html("<p>There was an error retrieving the survey.  Please try again later</p>")}).bind(this)})},AT.SurveyController.Dom=function(a){this.controller=a},AT.SurveyController.Dom.prototype={voteButton:function(){return this.controller.wrapper.find('.survey_form form input[type="submit"]')},showResultsButton:function(){return this.controller.wrapper.find('.survey_form [name="show_results"]:button')},resultsSection:function(){return this.controller.wrapper.find(".survey_results")},voteForm:function(){return this.controller.wrapper.find(".survey_form")},answerRadios:function(){return this.controller.wrapper.find(".survey_form form :radio")},render:function(){var a=this.controller.survey;this.controller.wrapper.addClass("survey"),this.controller.wrapper.append("<strong>"+a.get("question")+"</strong>"),this.controller.wrapper.append(Jaml.render("survey_form",a)),this.controller.wrapper.append(Jaml.render("survey_results",a))}},AT.SurveyController.prototype={render:function(){this.dom.render(),this.dom.voteButton().attr("disabled","disabled"),this.dom.voteForm().find("form").submit(_(this.handleVoteSubmission).bind(this)),this.dom.showResultsButton().click(_(this.handleShowResults).bind(this)),this.dom.answerRadios().click(_(function(){this.dom.voteButton().removeAttr("disabled")}).bind(this));var a;this.opts&&this.opts.state&&_.isString(this.opts.state)?a=this.opts.state:this.survey.get("closed")?a="closed":this.survey.get("user_logged_in")?this.survey.get("user_voted")?a="voted":a="not_voted":a="not_logged_in",this.updateState(a)},updateState:function(a){switch(a){case"not_logged_in":this.dom.answerRadios().attr("disabled","disabled"),this.dom.showResultsButton().filter(":visible").length>0&&this.dom.resultsSection().hide(),this.dom.voteForm().after(Jaml.render("survey_login_message"));break;case"not_voted":this.dom.answerRadios().removeAttr("disabled"),this.dom.showResultsButton().filter(":visible").length>0&&this.dom.resultsSection().hide();break;case"voted":this.dom.voteForm().hide();break;case"closed":this.dom.voteForm().hide(),this.dom.resultsSection().after("<p>This survey has ended.<p>")}},handleVoteSubmission:function(a){a.preventDefault();var c=b('form input[name="position"]:checked').val();this.survey.vote(c,{success:_(function(a){this.dom.resultsSection().replaceWith(Jaml.render("survey_results",a)),this.updateState("voted"),this.dom.resultsSection().before("<p>Thank you, your survey answer has been recorded.</p>")}).bind(this),error:_(function(a){this.dom.voteForm().before("<p>An error prevented us from casting your vote.  Please try again later.</p>")}).bind(this)})},handleShowResults:function(a){this.dom.resultsSection().show(),this.dom.showResultsButton().hide()}},Jaml.register("survey_results",function(a){div({cls:"survey_results"},table(Jaml.render("survey_choice_result",a.get("choices"))))}),Jaml.register("survey_choice_result",function(a){tr(td(a.get("answer")),td(div({cls:"bar"},div({style:"width: "+a.get("percentage")+"%"}))),td(a.get("votes").toString()))}),Jaml.register("survey_form",function(a){div({cls:"survey_form"},form({method:"POST",action:""},ol(Jaml.render("survey_form_choice",a.get("choices"))),input({type:"submit",value:"answer survey"})),input({type:"button",name:"show_results",value:"show results"}))}),Jaml.register("survey_form_choice",function(a){id="survey_position"+a.get("position"),li(input({type:"radio",id:id,name:"position",value:a.get("position")}),label({"for":id},a.get("answer")))}),Jaml.register("survey_login_message",function(){p("To take the survey",a({href:AT.config.host+"/sign_in?sign_out_first=1&return_to="+encodeURIComponent(window.location.href)},"sign in"),". If you do not have an account, please",a({href:AT.config.host+"/sign_up?return_to="+encodeURIComponent(window.location.href)},"register"),".")}),b(function(){b(".survey_container").each(function(){var a=b(this).attr("id").substr(7);new AT.SurveyController(a,b(this))})})})(jQuery)
