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