$(function(){function u(){function t(t){var u={DiscussionId:n.discussionId,Text:n.Text()};i(u,function(n){r(n,t)})}function u(t){n.isLoadingComments(!0);var i=$("#ddlCommentsOrdering").val(),r=$("#ddlCommentsRankingLevel").val();$.getJSON("/api/comment/get/",{id:n.discussionId,ordering:i,skip:t,userLevel:r,commentId:n.commentId},function(t){$.each(t.Comments,function(t,i){n.comments.push(i)});t.Comments.length>0&&n.commentsExist(!0);n.isLoadingComments(!1);n.lastCount(t.Comments.length)})}var n=this;n.discussionId=$("#hidDiscussionId").val();n.commentId=$("#__CommentId").val();n.isLoadingComments=ko.observable(!1);n.comments=ko.observableArray();n.Text=ko.observable("");n.lastCount=ko.observable(0);n.commentsExist=ko.observable(!1);n.addComment=function(){t(!1)};n.addCommentAndShare=function(){t(!0)};n.load=function(){n.comments.removeAll();u(0)};n.loadMore=function(){var t=n.comments().length;u(t)}}function t(n){var i=$(n).closest(".media-body").closest(".media"),t=$(i).find(".comment-reply-form")[0];$(t).is(":visible")?$(t).hide():$(t).show()}function i(n,t){$.ajax({type:"POST",url:"/api/comment/post",data:n,success:function(n){app.user.isLoggedIn&&(layoutUserInfoViewModel.loadUserInfo(),layoutUserInfoViewModel.checkLevelUp());discussionViewModel.load();achievementsApi.comments($("#hidDiscussionId").val());n.Points>0&&app.user.isLoggedIn&&displayPointsPopup(n.Points);$(".comment-form-container textarea").val("");t!=null&&t(n.Comment)},error:function(){$("#modal-posting-comment-error").modal("show")}})}function r(t,i){if(t!=null){layoutUserInfoViewModel!=null&&(layoutUserInfoViewModel.checkLevelUp(),t.Points>0&&(displayPointsPopup(t.Points),achievementsApi.comments()));var r=$(".alert-thankyou");$(r)[0]!=null&&$(r).show();i&&($("#mdlSocialShareButtons").removeAttr("data-discussion-id"),$("#mdlSocialShareButtons").removeAttr("data-comment-id"),$("#mdlSocialShareButtons").attr("data-comment-id",t.Id),$("#mdlSocialShareButtons .share-title").html("Share Comment"),n.load(),$("#mdlSocialShareButtons").modal("show"));toastr.success("Your comment was successfully posted!");n.load()}}var n=new u;ko.applyBindings(n,$("#discussion_comments")[0]);n.load();n.commentId.length>0&&$("html,body").animate({scrollTop:$("#discussion_comments").offset().top-80},"slow");$(".show-more-container a").click(function(t){n.loadMore();t.preventDefault()});$(".comment-filter").change(function(){n.load()});$(document).on("click",".comment-pane .comment-links .btn-comment-reply",function(n){n.preventDefault();t(this)});$(document).on("click",".comment-pane .comment-links .btn-comment-edit",function(n){var t=$(this).closest(".comment"),i=$(t).find(".comment-text"),r=$(t).find(".comment-editor-container");$(i).hide();$(r).show();n.preventDefault()});$(document).on("click",".comment-pane .comment-editor-container .btn-cancel",function(n){var t=$(this).closest(".comment"),i=$(t).find(".comment-text"),r=$(t).find(".comment-editor-container");$(i).show();$(r).hide();n.preventDefault()});$(document).on("click",".comment-pane .comment-editor-container .btn-save",function(){var n=$(this).closest(".comment"),t=$(n).find(".comment-text"),i=$(n).find(".comment-editor-container"),r=$(i).find("textarea"),u={Id:$(this).closest(".media").attr("data-comment-id"),Text:$(r).val()};$.post("/api/comment/update",u,function(n){n!=null?($(t).html(n.Text),$(i).hide(),$(t).show()):alert("Oops! Something went wrong. Please try again.")})});$(document).on("click",".comment-pane .comment-reply-form .comment-reply-button",function(){var e=$(this),n=$(this).closest(".media-body"),o=$("#hidDiscussionId").val(),s=$(n).closest(".media").attr("data-comment-id"),u=$(n).find(".comment-textbox"),f={DiscussionId:o,ParentId:s,Text:$(u).val()};f.Text.trim().length!=0&&i(f,function(n){r(n,!1);t(e);$(u).html("")})});$(document).on("click",".comment-pane .btn-show-full-comment",function(n){var t=$(this).closest(".comment"),i=$(t).find(".short-comment"),r=$(t).find(".full-comment");$(i).hide();$(r).show();n.preventDefault()});$(document).on("mouseover",".media .media-body .comment",function(){$(this).find(".comment-hover-display").show()});$(document).on("mouseout",".media .media-body .comment",function(){$(this).find(".comment-hover-display").hide()});$(document).on("click",".media .media-body .comment .btn-comment-abuse",function(n){var t=$(this).closest(".media").attr("data-comment-id");$("#mdlCommentAbuseReport").attr("data-comment-id",t);$("#mdlCommentAbuseReport").modal("show");n.preventDefault()});$("#mdlCommentAbuseReport #btn-report-abuse").click(function(){var n=$("#mdlCommentAbuseReport").attr("data-comment-id");$.post("/api/comment/ReportAbuse",{"":n},function(){$("#mdlCommentAbuseReport").modal("hide");$("#mdlCommentAbuseReport-success").modal("show")})});$(document).on("click",".media .btn-comment-like",function(t){var i=$(this).closest(".media").attr("data-comment-id");$.post("/api/comment/ToggleLike",{"":i},function(t){var r=null;$.each(n.comments(),function(n,t){t.Id==i&&(r=t)});n.comments.replace(r,t)});t.preventDefault()});$(document).on("click",".media .btn-comment-delete",function(t){if(confirm("Are you sure you wish to delete this comment?")){var i=$(this).closest(".media").attr("data-comment-id");$.post("/api/comment/delete",{"":i},function(){n.load()})}t.preventDefault()});$(document).on("click",".comments .media .comment-links .btn-comment-view-replies",function(n){$(this).closest(".comment-container").find(".comment-replies").show();$(this).hide();$(this).closest(".comment-container").find(".btn-comment-hide-replies").show();n.preventDefault()});$(document).on("click",".comments .media .comment-links .btn-comment-hide-replies",function(n){$(this).closest(".comment-container").find(".comment-replies").hide();$(this).hide();$(this).closest(".comment-container").find(".btn-comment-view-replies").show();n.preventDefault()});$(document).on("click",".media .btn-comment-moderate",function(t){if(confirm("Are you sure you want to moderate this comment?")){var i=$(this).closest(".media").attr("data-comment-id");$.post("/api/comment/ModeratePost",{"":i},function(){n.load()})}t.preventDefault()})});$(function(){function r(){var n=this;n.topComments=ko.observableArray();n.topCommentsLoading=ko.observable(!1);n.load=function(){n.topCommentsLoading(!0);$.getJSON("/api/comments/top",{discussionId:i},function(t){n.topComments(t);n.topCommentsLoading(!1)})}}var t=$(".sidebar #top-comments")[0],i=$("#hidDiscussionId").val(),n;typeof t!="undefined"&&(n=new r,ko.applyBindings(n,t),n.load())})