var VM=VM||{};VM.Reactions={init:function(){var n=VM.$body.find(".reactions");if(n.length){n.find(".react-view").removeClass("hidden").on("click",this.onShowReactions);n.find(".js-react").prop("disabled",!1).on("click",this.onReact).hover(this.onHover);VM.Storage.hasStorage()&&n.each(function(){VM.Reactions.activate($(this))})}},activate:function(n){var t=localStorage.getItem(this.getStorageKey(n));t?n.find(".js-react").addClass("reacted").filter("[value='"+t+"']").addClass("yours").attr("title","Your choice"):n.data("can-react",!0)},onReact:function(n){n.preventDefault();var i=$(this).addClass("loading").prop("disabled",!0),r=i.attr("value"),t=i.closest("form");t.data("can-react")&&$.ajax(t.attr("action")+"&type="+t.data("type")+"&subtype="+r,{method:"POST",data:t.serialize()}).done(function(n){VM.Reactions.showReactions(t,n.Reactions,!1);switch(n.Result){case 0:if(localStorage.setItem(VM.Reactions.getStorageKey(t),r),i.addClass("reacted").addClass("yours"),i.parent().siblings().find("button").addClass("reacted"),t.data("event-type")==="Importance"){var u=i.parent().index()+1;VM.Reactions.trackReaction(t,u.toString())}else VM.Reactions.trackReaction(t,r);t.data("can-react",!1);break;case 1:VM.Reactions.errorMessage(t,"There was a problem adding your reaction.");break;case 2:t.data("can-react",!1);VM.Reactions.errorMessage(t,"It looks like you've already added your reaction.");break;case 3:t.data("can-react",!1);VM.Reactions.errorMessage(t,"You've already added your reaction.")}VM.Reactions.showRelated(t,r)}).fail(function(){VM.Reactions.errorMessage(t,"There was a problem adding your reaction.")}).always(function(){i.removeClass("loading").prop("disabled",!1)})},onHover:function(){var n=$(this);n.hasClass("reacted")||$(this).toggleClass("hovered").parent().prevAll().children().toggleClass("hovered")},getStorageKey:function(n){return"VM_"+n.data("type")+"_"+n.data("id")},onShowReactions:function(n){n.preventDefault();var t=$(this).closest("form");$.ajax(t.data("results"),{method:"GET"}).done(function(n){VM.Reactions.showReactions(t,n,!0)}).fail(function(){alert("Sorry, the results are currently unavailable.")})},showReactions:function(n,t,i){var r,u,h;if(t!==null){var f=0,o=0,s=0,e=null;for(r in t)t.hasOwnProperty(r)&&(u=t[r],i&&u>o&&(o=u,e=r),f+=u);h=n.find(".js-react").each(function(n){var i=$(this),r=0,l=n+1,u=t[i.attr("value")],e;u&&f>0&&(r=u/f,e=u*l,s+=e);var o=(r*100).toFixed(0)+"%",h=i.find(".react-label"),c=h.next("b");c.length?c.text(o):h.after(""+o+"<\/b>");i.find(".theme-bg").css("opacity",r)});i&&e&&this.showRelated(n,e)}},showRelated:function(n,t){n.data("shown-related")||$.ajax(n.data("items").replace("=0","="+t),{method:"GET"}).done(function(i){if(n.data("shown-related",!0),i.length>50){var u=n.data("type")==="importance"?"Here are recent stories that are important:":"Here are other recent stories that make people feel "+t.toLowerCase()+":",r=$('