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()&&$.ajax(n.first().data("af"),{method:"POST"}).done(function(t){n.each(function(){var n=$(this);n.append(t);VM.Reactions.activate(n)})})}},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(){alert("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 u,f,h,c;if(t!==null){var r=0,o=0,s=0,e=null;for(u in t)t.hasOwnProperty(u)&&(f=t[u],i&&f>o&&(o=f,e=u),r+=f);h=n.find(".js-react").each(function(n){var i=$(this),u=0,l=n+1,f=t[i.attr("value")],e;f&&r>0&&(u=f/r,e=f*l,s+=e);var o=(u*100).toFixed(0)+"%",h=i.find(".react-label"),c=h.next("b");c.length?c.text(o):h.after("<b>"+o+"<\/b>");i.find(".theme-bg").css("opacity",u)});c=Math.round(s/r);h.lt(c).addClass("selected");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=$('<div class="react-related"><h3 class="theme-colour">'+u+"<\/h3>"+i+"<\/div>");r.find(".section-tr-bordered").last().removeClass("section-tr-bordered");n.append(r);VM.setup()}}).fail(function(){})},errorMessage:function(n,t){var i=n.find(".alert-warning");i.length?i.text(t):n.find(".nav").after('<p class="alert alert-warning">'+t+"<\/p>")},trackReaction:function(n,t){var i={};i.eventCategory=n.data("event-type");i.eventAction=t;i.eventLabel=n.data("title");VM.Track.trackEvent(i)}};VM.Momentum={dataSent:!1,clientId:null,init:function(){var n=this;ga&&ga(function(){n.clientId=ga.getAll()[0].get("clientId")});document.addEventListener("visibilitychange",function(){document.visiblityState==="hidden"&&n.sendData()});window.addEventListener("pagehide",function(){n.sendData()});window.navigator.userAgent.indexOf("MSIE ")>0&&window.addEventListener("beforeunload",function(){n.sendData()})},sendData:function(){var i;if(!this.dataSent){this.dataSent=!0;var n=VM.$body.find(".details"),r=n.find(".sc-s").last(),t=n.find("#comments-count"),u=new Date-VM.Me.pageLoadedTime,f=VM.Scroll.maxHeight===0?0:Math.min(VM.Scroll.maxDepth/VM.Scroll.maxHeight,1);viewport=$(window).width()+"x"+$(window).height();t.length===0&&(t=n.find(".sc-c"));i="/sendmomentum?id="+n.data("id")+"&tag="+n.data("tag")+"&shares="+parseInt(r.text()||0,10)+"&comments="+parseInt(t.text()||0,10)+"&viewport="+viewport+"&clientId="+this.clientId+"&scroll="+f+"&time="+u;navigator.sendBeacon?navigator.sendBeacon(i):$.ajax(i,{method:"POST"})}},onLoaded:function(){var n=$("#momentum-items");n.find('[data-ga-value="'+VM.$body.find(".details").data("id")+'"]').remove();n.find(".lazyimg").lazyloader();n.find(".timeago").timeago()}};VM.Embeds={tweetEmbeds:null,instaEmbeds:null,init:function(){var e,i,r,t,n,u,f;if(this.tweetEmbeds=[],this.instaEmbeds=[],this.facebookEmbeds=[],e=$("#details-body").children("p"),e.length>0)for(n=0;n<e.length;n++)i=e[n],r=i.innerText||i.textContent,r.indexOf("https://twitter.com")===0?this.tweetEmbeds.push(this.processEmbed(i,r,"twitter")):r.indexOf("https://www.instagram.com")===0&&this.instaEmbeds.push(this.processEmbed(i,r,"instagram"));if(t=$(".embed-custom-wrapper"),t.length>0)for(n=0;n<t.length;n++)u=$(t[n]).data("type"),f=$(t[n]).data("url"),u=="twitter"?this.tweetEmbeds.push(this.processEmbed(t[n],f,"twitter")):u=="instagram"?this.instaEmbeds.push(this.processEmbed(t[n],f,"instagram")):u=="facebook"?this.facebookEmbeds.push(this.processEmbed(t[n],f,"facebook")):u=="facebook-page"&&this.facebookEmbeds.push(this.processEmbed(t[n],f,"facebook-page"));this.loadWidgets()},loadWidgets:function(){this.tweetEmbeds.length>0&&$.when.apply($,this.tweetEmbeds).done(function(){$.getScript("https://platform.twitter.com/widgets.js")});this.instaEmbeds.length>0&&$.when.apply($,this.instaEmbeds).done(function(){$.getScript("https://platform.instagram.com/en_US/embeds.js")});this.facebookEmbeds.length>0&&$.when.apply($,this.facebookEmbeds).done(function(){$.getScript("https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2")})},processEmbed:function(n,t,i){var u=VM.getFacebookApiKey(),r="";return i=="twitter"?r="https://publish.twitter.com/oembed?url="+encodeURIComponent(t):i=="instagram"?r="https://graph.facebook.com/instagram_oembed?url="+encodeURIComponent(t)+"&access_token="+u:i=="facebook"?r="https://graph.facebook.com/oembed_post?url="+encodeURIComponent(t)+"&access_token="+u:i=="facebook-page"&&(r="https://graph.facebook.com/oembed_page?url="+encodeURIComponent(t)+"&access_token="+u),$.ajax({cache:!1,url:r,jsonp:"callback",dataType:"jsonp"}).success(function(t){if(t)try{n.innerHTML=t.html}catch(i){}})}};$(function(){VM.Scroll.setIsStory();VM.Reactions.init();VM.Momentum.init();VM.Embeds.init()})