var nc=nc||{};nc.contest={},nc.contest.LikeButton=function(){var n="",t=[],r=function(t){n=t},u=function(){var t=$(this);return(n=n||t.data("likeurl"),!n)?!1:(t.hasClass("active")?e(t.data("token"),t):f(t.data("token"),t),!1)},f=function(r,u){$.inArray(r,t)==-1&&(t.push(r),$.post(n,{entryToken:r,add:!0},function(n){if(n.NotLoggedIn){nc.openLoginPopup();return}if(n.Successful){var f=u.find("span.no");f.text(n.Count),i(n.Count),u.addClass("active")}t.pop(r)}))},e=function(r,u){$.inArray(r,t)==-1&&(t.push(r),$.post(n,{entryToken:r,add:!1},function(n){if(n.NotLoggedIn){nc.openLoginPopup();return}if(n.Successful){var f=u.find("span.no");f.text(n.Count),i(n.Count),u.removeClass("active")}t.pop(r)}))},i=function(n){$(".nc-entry-likecount").text(n)};return{likeClicked:u,setLikeUrl:r}}(),nc.contest.Entries=function(){var n,t="",i="";this.init=function(u,f,e,o,s){n=nc.FilteredSearch(u,s,["view","user"],".nc-entries-list"),t=e,i=o,nc.contest.LikeButton.setLikeUrl(f),r()},this.entrysuccess=function(t){$("form.nc-submit-entry input:text").val("").change(),t.NotLoggedIn?nc.openLoginPopup():t.Successful&&t.Url?nc.showMessageShareFb(t.Message,t.LongerMessage,t.Url):t.Successful?nc.showMessage(t.Message,t.LongerMessage):nc.showMessage(t.Message,t.LongerMessage),n.reload(),$(".nc-entriessearch").css("visibility","visible")};var r=function(){$(".nc-entries-list").on("click",".nc-entries-delete",null,function(){var n=$(this),i=n.data("token"),r;return i&&(r=n.data("confirmation"),nc.showConfirm(r,null,function(){$.post(t,{entryToken:i},function(t){u(t,n)})})),!1});$(".nc-entries-list").on("click",".nc-entries-finalist",null,function(){var n=$(this),r=n.data("token"),t;return r&&(t=!n.hasClass("active"),$.post(i,{entryToken:r,add:t},function(i){f(i,n,t)})),!1});$(".nc-entries-list").on("click","a.nc-entries-like",null,nc.contest.LikeButton.likeClicked)},u=function(n,t){n.Successful?t.closest("tr").remove():n.Message&&nc.showMessage(n.Message)},f=function(n,t,i){n.Successful?t.toggleClass("active",i):n.Message&&nc.showMessage(n.Message)};return this}(),nc.contest.commentSuccess=function(n){n.NotLoggedIn?($("div.nc-comment-msg").empty(),nc.openLoginPopup()):n.Successful?n.Html?($("div.nc-comment-msg").empty(),$("textarea.nc-comment").val(""),$("div.nc-comments").show(),$("ul.nc-comment-list").append(n.Html)):n.Message&&(nc.showMessage(n.Message,n.LongerMessage),$("div.nc-comment-msg").empty(),$("textarea.nc-comment").val("")):$("div.nc-comment-msg").empty().append('<p class="msg-error">'+n.Message+"</p>")},nc.contest.commentDeleteClicked=function(){function r(n,t){n.Successful&&t.closest("li.update").remove()}var n=$(this),t=n.data("url"),i;return t&&(i=n.data("confirmation"),nc.showConfirm(i,null,function(){$.post(t,function(t){r(t,n)})})),!1},nc.contest.commentEditClicked=function(){function e(){n.html('<textarea cols="100" rows="5" maxlength="3000">'+u+"</textarea>"),t.hide(),$("a.nc-comment-cancel",i).show().unbind().click(function(){return n.html(f),t.show(),$("a.nc-comment-cancel, a.nc-comment-save",i).hide(),!1}),$("a.nc-comment-save",i).show().unbind().click(function(){return $.post(r,{text:$("textarea",n).val()},function(n){o(n)}),!1})}function o(r){r.Successful?(n.html(r.Html),t.show(),$("a.nc-comment-cancel, a.nc-comment-save",i).hide()):nc.showMessage(r.Message)}var t=$(this),i=$(this).closest("ul"),n=t.closest(".update").children("p.nc-comment-text:first"),u=n.text(),f=n.html(),r=t.data("url");return $.valHooks.textarea={get:function(n){return n.value.replace(/\r?\n/g,"\r\n")}},n&&r&&e(),!1},nc.contest.entryCommentEditClicked=function(){function o(){n.html('<input type="text" value="'+i+'" class="w500" maxlength="140"></input>'),$("input",n).focus(),h(),$("a.nc-entrycomment-cancel",t).unbind().click(function(){return n.html(e),u(i.length==0),!1}),$("a.nc-entrycomment-save",t).unbind().click(function(){return $.post(r,{text:$("input",n).val()},function(n){s(n)}),!1})}function s(t){t.Successful?(n.html(t.Html),i=n.text(),u(i.length==0)):nc.showMessage(t.Message)}function h(){$("span.nc-entrycomment-addtxt, a.nc-entrycomment-edit",t).hide(),$("a.nc-entrycomment-cancel, a.nc-entrycomment-save",t).show()}function u(n){$("a.nc-entrycomment-cancel, a.nc-entrycomment-save",t).hide(),n?$("span.nc-entrycomment-addtxt",t).show():$("a.nc-entrycomment-edit",t).show()}var f=$(this),t=$(this).closest("div"),n=$("p.nc-entrycomment-text:first"),i=n.text(),e=n.html(),r=f.data("url");return n&&r&&o(),!1},nc.contest.followClicked=function(){var n=$(this),t=n.text(),i=n.data("text"),r=n.data("url"),u=!n.parent().hasClass("active");return!t||!i||!r?!1:($.post(r,{add:u},function(r){r.NotLoggedIn?nc.openLoginPopup():r.Successful&&(n.text(i),n.data("text",t),u?n.parent().addClass("active"):n.parent().removeClass("active"))}),!1)},$(function(){$("a.nc-entry-like").click(nc.contest.LikeButton.likeClicked),$("a.nc-follow").click(nc.contest.followClicked);$(".nc-comment-list").on("click","a.nc-comment-delete",null,nc.contest.commentDeleteClicked);$(".nc-comment-list").on("click","a.nc-comment-edit",null,nc.contest.commentEditClicked);$(".nc-entrycomment-buttons a.nc-entrycomment-add, .nc-entrycomment-buttons a.nc-entrycomment-edit").click(nc.contest.entryCommentEditClicked)})