var vote=function(){var i={informModerator:-1,undoMod:0,acceptedByOwner:1,upMod:2,downMod:3,offensive:4,favorite:5,close:6,reopen:7,deletion:10,undeletion:11,spam:12};var j=function(){var w='<a href="/users/login?returnurl='+escape(document.location)+'">login or register</a>';var v=$("div.vote").find("div").not(".vote-accepted");if(typeof allowUpVote!="undefined"&&allowUpVote){v=v.not(".vote-up")}v.unbind("click").click(function(x){q($(x.target),"Please "+w+" to use voting.")});t().unbind("click").click(function(x){q($(x.target),"Please "+w+" to flag this post.")})};var u=function(v){if(!v){v="div.vote"}$(v).find("div.vote-up").unbind("click").click(function(w){vote.up($(w.target))});$(v).find("div.vote-down").unbind("click").click(function(w){vote.down($(w.target))});$(v).find("div.vote-favorite").unbind("click").click(function(w){vote.favorite($(w.target))})};var g=function(v){v.parent().find("div").not(".vote-accepted").unbind("click")};var c=function(v){$.ajax({type:"GET",url:"/posts/"+v+"/votes",dataType:"json",success:r,cache:false})};var r=function(v){$.each(v,function(){var w=$("div.vote:has(input[value="+this.PostId+"])");switch(this.VoteTypeId){case i.upMod:w.find("div.vote-up").removeClass("sprite-vote-arrow-up").addClass("sprite-vote-arrow-up-on");break;case i.downMod:w.find("div.vote-down").removeClass("sprite-vote-arrow-down").addClass("sprite-vote-arrow-down-on");break;case i.favorite:w.find("div.vote-favorite").removeClass("sprite-vote-favorite-off").addClass("sprite-vote-favorite-on");w.find("div.favoritecount b").addClass("favoritecount-selected");break;default:alert("site.vote.js > highlightExistingVotes has no case for "+this.VoteTypeId);break}});votesCast=null};var m=function(){return $("div.vote div[id^='vote-accepted-']")};var d=function(){return $("div.post-menu a[id^='lock-post-']")};var t=function(){return $("div.post-menu a[id^='flag-post-']")};var k=function(v){return v.hasClass("sprite-vote-arrow-up-on")};var n=function(v){return v.hasClass("sprite-vote-favorite-on")};var a=function(v){return v.hasClass("sprite-vote-arrow-down-on")};var f=function(v){return v.parent().find("input").val()};var s=function(w,v){if(k(w)){w.removeClass("sprite-vote-arrow-up-on").addClass("sprite-vote-arrow-up")}if(a(v)){v.removeClass("sprite-vote-arrow-down-on").addClass("sprite-vote-arrow-down")}};var o=function(v,w){var x=v.siblings("span.vote-count-post");x.text(parseInt(x.text(),10)+w)};var p=function(x,w){g(x);var v=f(x);l(x,v,w,b)};var l=function(y,w,v,B,A){var z={fkey:fkey};if(A){for(var x in A){z[x]=A[x]}}$.ajax({type:"POST",url:"/posts/"+w+"/vote/"+v,data:z,dataType:"json",success:function(C){B(y,w,C)},error:function(){q(y,"An error has occurred - please retry your request.")}})};var b=function(w,v,x){if(x.Success){if(x.Message){e(w,x.Message)}}else{if(window.console&&window.console.firebug&&(!x.Message||x.Message.length<5)){q(w,"FireBug seems to be enabled, which can sometimes interfere with voting;<br>please refresh the page to see if your vote was processed.<br><br>If this persists, consider disabling FireBug for this site.")}else{q(w,x.Message);s(w,w);w.parent().find("span.vote-count-post").text(x.NewScore);if(x.LastVoteTypeId){h(w,x.LastVoteTypeId)}}}u(w.parent())};var h=function(w,v){if(v==i.upMod){w.parent().find("div.vote-up").removeClass("sprite-vote-arrow-up").addClass("sprite-vote-arrow-up-on")}else{if(v==i.downMod){w.parent().find("div.vote-down").removeClass("sprite-vote-arrow-down").addClass("sprite-vote-arrow-down-on")}}};var q=function(v,w){var x=$('<div class="vote-notification"><h2>'+w+"</h2>(click on this box to dismiss)</div>");x.click(function(y){$(".vote-notification").fadeOut("fast",function(){$(this).remove()})});v.parent().append(x);x.fadeIn("fast")};var e=function(w,y){var z=$('<div class="vote-notification"><h2>'+y+"</h2></div>");w.parent().append(z);z.fadeIn("fast");var v=function(){$(".vote-notification").fadeOut("fast",function(){$(this).remove()})};var x=Math.max(2500,y.length*40);setTimeout(v,x)};return{init:function(v){if(typeof allowUpVote!="undefined"&&allowUpVote){if(votesCast==null){c(v)}else{r(votesCast)}u();t().unbind("click").click(function(x){vote.flag($(x.target))});if(typeof isRegistered=="undefined"||!isRegistered){j()}}else{j()}m().unbind("click").click(function(x){vote.acceptedAnswer($(x.target))});var w=$("div.post-menu a[id^='close-question-']");w.unbind("click").click(function(x){vote.close(w)});$("div.post-menu a[id^='delete-question-']").unbind("click").click(function(x){vote.deletion($(x.target))})},up:function(y){var z=y.parent().find("div.vote-up");var x=y.parent().find("div.vote-down");var w=k(z);var v=a(x);var A=w?-1:(v?2:1);o(y,A);s(z,x);if(!w){z.removeClass("sprite-vote-arrow-up").addClass("sprite-vote-arrow-up-on")}p(y,w?i.undoMod:i.upMod)},down:function(y){var z=y.parent().find("div.vote-up");var x=y.parent().find("div.vote-down");var w=a(x);var v=k(z);var A=w?1:(v?-2:-1);o(y,A);s(z,x);if(!w){x.removeClass("sprite-vote-arrow-down").addClass("sprite-vote-arrow-down-on")}p(y,w?i.undoMod:i.downMod)},favorite:function(w){var x=w.parent().find("div.favoritecount b");var v=parseInt("0"+x.text().replace(/^\s+|\s+$/g,""),10);if(!n(w)){w.removeClass("sprite-vote-favorite-off").addClass("sprite-vote-favorite-on");x.addClass("favoritecount-selected").text(++v)}else{w.removeClass("sprite-vote-favorite-on").addClass("sprite-vote-favorite-off");x.removeClass("favoritecount-selected").text((v--<=0)?"":v)}w.unbind("click");l(w,f(w),i.favorite,function(y){w.click(function(z){vote.favorite($(z.target))})})},acceptedAnswer:function(w){if(typeof hasOpenBounty!="undefined"&&hasOpenBounty){var x=($("#"+v+"-is-owned-by-question-owner").length>0)?"Are you sure you want to accept your own answer?  You will STILL LOSE the offered rep and THIS CANNOT BE UNDONE!":"Are you sure you want to accept this answer to your bounty question - THIS CANNOT BE UNDONE!";if(!confirm(x)){return}}if(typeof isBountyQuestion!="undefined"&&isBountyQuestion&&typeof hasOpenBounty=="undefined"){q(w,"You cannot change the accepted answer on a bounty question");return}m().unbind("click");var v=w.attr("id").substring("vote-accepted-".length);l(w,v,i.acceptedByOwner,function(B,z,D){if(D.Message.search(/vote-accepted/)!==-1){var A="comments-link";var C="comments-container";$("div.answer").removeClass("accepted-answer");$("div.vote-accepted").removeClass("sprite-vote-accepted-on").addClass("sprite-vote-accepted");$("a.comments-link-accepted").removeClass().addClass(A);$("div.comments-container-accepted").removeClass().addClass(C);var y=function(E){E.find(".comments-link").removeClass().addClass("comments-link-owner").end().find(".comments-container").removeClass().addClass("comments-container-owner")};$("div.answer:has(input[id$='-is-owned-by-question-owner'])").not(".owner-answer").addClass("owner-answer").each(function(){y($(this))});if(D.Message.search(/vote-accepted-on/)>-1){$("div.answer:has(div[id^='vote-accepted-"+z+"'])").removeClass("owner-answer").addClass("accepted-answer");A=A+"-accepted";C=C+"-accepted"}else{if($("#"+z+"-is-owned-by-question-owner").length>0){y($("#answer-"+z))}}$("a[id='comments-link-"+z+"']").removeClass().addClass(A).siblings("div").removeClass().addClass(C);B.removeClass("sprite-vote-accepted sprite-vote-accepted-on").addClass(D.Message);if(typeof hasOpenBounty!="undefined"&&hasOpenBounty){hasOpenBounty=false;$("#bounty-notification").hide()}}else{q(B,D.Message)}m().click(function(E){vote.acceptedAnswer($(E.target))})})},flag:function(x){var v=x.attr("id").substring("flag-post-".length);var C="form-flag-"+v;var F=$("#"+v+"-is-owned-by-current-user").length>0;var D=[[i.offensive,"Offensive, Abusive, or Hate Speech",!F],[i.spam,"Spam",!F],[i.informModerator,"Requires Moderator attention",true]];var A='<div class="vote-notification flag-menu"><h2>Please flag with care:</h2>';A+='<div class="flag-reasons"><form id="'+C+'">';for(var y=0;y<D.length;y++){if(D[y][2]){var E="flag-radio"+v+"-"+D[y][0];A+='<input type="radio" id="'+E+'" name="flag-'+v+'" value="'+D[y][0]+'">';A+='<label for="'+E+'">'+D[y][1]+"</label><br>"}}A+='<div class="flag-comment">Why are you flagging this post?<textarea name="flag-reason" cols="33" rows="4"></textarea>';A+='<br><span class="text-counter"></span></div>';A+="</form></div>";A+='<a class="flag-cancel">Cancel</a><a class="flag-submit">Flag Post</a>';A+="</div>";var z=$(A);var w=z.find("#"+C);var B=w.find("textarea");w.find("input").click(function(){var G=w.find("div.flag-comment");var H=vote.flagIsInform(w);G.toggle(H);if(H){B.focus()}vote.flagAllowSubmit(z,B,H)});B.bind("blur focus keyup",function(){comments.updateTextCounter(this,150,10);vote.flagAllowSubmit(z,B,vote.flagIsInform(w))});z.find("a.flag-submit").click(function(){if(vote.flagIsInform(w)&&!vote.flagTextValid(B)){return}vote.flagSubmit(x,v,w,B)});z.find("a.flag-cancel").click(function(){vote.flagClosePopup(x)});x.parent().append(z);z.fadeIn("fast")},flagIsInform:function(v){var w=v.find("input:radio:checked");if(w.length==0){return false}return w.val()==i.informModerator},flagAllowSubmit:function(w,x,y){var v=y?vote.flagTextValid(x):true;w.find("a.flag-submit").toggle(v)},flagTextValid:function(w){var v=$.trim(w.val()).length;return(v>=10&&v<=150)},flagClosePopup:function(v){v.parent().find(".vote-notification").fadeOut("fast",function(){$(this).remove()})},flagSubmit:function(y,w,v,z){vote.flagClosePopup(y);var x=v.find("input:radio:checked").val();var w=y.attr("id").substring("flag-post-".length);if(x==i.informModerator){$.ajax({type:"POST",url:"/messages/inform-moderator-about-post/"+w,dataType:"json",data:{fkey:fkey,msg:z.val()},success:function(A){showAjaxError(y.parent(),A.Message)},error:function(A,C,B){showAjaxError(y.parent(),(A.responseText&&A.responseText.length<100?A.responseText:"An error occurred during submission"))}})}else{l(y,w,x,vote.flagSubmitCallback,{comment:z.val()})}},flagSubmitCallback:function(w,v,y){if(y&&y.Success){if(y.Message){}}else{var x=w.parent();if(y&&y.Message){showAjaxError(x,y.Message)}else{showAjaxError(x,"A problem occurred during flagging")}}},close:function(w){var x=w.text().indexOf("open")>-1;var v=w.attr("id").substring("close-question-".length);if(x){if(confirm("Nominate this question for reopening?")){l(w,v,i.reopen,vote.close_result)}}else{if(!vote.close_reasons){vote.close_fetchReasons(w,v)}else{vote.close_renderForm(w)}}},close_reasons:null,close_fetchReasons:function(w,v){$(".error-notification").fadeOutAndRemove();appendLoader(w);$.ajax({type:"GET",url:"/posts/close-reasons/"+v,dataType:"json",success:function(x){removeLoader();vote.close_reasons=x;vote.close_renderForm(w)},error:function(x,z,y){removeLoader();showAjaxError(w.parent(),(x.responseText&&x.responseText.length<100?x.responseText:"An error occurred while fetching close reasons"))}})},close_renderForm:function(A){var v=A.attr("id").substring("close-question-".length);var y="close-popup-"+v;$("#"+y).remove();var x='<div id="'+y+'" class="vote-notification"><h2>Why should this question be closed?</h2><ul>';for(var w=0;w<vote.close_reasons.length;w++){var z=vote.close_reasons[w];x+='<li><a id="close-reason-'+z.id+'" class="close-reason"'+(z.description?' title="'+z.description+'"':"")+">"+z.name+"</a>"+(z.count>0?'<span title="this many votes already exist">('+z.count+")</span>":"")+"</li>"}x+='</ul><a class="close-cancel">Cancel</a>';x+="</div>";var B=$(x);B.find("a.close-reason").click(function(){vote.close_reasonClick(A,B,$(this),v)});B.find("a.close-cancel").click(function(){B.fadeOutAndRemove()});A.parent().append(B);B.fadeIn("fast")},close_reasonExactDuplicateId:1,close_hasLinkedDuplicateQuestions:false,close_reasonClick:function(w,x,y,v){var z=y.attr("id").substring("close-reason-".length);if(z==vote.close_reasonExactDuplicateId){vote.close_duplicateForm(w,x,v)}else{x.fadeOutAndRemove();appendLoader(w);l(w,v,i.close,vote.close_result,{"close-reason-id":z})}},close_duplicateForm:function(y,z,w){z.find("h2, ul").remove();var A="duplicate-question-"+w;var x='<div style="padding-bottom:18px"><h2>This question is a duplicate of which other question?</h2><input id="'+A+'" type="text" size="78"><br><span>Type a question id or title to search for valid targets</span><div class="existing-linked-questions"></div></div><a class="close-submit">Vote to Close</a>';var v=$(x);v.find("#"+A).autocomplete("/search/duplicate-questions/"+w,{highlightItem:true,matchContains:true,matchSubset:false,scroll:true,scrollHeight:300,formatItem:function(B){z.find("a.close-submit").hide();var D=B[0];var C=B[1];return(D=="-1")?('<b style="color:#990000">'+C+"</b>"):(D+" - "+C)},formatResult:function(B){var C=B[0];return(C=="-1")?" ":C}}).result(function(C,B){z.find("a.close-submit").toggle((B&&B[0]!="-1"))});z.prepend(v);vote.close_showExistingDuplicates(z,w);z.find("#"+A).focus();z.find("a.close-submit").click(function(){z.fadeOutAndRemove();l(y,w,i.close,vote.close_result,{"close-reason-id":vote.close_reasonExactDuplicateId,"duplicate-question-id":$("#"+A).val()})})},close_showExistingDuplicates:function(A,v){var z=false;for(var x=0;x<vote.close_reasons.length;x++){var y=vote.close_reasons[x];if(y.id==vote.close_reasonExactDuplicateId){if(y.count>0){z=true}break}}if(!z){return}var w=A.find("div.existing-linked-questions");appendLoader(w);w.append('<hr style="color:#fff; background-color:#fff;"><p>Other users chose these questions as the master question:</p>');$.getJSON("/posts/existing-close-duplicate-questions/"+v,function(C){removeLoader();for(var B=0;B<C.length;B++){w.append('<li><a href="'+C[B].url+'" target="_blank">'+C[B].title+"</a></li>")}})},close_result:function(w,v,y){removeLoader();if(y&&y.Success){if(y.Message){var z=w.text().indexOf("open")>-1;w.text(w.text().replace(/\w?\(\d\)/,"")+" "+y.Message);q(w,"This question still needs "+y.NewScore+" vote(s) from other users to "+(z?"reopen":"close"))}else{location.reload(true)}vote.close_reasons=null}else{var x=w.parent();if(y&&y.Message){showAjaxError(x,y.Message)}else{showAjaxError(x,"A problem occurred during closing/reopening")}}},deletion:function(x){var v=x.attr("id").substring("delete-question-".length);var w=x.text().indexOf("undelete")>-1;if(confirm("Vote to "+(w?"un":"")+"delete this post?")){l(x,v,(w?i.undeletion:i.deletion),vote.deletionCallback)}},deletionCallback:function(y,x,z){var A=y.text().indexOf("undelete")>-1;if(z&&z.Success){y.text(z.Message);if(z.NewScore<0){var w=$("#question:has(a[id='delete-question-"+x+"'])").length>0;var v=w?"#question, div.answer":"#answer-"+x;vote.setDeleteStyles($(v),!A)}else{q(y,"This post still needs "+z.NewScore+" vote(s) from other users to "+(A?"un":"")+"delete")}}else{var B=(z&&z.Message)?z.Message:"A problem occurred during "+(A?"un":"")+"deletion";showAjaxError(y.parent(),B)}},setDeleteStyles:function(w,v){if(v){$("div.question-status:has(span:contains('delete'))").show();w.addClass("deleted-answer").find("a[id^='delete-question-']").addClass("deleted-post").end().find("div[id^='comments-']").addClass("comments-container-deleted").end().find("a[id^='comments-link-']").addClass("comments-link-deleted")}else{document.location.reload(true)}},bountyStart:function(w){var v=$("#question div.vote input:first").val();$.post("/posts/"+v+"/bounty-start",{fkey:fkey,amount:w},function(x){if(x.Success){location.reload(true)}else{q($("#bounty-errors"),x.Message);$("#bounty-submit").attr("disabled","")}},"json")}}}();function bindAllPostClicks(){}function unbindAllPostClicks(){}var comments=function(){var k=600;var a=function(l){return $("#comments-"+l)};var i=function(l){appendLoader("#comments-"+l+" div.comments")};var f=function(m,p){var l=$("#form-comments-"+m);if(l.length>0){var o='<table><tr><td><textarea name="comment" cols="68" rows="3" maxlength="'+k;o+='" onblur="comments.updateTextCounter(this)" ';o+='onfocus="comments.updateTextCounter(this)" onkeyup="comments.updateTextCounter(this)"></textarea>';o+='<input type="submit" value="Add Comment" /></td></tr><tr><td><span class="text-counter"></span>';o+='<span class="form-error"></span></td></tr></table>';l.append(o);l.validate({rules:{comment:{required:true,minlength:15}},errorElement:"span",errorClass:"form-error",errorPlacement:function(q,r){var s=r.parents("form").find("span.form-error");s.replaceWith(q)},submitHandler:function(q){disableSubmitButton($(q));d(m,$(q))}});var n=$("#comments-"+m+" tr.comment:first td.comment-actions").width()||-1;n+=9;l.children("table").css("margin-left",n+"px")}};var e=function(l,m){i(l);$.ajax({type:"GET",url:"/posts/"+l+"/comments",dataType:"html",success:function(n){b(l,n)},error:function(n,p,o){removeLoader();showAjaxError("#comments-"+l,(n.responseText&&n.responseText.length<100?n.responseText:"An error has occured while fetching comments"))}})};var b=function(l,m){var n=a(l).find("div.comments");if(n.children().length>0){n.children().remove()}n.append(m);h();j();removeLoader()};var c=function(n){var l="";if(n&&n>0){var m=n<5?"":n<=15?"warm":n<=30?"hot":"supernova";l+='<span title="number of \'great comment\' votes received" class="'+m+'">'+n+"</span>"}return l};var j=function(){$("div.comment-up").click(function(){g($(this),2,"sprite-comment-up","sprite-comment-up-hover","comment-up",function(m,l){m.closest("tr").siblings("tr").remove();m.parent().siblings().children().remove();m.parent().siblings().append(c(l.NewScore))})}).hover(function(){$(this).removeClass("sprite-comment-up").addClass("sprite-comment-up-hover")},function(){$(this).removeClass("sprite-comment-up-hover").addClass("sprite-comment-up")});$("div.comment-flag").click(function(){if(confirm("Really flag this comment as offensive, spam, or hate speech?")){g($(this),4,"sprite-comment-flag","sprite-comment-flag-hover","comment-flag",function(m,l){if(l.NewScore==-1){m.parents("tr.comment").remove()}else{m.parents("tr.comment").find("div.comment-up").remove();m.remove()}})}}).hover(function(){$(this).removeClass("sprite-comment-flag").addClass("sprite-comment-flag-hover")},function(){$(this).removeClass("sprite-comment-flag-hover").addClass("sprite-comment-flag")});$("div.comment-delete").click(function(){if(confirm("Really delete this comment?")){g($(this),10,"sprite-comment-del","sprite-comment-del-hover","comment-delete",function(m,l){m.parents("tr.comment").remove()})}}).hover(function(){$(this).removeClass("sprite-comment-del").addClass("sprite-comment-del-hover")},function(){$(this).removeClass("sprite-comment-del-hover").addClass("sprite-comment-del")});$("tr.comment").hover(function(){$(this).find("div.comment-up, div.comment-flag, div.comment-delete").css("visibility","visible").closest("tr.comment").addClass("comment-hover")},function(){$(this).removeClass("comment-hover").find("div.comment-up, div.comment-flag, div.comment-delete").css("visibility","hidden")})};var h=function(){$("tr.comment").unbind("mouseenter mouseleave");$("div.comment-up, div.comment-flag, div.comment-delete").unbind("click mouseenter mouseleave")};var g=function(s,p,q,m,o,n){var r=s.parents("tr.comment").attr("id").substr("comment-".length);$("div.error-notification").hide();s.removeClass().unbind("click mouseenter mouseleave").addClass("sprite "+m);appendLoader(s.parent());var l=function(){s.addClass(o).click(function(){g(s,p,q,m,o,n)}).addClass("sprite "+q)};$.ajax({type:"POST",url:"/posts/comments/"+r+"/vote/"+p,dataType:"json",data:{fkey:fkey},success:function(t){if(t.Success){n(s,t)}else{showAjaxError(s.parent(),t.Message);l()}},error:function(t,v,u){showAjaxError(s.parent(),(t.responseText&&t.responseText.length<100?t.responseText:"An error occurred during voting"));l()}});removeLoader()};var d=function(n,m){var l=m.find("textarea");if(l.val()&&l.val().length>k){alert("Comments are limited to "+k+" characters");enableSubmitButton(formSelector);return}i(n);var o=function(){$(".error-notification").fadeOut("fast",function(){$(this).remove()})};$.ajax({type:"POST",url:"/posts/"+n+"/comments",dataType:"html",data:{comment:l.val(),fkey:fkey},success:function(p){o();b(n,p);l.val("");comments.updateTextCounter(l);enableSubmitButton(m)},error:function(p,r,q){removeLoader();o();showAjaxError(m,(p.responseText&&p.responseText.length<100?p.responseText:"An error occurred during comment submission"));enableSubmitButton(m)}})};return{init:function(){$("a[id^='comments-link-']").unbind("click").click(function(){var l=$(this).attr("id").substr("comments-link-".length);var m=a(l);f(l,m);m.removeClass("display-none");if($(this).text().indexOf("more comment")>-1){e(l,m)}else{m.find("textarea").focus()}$(this).hide()});j()},updateTextCounter:function(m,l,n){n=n||15;l=l||k;var o=m.value?$.trim(m.value).length:0;var p=$(m);counter=p.parents("form").find("span.text-counter");if(o==0){counter.html("Enter at least {} characters.".replace("{}",n))}else{if(o<n){counter.html("{} more to go...".replace("{}",n-o))}else{counter.html((l-o)+" character"+(o<l-1?"s":"")+" left")}}}}}();
