define(["jquery","local-storage","mustache","autosize","zenscroll","cookie-helper","masonry","jquery.vclick","jquery-animate3"],function(c,e,l,t,o,n){c.ajaxPrefilter(function(e,t,i){"post"==e.type.toLowerCase()&&(e.data=c.param(c.extend({},t.data,{"XSRF-TOKEN":n.read("XSRF-TOKEN")}))),!1===/^(https?:)?\/\//.test(e.url+"")&&i.setRequestHeader("X-Forced-Referer",window.location.href)});function i(e){var t=c(this).attr("data-reply-username"),i=c(this).closest(".message").find(".replyform");if(i.toggle(),i.is(":visible"))return void 0!==t&&!1!==t?i.find("textarea").removeClass("init").val("@"+t+": "):i.find("textarea").addClass("init").val(""),setTimeout(function(){i.find("textarea").click().focus(),o.center(i[0],300)}),!1}function s(e){var t=c(this).parent().siblings(".replies"),i=t.children(".reply").eq(t.hasClass("reverse")?0:-1),n=t.children(".reply").eq(t.hasClass("reverse")?1:-2),s=this;c(s).hasClass("opn")?(c(s).removeClass("opn").addClass("cls"),t.children(".reply.reveal").animate3({opacity:0},{duration:300,complete:function(){t.children(".reply.reveal").removeClass("reveal")}})):(c(s).removeClass("cls").addClass("opn"),t.children(".reply").not(i).not(n).addClass("reveal").animate3({opacity:1},{duration:300}))}function a(){var n=c(this).closest(".message"),e=c(this).closest(".replyform"),t=c.trim(e.find("textarea[name=reply]").val()),s=c(this).hasClass("edu-submit");if(""==t)return window.alert("Please enter your reply."),e.find("textarea[name=reply]").focus(),!1;if(t.length<25)return window.alert("You must enter at least 25 characters."),e.find("textarea[name=reply]").focus(),!1;if(t.split(" ").length<3)return window.alert("We need at least 3 words and 25 characters"),e.find("textarea[name=reply]").focus(),!1;e.find("textarea").css({opacity:.5,backgroundColor:"#fff"}).attr("disabled"),e.find(".submit").css("opacity",.5);var a={action:"Discussion.Submit",url:window.location.href+"",mid:n.attr("data-comment-id"),content:t,just_data:!0};return s&&(a={action:"Posts.AddComment",new_way:!0,pid:c(this).attr("data-post-id"),parent:n.attr("data-comment-id"),comment:t}),c.post(v,a,function(i,e){if(i.error)return"not loggedin"==i.message?gIsLoggedIn=!1:0===i.message.indexOf("Invalid characters")&&window.alert(i.message.replace(/\s+\[[a-f\d]{32}\]$/,"")),!1;i.guest?top.location.href=i.redirect:function(){var e=document.createElement("div"),t=l.render(g,{avatars_on_demand:!1,main_avatar:i.user.avatar,comments:i.comment,edu_disc:s,post_id:a.pid,children_in_reverse:discussionSection.children_in_reverse});e.innerHTML=t,w(e),y(e);c(e).children();n.find("button.more-replies").hasClass("opn")?c(e).find("button.more-replies").trigger("vclick"):c(e).find("div.replies article.reply").eq(discussionSection.children_in_reverse?2:-3).addClass("reveal").css({opacity:1}),n.replaceWith(c(e).children()),p.find(".message").removeClass("message-first"),p.find(".message").eq(0).addClass("message-first")}()},"json"),!1}function r(){var n=c(this).closest(".message"),e=discussionSection.edu,t={action:e?"Posts.DeleteComment":"Discussion.Delete"};if(c(this).parent().parent().hasClass("message"))window.confirm("Are you sure you want to delete this comment and all its replies?")&&(n.find("button.reply").unbind("vclick"),n.find("button.delete").unbind("vclick"),n.css("opacity",.5),p.find(".message .replyform").hide(),e?t.cid=c(this).attr("data-mid"):t.mid=c(this).attr("data-mid"),c.ajax({url:v,type:"POST",data:t,dataType:"json",success:function(e){discussionSection.rendered-=1,discussionSection.total-=1,n.animate3({opacity:0},{duration:250,complete:function(){var e=!!f&&salvattore.removeColumns(f);n.remove(),c(e).find(".message").length<1&&p.addClass("no-qs"),f&&salvattore.recreateColumns(f,e),p.find(".message").removeClass("message-first"),p.find(".message").eq(0).addClass("message-first"),b()}})},error:function(e){401!=e.status?(window.alert("There was an error with your session. The page will now refresh."),top.location.reload()):top.location.href="/join/light?redirect_to="+encodeURIComponent(window.location.href)}}));else if(window.confirm("Are you sure you want to delete this reply?")){var s=c(this).closest(".reply");s.find("button.reply").unbind("click"),s.find("button.delete").unbind("click"),s.css("opacity",.5),p.find(".message .replyform").hide(),e?t.cid=c(this).attr("data-rid"):(t.mid=n.find(".actions button.delete[data-mid]").attr("data-mid"),t.rid=c(this).attr("data-rid")),c.post(v,t,function(e,t){if(e.error)return window.alert("There was an error with your session. The page will now refresh."),top.location.reload(),!1;var i=parseFloat(n.children(".actions").children("button.reply").attr("data-reply-count"));i--,n.children(".actions").children("button.reply").text("Reply"+(i?" ("+i+")":"")).attr("data-reply-count",i),i<=2&&n.children(".actions").children(".more-replies").remove(),s.animate3({opacity:0},{duration:250,complete:function(){s.remove()}})},"json")}return!1}function d(e){var t=c(this).parent();return t.find(".hidden").is(":hidden")?(t.find(".hidden").show(),t.find(".more-ellipsis").hide(),$(this).text("show less")):(t.find(".hidden").hide(),t.find(".more-ellipsis").show(),$(this).text("show more")),e.preventDefault(),!1}function u(e){return c(this).parent().toggleClass("more"),e.preventDefault(),!1}function m(e){var t=c(this).attr("data-flag-url");window.open(t,"_blank"),e.preventDefault(),e.stopPropagation()}var f,p=c("section.sect-community-discussion"),h=!1,v="/ajax.php",g=c("script#discussion-questions-tpl").html(),w=function(e){h&&(h.id&&(c(e).find(".message-notmine[data-uid="+h.id+"]").removeClass("message-notmine"),c(e).find(".reply-notmine[data-uid="+h.id+"]").removeClass("reply-notmine")),h.pp&&c(e).find(".replyform img").prop("src",h.pp))},y=function(e){c(e).find(".message .actions button.reply").on("vclick",i),c(e).find(".message .actions button.more-replies").on("vclick",s),c(e).find(".message .actions button.delete").on("vclick",r),c(e).find(".replyform .submit button.whbl-link").on("vclick",a),c(e).find(".message .actions button.flag").on("vclick",m),t&&c(e).find("textarea").each(function(){t(this)}),c(e).find(".message .content a.more").on("vclick",d)},b=function(e){setTimeout(function(){(0<p.find(".message:hidden").length||discussionSection.rendered<discussionSection.total)&&p.find(".more-comm").show(),e&&e()})};if(0!=p.length){(h=n.read("mw_signon"))&&(h=decodeURIComponent(h),"un"in(h=JSON.parse(h))&&"pp"in h||(h=null)),h&&w(p),p.find(".discussionform form").on("submit",function(){var o=this,e=c.trim(c(this).find("textarea[name=question]").val()),r=discussionSection.edu;if(""==e)return window.alert("Please enter your message."),c(this).find("textarea[name=question]").focus(),!1;if(e.length<25)return window.alert("You must enter at least 25 characters."),c(this).find("textarea[name=question]").focus(),!1;if(e.split(" ").length<3)return window.alert("We need at least 3 words and 25 characters"),c(this).find("textarea[name=question]").focus(),!1;c(this).find(".error").remove(),c(this).find("textarea").css({opacity:.5,backgroundColor:"#fff"}).prop("disabled","disabled"),c(this).find(".submit").css("opacity",.5);var d={action:"Discussion.Submit",url:window.location.href+"",content:e,just_data:!0};return r&&(d={action:"Posts.AddComment",pid:c(o).attr("data-post-id"),comment:e,new_way:!0}),c.post(v,d,function(e,t){if(c(o).find("textarea").css("opacity",1).attr("disabled",!1),c(o).find(".submit").css("opacity",1),e.error)return"not loggedin"==e.message?gIsLoggedIn=!1:c.isPlainObject(e.message)&&"msg"in e.message&&"bad"in e.message&&c("<div class='error'>"+e.message.msg+": "+e.message.bad+"</div>").insertBefore(c(o).find(".submit")),c("div.discussion textarea").trigger("focus"),!1;if(e.guest)top.location.href=e.redirect;else{c(o).find("textarea").val("").trigger("change"),p.find(".message").removeClass("message-first");var i=document.createElement("div"),n=l.render(g,{avatars_on_demand:!1,main_avatar:e.user.avatar,comments:e.comment,edu_disc:r,post_id:d.pid});i.innerHTML=n,w(i),y(i);var s=c(i).children(),a=!!f&&salvattore.removeColumns(f);(f?c(a):p.find(".questions-container")).find("div.discussionform").after(s),c(o).find("textarea").css("height",""),f&&salvattore.recreateColumns(f,a),p.removeClass("no-qs"),b(),discussionSection.rendered+=1,discussionSection.total+=1}},"json"),!1}),p.find("div.discussionform form .submit button.whbl-link").on("vclick",function(){c(this).parents("form").trigger("submit")}),p.find("div.discussionform .discussion-disc .more-btn").on("vclick",u),p.find("div.discussionform .discussion-disc .less-btn").on("vclick",u),y(p),p.find(".more-comm .btn").on("vclick",function(){var i=c(this),e=discussionSection.edu,n=i.text();i.hasClass("disabled")||(i.addClass("disabled").html("<i class='bf-icon-spin5 animate-spin'></i>"),c.ajax({method:"GET",url:v,data:{action:e?"Posts.EduGetComments":"Discussion.GetComments",p:discussionSection.current_page+1,url:window.location.pathname,post_id:e?p.find(".discussionform form").attr("data-post-id"):void 0},success:function(e){1!=e.pages&&e.pages!=e.page||i.remove(),discussionSection.rendered+=e.comments.length,discussionSection.total_pages=e.pages,discussionSection.current_page=e.page;var t=document.createElement("div");t.innerHTML=l.render(g,{avatars_on_demand:!1,main_avatar:e.user.avatar,comments:e.comments}),w(t),y(t),c(t).children().each(function(){f?salvattore.appendElements(f,[this]):(p.find(".questions-container").append(this),i.css("opacity",0).animate3({opacity:1},{duration:300}))}),delete t,i.removeClass("disabled").text(n)}}))}),p.hasClass("full-page")&&(f=p.find(".questions-container")[0],salvattore.init(f),salvattore.rescanMediaQueries(f),setTimeout(function(){p.find(".message").length<1&&p.addClass("no-qs"),salvattore.recreateColumns(f)}),c(window).on("resize",function(){$(document.activeElement).is("textarea")||salvattore.rescanMediaQueries(f)})),c(window).on("load",function(e){var t=function(e,t){t=t||window.location.href,e=e.replace(/[\[\]]/g,"\\$&");var i=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return i?i[2]?decodeURIComponent(i[2].replace(/\+/g," ")):"":null}("comment-id",window.location.href);if(t){var i=p.find('[data-comment-id="'+t+'"]');if(!(i.length<1)){function n(){o.to(i[0],300,160,function(){i.animate3({"background-color":"rgba(75, 201, 153, 0.1)"},{duration:100,complete:function(){i.animate3({"background-color":"rgba(255, 255, 255, 0)"},{duration:2e3})}})})}var s=i;if(f){i.hasClass("reply")&&(s=i.parents(".message")).find("button.more-replies.cls").trigger("vclick");var a=s.index();p.find(".column").each(function(){c(this).children().each(function(e){if(e==a)return c(this).prev(".message:hidden").add(this).addClass("reveal").animate3({opacity:1},{duration:300}),!1})}),n()}else i.hasClass("reply")&&(s=i.parents(".message")).find("button.more-replies.cls").trigger("vclick"),s.prev(".message:hidden").add(s).addClass("reveal").animate3({opacity:1},{duration:300}),n()}}});var C,x=0,_=!1;C=window.setInterval(function(){if("border-box"==p.css("box-sizing")||100<x)return b(),window.clearInterval(C),void(_=!0);x++},50),c(document).ready(function(){window.clearInterval(C),_||b()})}});