var VM=VM||{};VM.Poll={$form:null,contentTitle:"",init:function(){VM.$body.find(".poll-extra").val(this.getExtraData())},initInline:function(n){this.$form=$("#poll-inline").on("submit",this.onSubmit).on("click",".poll-inline-toggler",function(){VM.Poll.toggleResults(!0)});this.$form.find(".poll-extra").val(this.getExtraData());this.contentTitle=n;VM.Ajax.resetValidation(this.$form)},getExtraData:function(){var n={Resolution:""+screen.width+"X"+screen.height+""};return JSON.stringify(n)},onSubmit:function(n){if(n.preventDefault(),VM.Poll.$form.valid()){var t=VM.Poll.$form.find("#poll-inline-vote").addClass("loading").prop("disabled",!0);$.ajax("/polls/votecontent",{method:"POST",data:VM.Poll.$form.serialize()}).done(function(n){if(n.options){var t=0;$.each(n.options,function(){t+=this.votes});t===0&&(t=1);$.each(n.options,function(){var n=VM.Poll.$form.find("#option_"+this.id),i=(this.votes/t*100).toFixed(1)+"%";n.find(".poll-details-voteinfo").text(this.votes+" vote"+(this.votes!==1?"s":"")+" "+i);n.find(".poll-details-votes").css("width",i)});VM.Poll.toggleResults(!1);VM.Poll.$form.find("#poll-inline-revote").remove();VM.Poll.trackEvent("Vote")}}).fail(function(){alert("There was a problem voting.")}).always(function(){t.removeClass("loading").prop("disabled",!1)})}},toggleResults:function(n){var t=VM.Poll.$form.find(".poll-inline-toggle").toggleClass("hidden");VM.Poll.$form.find(".poll-details-votes").css("animation","progress 600ms");n&&t.first().hasClass("hidden")&&VM.Poll.trackEvent("View Results")},trackEvent:function(n){var t={};t.eventCategory="In-Story Poll";t.eventAction=n;t.eventLabel=this.contentTitle;VM.Track.trackEvent(t)}};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={id:null,tag:null,shareCount:0,commentCount:0,counters:0,countersRendered:0,dataSent:!1,init:function(){var n=VM.$body.find(".details"),i=n.find(".sc-s").last(),t=n.find("#comments-count");this.id=n.data("id");this.tag=n.data("tag");t.length===0&&(t=n.find(".sc-c"));i.length&&(this.counters++,this.watchCount(i,i.closest(".sc"),this.setShareCount));t.length&&(this.counters++,this.watchCount(t,t.parent(),this.setCommentsCount))},setShareCount:function(n){VM.Momentum.shareCount=n},setCommentsCount:function(n){VM.Momentum.commentCount=n},watchCount:function(n,t,i){var r=this,u=0,f=window.setInterval(function(){u++;t.hasClass("rendered")&&(clearInterval(f),i(parseInt(n.text(),10)),r.countersRendered++,r.countersRendered===r.counters&&r.sendData());u===100&&clearInterval(f)},50)},sendData:function(n,t,i,r){if(!this.dataSent){this.dataSent=!0;var u=$(window).width()+"x"+$(window).height();$.ajax("/sendmomentum?id="+(n||this.id)+"&tag="+(t||this.tag)+"&shares="+(i||this.shareCount||0)+"&comments="+(r||this.commentCount||0)+"&viewport="+u,{method:"POST"})}}};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.Poll.init();VM.Reactions.init();VM.Momentum.init();VM.Embeds.init()})