jQuery.fn.extend({scrollTo:function(speed,easing){return this.each(function(){var targetOffset=$(this).offset().top;$("html,body").animate({scrollTop:targetOffset},speed,easing)})}});var Comment=function(){var self=this;this.selectors={template:"comment_template",error:".error-inline"};this.initialize=function(element,is_new){this.els={};this.els.li=element;this.els.replies=this.els.li.children(".replies");this.els.replies_counter=this.els.li.find(".reply-count:first");this.els.replies_block=this.els.li.find(".replies-block:first");this.id=this.els.li.attr("cid");this.addEvents();if(is_new==1||is_new==2){userbar_message("action",is_new==1?"post_comment":"comment_game")}};this.show=function(){this.els.li.removeClass("hidden")};this.addEvents=function(){this.els.li.find(".comment-edit:first > a, .edit-form:first a").click(this.toggleEditForm);this.els.li.find(".edit-form").submit(this.submitEdit);this.els.li.find(".comment-delete:first").click(this.submitDelete);this.els.li.find(".comment-reply:first > a, .reply-form:first a").click(this.toggleReplyForm);this.els.li.find(".reply-form:first").submit(this.submitReply);this.els.li.find(".replies-block:first > a").click(this.toggleReplies);this.els.li.find(".report > a").click(this.submitReport);this.els.li.find(" > a#more_comments").click(this.moreComment)};this.moreComment=function(e){if($("#more_comments").attr("disabled")=="disabled"){return false}$("#more_comments").html("<img src='/static/images/ajax-loader.gif' style='vertical-align: middle;' alt='' />");$("#more_comments").attr("disabled","disabled");$.getJSON("/comments/show_next/",{"paging-info":$("#hid_comment_paging_info").val(),comment_id:$("#hid_comment_id").val()},function(a){last_li=$("#hid_comment_id").parent().prev();last_li.after(a.html);lis=last_li.nextAll().filter(".comment-entry");lis.each(function(x,li){comments.addElement(li)});$("#hid_comment_id").val($("li.comment-entry:last").attr("cid"));if(lis.length==0){$("#more_comments").html("");$("#hid_comment_id").parent().remove()}else{if(a.paging_text==""){$("#more_comments").html("");$("#hid_comment_id").parent().remove()}else{$("#more_comments").html(a.paging_text);x=$("#hid_comment_paging_info").val();arr=x.split("-");$("#hid_comment_paging_info").val(arr[0]+"-"+arr[1]+"-"+a.page_no)}}$("#more_comments").removeAttr("disabled")})};this.updateBody=function(text){this.els.li.find("p:first").html(text)};this.isDeleted=function(message){this.els.li.addClass("deleted-comment");this.els.li.children("div:first").addClass("deleted-block");this.els.li.find("p:first").html(message);this.els.li.find(".reply-flag-wrap:first").addClass("hidden");this.els.li.find(".comment-author-level:first, .comment-date:first").css("display","none")};this.toggleEditForm=function(e){self.els.li.find(".comment-edit:first > a").text(self.els.li.find(".edit-form:first").hasClass("hidden")?"Cancel":"Edit");self.els.li.find(".edit-form:first").toggleClass("hidden");self.els.li.find(".reply-form:first").addClass("hidden");self.els.li.find(".comment-reply:first > a").text("Reply");return false};this.submitEdit=function(e){var form=self.els.li.find(".edit-form:first");var textarea=form.find("textarea:first");if(textarea.val()==""){self.showFailNote("A body is required.",".edit-form");return false}self.els.li.find(".edit-form fieldset img:first").toggleClass("hidden");self.els.li.find(".edit-form fieldset button:first").addClass("disabled");self.els.li.find(".edit-form fieldset button:first").attr("disabled",true);$.post(form.attr("action"),{id:self.id,body:textarea.val(),object_type:comments.obj.type},function(a){self.els.li.find(".edit-form fieldset img:first").toggleClass("hidden");self.els.li.find(".edit-form fieldset button:first").removeClass("disabled");self.els.li.find(".edit-form fieldset button:first").attr("disabled",false);if(a.result=="success"){self.updateBody(a.body);self.toggleEditForm(false);self.showSuccessNote(a.note)}else{self.showFailNote(a.note,".edit-form")}},"json");return false};this.submitDelete=function(e){if(!confirm("Are you sure you want to delete this comment? This cannot be undone.")){return false}$.post("/comments/delete/",{id:self.id,object_type:comments.obj.type},function(a){if(a.result=="success"){self.isDeleted(a.note);self.hideNotes();self.hideForms();self.els.replies_block.addClass("hidden");self.els.replies.addClass("hidden");if(a.parent!=0){self.updateParentReplyCount(a.parent,a.sibling_count)}comments.updateCount(-a.delete_count)}else{alert(a.note)}},"json");return false};this.submitReport=function(e){$.post("/misc/flag/",{object_type:"Comment_Model",object_id:self.id},function(a){$(e.target).parent().html('<div class="comment-report report">'+a.note+"</div>")},"json");return false};this.toggleReplies=function(e){if(self.isRequestNeeded()){self.requestReplies()}else{self.els.replies.toggleClass("hidden")}return false};this.showReplies=function(){this.els.li.children(".replies:first").removeClass("hidden")};this.isRequestNeeded=function(){return parseInt(self.els.replies_counter.html())>self.els.replies.children().length};this.requestReplies=function(){this.toggleLoading();$.getJSON("/comments/get_replies/"+self.id,{},function(a){self.injectReplies(a.html);self.showReplies();self.toggleLoading()})};this.toggleLoading=function(){this.els.li.find(".replies-block:first img").toggleClass("hidden")};this.hideForms=function(){this.els.li.find(".edit-form").addClass("hidden");this.els.li.find(".reply-form").addClass("hidden")};this.toggleReplyForm=function(e){self.els.li.find(".comment-reply > a").text(self.els.li.children().children(".reply-form").hasClass("hidden")?"Cancel":"Reply");self.els.li.children().children(".reply-form").toggleClass("hidden");self.els.li.find(".edit-form").addClass("hidden");self.els.li.find(".comment-edit > a").text("Edit");return false};this.submitReply=function(e){var form=self.els.li.children().children(".reply-form");var textarea=form.find("textarea");if(textarea.val()==""){self.showFailNote("A body is required.",".reply-form");return false}self.els.li.find(".reply-form fieldset img:first").toggleClass("hidden");self.els.li.find(".reply-form fieldset button:first").addClass("disabled");self.els.li.find(".reply-form fieldset button:first").attr("disabled",true);$.post(form.attr("action"),{id:self.id,body:textarea.val(),object_id:comments.obj.id,object_type:comments.obj.type,parent_id:self.id,get_children:self.isRequestNeeded()},function(a){self.els.li.find(".reply-form fieldset img:first").toggleClass("hidden");self.els.li.find(".reply-form fieldset button:first").removeClass("disabled");self.els.li.find(".reply-form fieldset button:first").attr("disabled",false);if(a.result=="success"){self[self.isRequestNeeded()?"injectReplies":"injectReply"](a.html,a.wow);comments.comments[self.els.replies.find("li:first").attr("cid")].showSuccessNote(a.note);self.toggleReplyForm();textarea.val("");self.showReplies();comments.updateCount(1)}else{self.showFailNote(a.note,".reply-form")}},"json");return false};this.injectReply=function(html,is_new){this.els.replies.prepend(html);this.updateReplyCount();comments.addElement(this.els.replies.children("li:first"),is_new)};this.injectReplies=function(html){this.els.replies.html(html);this.els.replies.find("li").each(function(x,li){comments.addElement(li)});this.updateReplyCount();this.showReplies()};this.updateReplyCount=function(){var count=this.els.replies.children().length;var deleted_count=this.els.replies.children(".deleted-comment").length;count=count-deleted_count;if(count==0){return false}$(this.els.li.find(".reply-count")[0]).html(count==1?"1 reply":count+" replies");if(count>=1){$(this.els.li.find(".replies-block")[0]).removeClass("hidden")}};this.updateParentReplyCount=function(comment_id,child_count){if(child_count>0){$($('.comment-entry[cid="'+comment_id+'"]').find(".reply-count")[0]).html(child_count==1?"1 reply":child_count+" replies")}else{$($('.comment-entry[cid="'+comment_id+'"]').find(".reply-count")[0]).html("0 reply")}};this.showSuccessNote=function(note){this.hideNotes();this.els.li.children(".orange-note").html(note).removeClass("hidden");$(".active").removeClass("active");this.els.li.addClass("active")};this.showFailNote=function(note,form){this.hideNotes();this.els.li.find(form+" "+this.selectors.error).html(note).css("display","block");$(".active").removeClass("active")};this.hideNotes=function(){$(".orange-note").addClass("hidden");$(".error-inline").addClass("hidden").css("display","none")}};var Comments=function(){var self=this;this.obj={};this.comments={};this.initialize=function(obj_type,obj_id){this.obj={type:obj_type,id:obj_id};this.els={};this.els.container=$("#comments");this.els.form=$(".comment-form:first").submit(this.submitComment);this.els.button=this.els.form.find("button:first");this.els.textarea=this.els.form.find("textarea:first");this.els.error_note=this.els.form.find(".error-inline:first");this.organizeComments();this.checkHash()};this.submitComment=function(e){if(self.els.textarea.val()==""){self.showErrorNote("A body is required.");return false}self.hideErrorNote();$("#ajax-loading").toggleClass("hidden");self.els.button.addClass("disabled").attr("disabled",true);$.post("/comments/create/",{body:self.els.textarea.val(),object_id:self.obj.id,object_type:self.obj.type,parent_id:0},function(a){$("#ajax-loading").toggleClass("hidden");self.els.button.removeClass("disabled").attr("disabled",false);if(a.result=="success"){self.els.textarea.val("");var element=self.injectComment(a.html);var comment=self.addElement(element,a.wow);comment.showSuccessNote(a.note);self.updateCount(1)}else{self.showErrorNote(a.note)}},"json");return false};this.organizeComments=function(){this.els.container.children("li").each(function(x,li){self.addElement(li)})};this.addElement=function(element,is_new){element=$(element);var comment=new Comment();comment.initialize(element,is_new);this.comments[element.attr("cid")]=comment;return comment};this.injectComment=function(html){var odd=this.els.container.children("li:first").hasClass("odd");this.els.container.prepend(html);return this.els.container.children("li:first").addClass(!odd?"odd":"")};this.hideErrorNote=function(){this.els.error_note.css("display","none")};this.showErrorNote=function(note){this.els.error_note.text(note).css("display","block")};this.checkHash=function(){var id=window.location.href.split("-")[1];if(!id){return}if(this.comments[id]){return}if($("#more_comments").length>0){$.getJSON("/comments/show_next/",{"paging-info":$("#hid_comment_paging_info").val(),comment_id:$("#hid_comment_id").val(),hash_comment_id:id},function(a){if(a.result=="success"){last_li=$("#hid_comment_id").parent().prev();last_li.after(a.html);lis=last_li.nextAll().filter(".comment-entry");lis.each(function(x,li){comments.addElement(li)});$("#hid_comment_id").val($("li.comment-entry:last").attr("cid"));if(a.paging_text==""){$("#more_comments").text("");$("#hid_comment_id").parent().remove()}else{$("#more_comments").text(a.paging_text);x=$("#hid_comment_paging_info").val();arr=x.split("-");$("#hid_comment_paging_info").val(arr[0]+"-"+arr[1]+"-"+a.page_no)}if(a.parent==1){$.post("/comments/get_thread_for/"+id,{},function(a){if(a.result=="success"){self.comments[a.parent_id].injectReplies(a.html)}self.comments[id].els.li.scrollTo(1000)},"json")}else{self.comments[id].els.li.scrollTo(1000)}}},"json")}else{$.post("/comments/get_thread_for/"+id,{},function(a){if(a.result=="success"){self.comments[a.parent_id].injectReplies(a.html)}self.comments[id].els.li.scrollTo(1000)},"json")}};this.updateCount=function(amt){var el=$("#top-level-comment-count");newamt=parseInt(el.html())+amt+"";el.html(newamt)}};var comments;$(document).ready(function(){comments=new Comments();comments.initialize($("#obj_type").val(),$("#obj_id").val())});