window.addEvent("domready",function(){CommentManager.initialize()});var CommentManager={initialize:function(){this.notesState=$("notesState");this.notesControl=$("notesControl");this.notesDiv=$("notesDiv");this.previewBtn=$("comment_preview");this.preview=$("previewNote");this.form=$("addCommentForm");this.formTitleId="addCommentTitle";this.formTitle=$(this.formTitleId);this.formTextId="comment_content";this.formText=$(this.formTextId);when($("wikiCommentsContainer"),function(a){this.container=a}.bind(this));when($("guideCommentsContainer"),function(a){this.container=a}.bind(this));this.addFormEvents();this.addNoteDivActions(this.notesDiv);this.notesControl.addEvent("click",function(a){new Event(a).stop();this.toggleNotes(!Number(this.notesControl.getProperty("data-notes-active")))}.bind(this))},toggleNotes:function(a){this.preview.set("html","");if(a){$("commentsLoading").show();this.noteLoading.loading("Loading comments...");new Request.AjaxIO("showPage",{onSuccess:function(b){this.noteLoading.doneLoading();$("commentsLoading").hide();this.notesDiv.set("html",b.html);this.addNoteDivActions(this.notesDiv);this.notesState.setProperty("text","on");this.notesControl.setProperties({text:"turn off","data-notes-active":"1",href:"#off"})}.bind(this)}).send("1")}else{this.notesDiv.set("html","");new Request.AjaxIO("showPage",{onSuccess:function(b){this.notesState.setProperty("text","off");this.notesControl.setProperties({text:"turn on","data-notes-active":"0",href:"#on"})}.bind(this)}).send("0")}},addFormEvents:function(){this.loadingStatus=new LoadingStatus(this.form);this.formManager=new FormManager(this.form);this.noteLoading=new LoadingStatus(this.container);this.formManager.required(this.formTextId);this.formManager.addSubmitHandler(function(){Auth.required({message:"You must be logged in to add a new comment",onAuthorize:function(){this.loadingStatus.loading("Adding note...");new Request.AjaxIO("addComment",{onSuccess:function(a){this.loadingStatus.doneLoading();if(a.success){this.toggleNotes(true);this.formText.set("value","");this.notesDiv.set("html",a.html);this.addNoteDivActions(this.notesDiv);new Fx.Scroll(window).toElement("c"+a.commentid)}else{this.formManager.showError(this.formTextId,"Comment not added. Was this a double post? If not, please refresh the page and try again.")}}.bind(this)}).send(this.formText.value,this.context,this.contextid,this.parentid)}.bind(this)})}.bind(this));this.previewBtn.addEvent("click",function(a){new Event(a).stop().preventDefault();this.formManager.validate(function(){this.loadingStatus.loading("Generating preview...");new Request.AjaxIO("previewComment",{onSuccess:function(b){this.preview.set("html",b.html);this.loadingStatus.doneLoading()}.bind(this)}).send(this.formText.value,this.context,this.contextid)}.bind(this))}.bind(this));$("syntaxToggle").addEvent("click",function(a){new Event(a).stop().preventDefault();$("formatting").toggle()});when($("guideSteps"),function(a){this.addShowFormEvents(a)}.bind(this));when($("notesHeader"),function(a){this.addShowFormEvents(a)}.bind(this));when($("toggleDeletedComments"),function(a){a.addEvent("click",function(b){new Event(b).stop().preventDefault();var e="View Active";var d="viewDeleted";var c="deleted";if(a.get("data-mode")!="active"){e="View Deleted";d="showPage";c="active"}new Request.AjaxIO(d,{onSuccess:function(f){this.toggleNotes(true);this.notesDiv.set("html",f.html);this.addNoteDivActions(this.notesDiv)}.bind(this)}).send("1");a.set("html",e);a.set("data-mode",c)}.bind(this))}.bind(this));window.onbeforeunload=function(){if(this.formText.value.trim()!=""){return"Your comments have not yet been posted. If you continue, your changes will be lost."}}.bind(this)},addShowFormEvents:function(a){a.getElements(".addNote").each(function(b){b.addEvent("click",function(c){new Event(c).stop().preventDefault();this.showForm(b)}.bind(this))}.bind(this))},addNoteDivActions:function(a){this.addShowFormEvents(a);Icons.addEvents(this.notesDiv);$$(".deleteNote").each(function(b){b.addEvent("click",function(c){new Event(c).stop().preventDefault();Auth.required({message:"You must be logged in to delete a comment",onAuthorize:function(){new Request.AjaxIO("delComment",{onSuccess:function(){var d=$("c"+b.get("data-commentid")).getParent(".noteTextDiv");if(d){d.destroy()}}}).send(b.get("data-commentid"))}})})});$$(".unDeleteNote").each(function(b){b.addEvent("click",function(c){new Event(c).stop().preventDefault();Auth.required({message:"You must be logged in to restore a comment",onAuthorize:function(){new Request.AjaxIO("unDelComment",{onSuccess:function(){var d=$("c"+b.get("data-commentid"));if(d){d.destroy()}}}).send(b.get("data-commentid"))}})})});$$(".flagNote").each(function(b){b.addEvent("click",function(c){new Event(c).stop().preventDefault();new Request.AjaxIO("flagInappropriate",{onSuccess:function(){new Element("span",{html:"notification sent",styles:{color:"#999","font-style":"italic"}}).replaces(b)}}).send(b.get("data-commentid"))})})},showForm:function(c){var b=c.get("data-context");var a=c.get("data-contextid");var d=c.get("title");if(b==null||a==null||d==null){this.context=this.contextid=this.title=this.parentid=null;return}this.context=b;this.contextid=a;this.parentid=c.get("data-parentid");when(this.formTitle,function(e){e.set("html",d)}.bind(this));this.form.show();this.formManager.clear(this.formText);new Fx.Scroll(window).toElement(this.form).chain(function(){this.formText.focus()}.bind(this))}};
