(function($){var SelectionSharer=function(options){var self=this;options=options||{};if(typeof options=="string")options={elements:options};this.sel=null;this.textSelection="";this.htmlSelection="";this.appId=$('meta[property\x3d"fb:app_id"]').attr("content")||$('meta[property\x3d"fb:app_id"]').attr("value");this.url2share=$('meta[property\x3d"og:url"]').attr("content")||$('meta[property\x3d"og:url"]').attr("value")||window.location.href;this.getSelectionText=function(sel){var html="",text="";sel=
sel||window.getSelection();if(sel.rangeCount){var container=document.createElement("div");for(var i=0,len=sel.rangeCount;i<len;++i)container.appendChild(sel.getRangeAt(i).cloneContents());text=container.textContent;html=container.innerHTML}self.textSelection=text;self.htmlSelection=html||text;return text};this.selectionDirection=function(selection){var sel=selection||window.getSelection();var range=document.createRange();if(!sel.anchorNode)return 0;range.setStart(sel.anchorNode,sel.anchorOffset);
range.setEnd(sel.focusNode,sel.focusOffset);var direction=range.collapsed?"backward":"forward";range.detach();return direction};this.showPopunder=function(){self.popunder=self.popunder||document.getElementById("selectionSharerPopunder");var sel=window.getSelection();var selection=self.getSelectionText(sel);if(sel.isCollapsed||selection.length<10||!selection.match(/ /))return self.hidePopunder();if(self.popunder.classList.contains("fixed")){self.popunder.style.bottom=0;return self.popunder.style.bottom}var range=
sel.getRangeAt(0);var node=range.endContainer.parentNode;if(self.popunder.classList.contains("show")){if(Math.ceil(self.popunder.getBoundingClientRect().top)==Math.ceil(node.getBoundingClientRect().bottom))return;return self.hidePopunder(self.showPopunder)}if(node.nextElementSibling)self.pushSiblings(node);else{if(!self.placeholder){self.placeholder=document.createElement("div");self.placeholder.className="selectionSharerPlaceholder"}var margin=window.getComputedStyle(node).marginBottom;self.placeholder.style.height=
margin;self.placeholder.style.marginBottom=-2*parseInt(margin,10)+"px";node.parentNode.insertBefore(self.placeholder)}var offsetTop=window.pageYOffset+node.getBoundingClientRect().bottom;self.popunder.style.top=Math.ceil(offsetTop)+"px";setTimeout(function(){if(self.placeholder)self.placeholder.classList.add("show");self.popunder.classList.add("show")},0)};this.pushSiblings=function(el){while(el=el.nextElementSibling){el.classList.add("selectionSharer");el.classList.add("moveDown")}};this.hidePopunder=
function(cb){cb=cb||function(){};if(self.popunder=="fixed"){self.popunder.style.bottom="-50px";return cb()}self.popunder.classList.remove("show");if(self.placeholder)self.placeholder.classList.remove("show");var el;var els=document.getElementsByClassName("moveDown");while(el=els[0])el.classList.remove("moveDown");setTimeout(function(){if(self.placeholder)document.body.insertBefore(self.placeholder);cb()},600)};this.show=function(e){setTimeout(function(){var sel=window.getSelection();var selection=
self.getSelectionText(sel);if(!sel.isCollapsed&&selection&&selection.length>10&&selection.match(/ /)){var range=sel.getRangeAt(0);var topOffset=range.getBoundingClientRect().top-5;var top=topOffset+self.getPosition().y-self.$popover.height();var left=0;if(e)left=e.pageX;else{var obj=sel.anchorNode.parentNode;left+=obj.offsetWidth/2;do left+=obj.offsetLeft;while(obj=obj.offsetParent)}switch(self.selectionDirection(sel)){case "forward":left-=self.$popover.width();break;case "backward":left+=self.$popover.width();
break;default:return}self.$popover.removeClass("anim").css("top",top+10).css("left",left).show();setTimeout(function(){self.$popover.addClass("anim").css("top",top)},0)}},10)};this.hide=function(){self.$popover.hide()};this.smart_truncate=function(str,n){if(!str||!str.length)return str;var toLong=str.length>n,s_=toLong?str.substr(0,n-1):str;s_=toLong?s_.substr(0,s_.lastIndexOf(" ")):s_;return toLong?s_+"...":s_};this.getRelatedTwitterAccounts=function(){var usernames=[];var creator=$('meta[name\x3d"twitter:creator"]').attr("content")||
$('meta[name\x3d"twitter:creator"]').attr("value");if(creator)usernames.push(creator);var anchors=document.getElementsByTagName("a");for(var i=0,len=anchors.length;i<len;i++)if(anchors[i].attributes.href&&typeof anchors[i].attributes.href.value=="string"){var matches=anchors[i].attributes.href.value.match(/^https?:\/\/twitter\.com\/([a-z0-9_]{1,20})/i);if(matches&&matches.length>1&&["widgets","intent"].indexOf(matches[1])==-1)usernames.push(matches[1])}if(usernames.length>0)return usernames.join(",");
else return""};this.shareTwitter=function(e){e.preventDefault();var text="\u201c"+self.smart_truncate(self.textSelection.trim(),114)+"\u201d";var url="http://twitter.com/intent/tweet?text\x3d"+encodeURIComponent(text)+"\x26related\x3d"+self.getRelatedTwitterAccounts()+"\x26url\x3d"+encodeURIComponent(self.url2share);if(self.viaTwitterAccount&&text.length<120-6-self.viaTwitterAccount.length)url+="\x26via\x3d"+self.viaTwitterAccount;var w=640,h=440;var left=screen.width/2-w/2;var top=screen.height/
2-h/2-100;window.open(url,"share_twitter","toolbar\x3dno, location\x3dno, directories\x3dno, status\x3dno, menubar\x3dno, scrollbars\x3dno, resizable\x3dno, copyhistory\x3dno, width\x3d"+w+", height\x3d"+h+", top\x3d"+top+", left\x3d"+left);self.hide();return false};this.shareFacebook=function(e){e.preventDefault();var text=self.htmlSelection.replace(/<p[^>]*>/gi,"\n").replace(/<\/p>| {2}/gi,"").trim();var url="https://www.facebook.com/dialog/feed?"+"app_id\x3d"+self.appId+"\x26display\x3dpopup"+
"\x26caption\x3d"+encodeURIComponent(text)+"\x26link\x3d"+encodeURIComponent(self.url2share)+"\x26href\x3d"+encodeURIComponent(self.url2share)+"\x26redirect_uri\x3d"+encodeURIComponent(self.url2share);var w=640,h=440;var left=screen.width/2-w/2;var top=screen.height/2-h/2-100;window.open(url,"share_facebook","toolbar\x3dno, location\x3dno, directories\x3dno, status\x3dno, menubar\x3dno, scrollbars\x3dno, resizable\x3dno, copyhistory\x3dno, width\x3d"+w+", height\x3d"+h+", top\x3d"+top+", left\x3d"+
left)};this.shareLinkedIn=function(e){e.preventDefault();var text=self.htmlSelection.replace(/<p[^>]*>/gi,"\n").replace(/<\/p>| {2}/gi,"").trim();var url="https://www.linkedin.com/shareArticle?mini\x3dtrue\x26url\x3d"+encodeURIComponent(self.url2share)+"\x26title\x3d"+encodeURIComponent(text);var w=640,h=440;var left=screen.width/2-w/2;var top=screen.height/2-h/2-100;window.open(url,"share_linkedin","toolbar\x3dno, location\x3dno, directories\x3dno, status\x3dno, menubar\x3dno, scrollbars\x3dno, resizable\x3dno, copyhistory\x3dno, width\x3d"+
w+", height\x3d"+h+", top\x3d"+top+", left\x3d"+left)};this.shareEmail=function(){var text=self.textSelection.replace(/<p[^>]*>/gi,"\n").replace(/<\/p>| {2}/gi,"").trim();var email={};email.subject=encodeURIComponent("Quote from "+document.title);email.body=encodeURIComponent("\u201c"+text+"\u201d")+"%0D%0A%0D%0AFrom: "+document.title+"%0D%0A"+window.location.href;$(this).attr("href","mailto:?subject\x3d"+email.subject+"\x26body\x3d"+email.body);self.hide();return true};this.render=function(){var popoverHTML=
'\x3cdiv class\x3d"selectionSharer" id\x3d"selectionSharerPopover" style\x3d"position:absolute;"\x3e'+'  \x3cdiv id\x3d"selectionSharerPopover-inner"\x3e'+"    \x3cul\x3e"+'      \x3cli\x3e\x3ca class\x3d"action tweet" href\x3d"" title\x3d"Share this selection on Twitter" target\x3d"_blank"\x3eTweet\x3c/a\x3e\x3c/li\x3e'+'      \x3cli\x3e\x3ca class\x3d"action facebook" href\x3d"" title\x3d"Share this selection on Facebook" target\x3d"_blank"\x3eFacebook\x3c/a\x3e\x3c/li\x3e'+'      \x3cli\x3e\x3ca class\x3d"action linkedin" href\x3d"" title\x3d"Share this selection on LinkedIn" target\x3d"_blank"\x3eLinkedIn\x3c/a\x3e\x3c/li\x3e'+
'      \x3cli\x3e\x3ca class\x3d"action email" href\x3d"" title\x3d"Share this selection by email" target\x3d"_blank"\x3e\x3csvg width\x3d"20" height\x3d"20"\x3e\x3cpath stroke\x3d"%23FFF" stroke-width\x3d"6" d\x3d"m16,25h82v60H16zl37,37q4,3 8,0l37-37M16,85l30-30m22,0 30,30"/\x3e\x3c/svg\x3e\x3c/a\x3e\x3c/li\x3e'+"    \x3c/ul\x3e"+"  \x3c/div\x3e"+'  \x3cdiv class\x3d"selectionSharerPopover-clip"\x3e\x3cspan class\x3d"selectionSharerPopover-arrow"\x3e\x3c/span\x3e\x3c/div\x3e'+"\x3c/div\x3e";var popunderHTML=
'\x3cdiv id\x3d"selectionSharerPopunder" class\x3d"selectionSharer"\x3e'+'  \x3cdiv id\x3d"selectionSharerPopunder-inner"\x3e'+"    \x3clabel\x3eShare this selection\x3c/label\x3e"+"    \x3cul\x3e"+'      \x3cli\x3e\x3ca class\x3d"action tweet" href\x3d"" title\x3d"Share this selection on Twitter" target\x3d"_blank"\x3eTweet\x3c/a\x3e\x3c/li\x3e'+'      \x3cli\x3e\x3ca class\x3d"action facebook" href\x3d"" title\x3d"Share this selection on Facebook" target\x3d"_blank"\x3eFacebook\x3c/a\x3e\x3c/li\x3e'+
'      \x3cli\x3e\x3ca class\x3d"action linkedin" href\x3d"" title\x3d"Share this selection on LinkedIn" target\x3d"_blank"\x3eLinkedIn\x3c/a\x3e\x3c/li\x3e'+'      \x3cli\x3e\x3ca class\x3d"action email" href\x3d"" title\x3d"Share this selection by email" target\x3d"_blank"\x3e\x3csvg width\x3d"20" height\x3d"20"\x3e\x3cpath stroke\x3d"%23FFF" stroke-width\x3d"6" d\x3d"m16,25h82v60H16zl37,37q4,3 8,0l37-37M16,85l30-30m22,0 30,30"/\x3e\x3c/svg\x3e\x3c/a\x3e\x3c/li\x3e'+"    \x3c/ul\x3e"+"  \x3c/div\x3e"+
"\x3c/div\x3e";self.$popover=$(popoverHTML);self.$popover.find("a.tweet").click(self.shareTwitter);self.$popover.find("a.facebook").click(self.shareFacebook);self.$popover.find("a.linkedin").click(self.shareLinkedIn);self.$popover.find("a.email").click(self.shareEmail);$("body").append(self.$popover);self.$popunder=$(popunderHTML);self.$popunder.find("a.tweet").click(self.shareTwitter);self.$popunder.find("a.facebook").click(self.shareFacebook);self.$popover.find("a.linkedin").click(self.shareLinkedIn);
self.$popunder.find("a.email").click(self.shareEmail);$("body").append(self.$popunder);if(self.appId&&self.url2share)$(".selectionSharer a.facebook").css("display","inline-block")};this.setElements=function(elements){if(typeof elements=="string")elements=$(elements);self.$elements=elements instanceof $?elements:$(elements);self.$elements.mouseup(self.show).mousedown(self.hide).addClass("selectionShareable");self.$elements.bind("touchstart",function(){self.isMobile=true});document.onselectionchange=
self.selectionChanged};this.selectionChanged=function(e){if(!self.isMobile)return;if(self.lastSelectionChanged)clearTimeout(self.lastSelectionChanged);self.lastSelectionChanged=setTimeout(function(){self.showPopunder(e)},300)};this.getPosition=function(){var supportPageOffset=window.pageXOffset!==undefined;var isCSS1Compat=(document.compatMode||"")==="CSS1Compat";var x=supportPageOffset?window.pageXOffset:isCSS1Compat?document.documentElement.scrollLeft:document.body.scrollLeft;var y=supportPageOffset?
window.pageYOffset:isCSS1Compat?document.documentElement.scrollTop:document.body.scrollTop;return{x:x,y:y}};this.render();if(options.elements)this.setElements(options.elements)};$.fn.selectionSharer=function(){var sharer=new SelectionSharer;sharer.setElements(this);return this};if(typeof define=="function")define(function(){SelectionSharer.load=function(name,req,onLoad){var sharer=new SelectionSharer;sharer.setElements("p");onLoad()};return SelectionSharer});else if(typeof module==="object"&&module.exports)module.exports=
SelectionSharer;else window.SelectionSharer=SelectionSharer})(jQuery);
