/*
  Emphasis
  by Michael Donohoe (@donohoe)
  https://github.com/NYTimes/Emphasis
  http://open.blogs.nytimes.com/2011/01/10/emphasis-update-and-source/
  
  jQueryized by Rob Flaherty (@ravelrumba)
  https://github.com/robflaherty/Emphasis
      
  Adapted for WordPres by Benjamin J. Balter (@BenBalter)
  http://wordpress.org/extend/plugins/wp-emphasis/
*/
jQuery(function(h){var k={init:function(){this.config();this.vu=this.s=this.h=this.p=this.pl=!1;this.kh="|";this.addCSS();this.readHash();h(document).bind("keydown",this.keydown)},config:function(){this.paraSelctors=h(".entry p:not(:empty), .post p:not(:empty), .page p:not(:empty), article p:not(:empty)");this.classReady="emReady";this.classActive="emActive";this.classHighlight="emHighlight";this.classInfo="emInfo";this.classAnchor="emAnchor";this.classActiveAnchor="emActiveAnchor"},addCSS:function(){var a=document.createElement("style");
a.setAttribute("type","text/css");var b="p."+this.classActive+" span { background-color:#f2f4f5; } p span."+this.classHighlight+" { background-color:#fff0b3; } span."+this.classInfo+" { position:absolute; margin:-1px 0px 0px -8px; padding:0; font-size:10px; background-color: transparent !important} span."+this.classInfo+" a { text-decoration: none; } a."+this.classActiveAnchor+" { color: #000; font-size: 11px; }";try{a.innerHTML=b}catch(c){a.styleSheet.cssText=b}document.getElementsByTagName("head")[0].appendChild(a)},
readHash:function(){var a=decodeURI(location.hash),b=!1,c=[],e={},d,f,g;if(0>a.indexOf("[")&&0>a.indexOf("]")){if(f=/[ph][0-9]+|s[0-9,]+|[0-9]/g,a)for(;null!==(d=f.exec(a));)if(g=d[0].substring(0,1),d=d[0].substring(1),"p"===g)b=parseInt(d,10);else if("h"===g)c.push(parseInt(d,10));else{d=d.split(",");for(g=0;g<d.length;g++)d[g]=parseInt(d[g],10);e[c[c.length-1]]=d}}else if(b=a.match(/p\[([^[\]]*)\]/),g=a.match(/h\[([^[\]]*)\]/),b=b&&0<b.length?b[1]:!1,a=g&&0<g.length?g[1]:!1){a=a.match(/[a-zA-Z]+(,[0-9]+)*/g);
for(g=0;g<a.length;g++)if(d=a[g].split(","),f=d[0],f=this.findKey(f).index,void 0!==f){c.push(parseInt(f,10)+1);d.shift();if(0<d.length)for(f=1;f<d.length;f++)d[f]=parseInt(d[f],10);e[c[c.length-1]]=d}}this.p=b;this.h=c;this.s=e;this.goAnchor(b);this.goHighlight(c,e)},keydown:function(a){var b=k;b.kh=b.kh+a.keyCode+"|";if(-1<b.kh.indexOf("|16|16|"))b.vu=b.vu?!1:!0,b.paragraphInfo(b.vu);setTimeout(function(){b.kh="|"},500)},paragraphList:function(){if(this.pl&&0<this.pl.list.length)return this.pl;
var a=this,b=[],c=[],e=0,d=this.paraSelctors.length,f,g,i;for(f=0;f<d;f++)if(g=this.paraSelctors[f],0<(g.innerText||g.textContent||"").length)i=a.createKey(g),b.push(g),c.push(i),g.setAttribute("data-key",i),g.setAttribute("data-num",e),h(g).bind("click",function(b){a.paragraphClick(b)}),e++;return this.pl={list:b,keys:c}},paragraphClick:function(a){if(this.vu){var b=!1,c="P"===a.currentTarget.nodeName?a.currentTarget:!1,e=h(c),d="SPAN"===a.target.nodeName?a.target:!1,f="A"===a.target.nodeName?a.target:
!1;f&&!h(f).hasClass(this.classActiveAnchor)&&(this.updateAnchor(f),b=!0,a.preventDefault());if(!c&&!d)this.removeClass(this.classActive);else{if(e.hasClass(this.classReady))!e.hasClass(this.classActive)&&d&&!h(d).hasClass(this.classHighlight)?(h(this).removeClass(this.classActive),e.addClass(this.classActive)):(e.hasClass(this.classActive)||(h(this).removeClass(this.classActive),e.addClass(this.classActive)),d&&(h(d).toggleClass(this.classHighlight),b=!0));else{b=this.getSentences(c);a=b.length;
for(d=0;d<a;d++)b[d]="<span data-num='"+(d+1)+"'>"+this.rtrim(b[d])+"</span>";b=b.join(". ").replace(/__DOT__/g,".").replace(/<\/span>\./g,".</span>");d=b.substring(b.length-8).charCodeAt(0);-1==="|8221|63|46|41|39|37|34|33|".indexOf(d)&&(b+=".");c.innerHTML=b;c.setAttribute("data-sentences",a);h(this).removeClass(this.classActive);e.addClass(this.classActive);e.addClass(this.classReady);b=!0}b&&this.updateURLHash()}}},paragraphInfo:function(a){var b,c,e,d,f;if(a){if(a=h("span."+this.classInfo),0===
a.length){b=this.paragraphList();a=b.list.length;for(c=0;c<a;c++)if(e=b.list[c]||!1)d=b.keys[c],f=d===this.p?" "+this.classActiveAnchor:"",e.innerHTML="<span class='"+this.classInfo+"'><a class='"+this.classAnchor+f+"' href='#p["+d+"]' data-key='"+d+"' title='Link to "+this.ordinal(c+1)+" paragraph'>&para;</a></span>"+e.innerHTML}}else{b=h("span."+this.classInfo);a=b.length;for(c=0;c<a;c++)h(b[c]).remove();h(this).removeClass(this.classActive)}},updateAnchor:function(a){this.p=a.getAttribute("data-key");
h(this).removeClass(this.classActiveAnchor);h(a).addClass(this.classActiveAnchor)},updateURLHash:function(){var a="h[",b=h("p.emReady"),c=b.length,e,d,f,g,i;for(e=0;e<c;e++)if(d=b[e].getAttribute("data-key"),h(b[e]).hasClass(this.classHighlight))a+=","+d;else if(f=h("span."+this.classHighlight,b[e]),g=f.length,i=b[e].getAttribute("data-sentences"),0<g&&(a+=","+d),i!==g)for(d=0;d<g;d++)a+=","+f[d].getAttribute("data-num");a=((this.p?"p["+this.p+"],":"")+(a.replace("h[,","h[")+"]")).replace(",h[]",
"");location.hash=a},createKey:function(a){var b="",a=(a.innerText||a.textContent||"").replace(/[^a-z\. ]+/gi,""),c,e;if(a&&1<a.length&&(c=this.getSentences(a),0<c.length)){a=this.cleanArray(c[0].replace(/[\s\s]+/gi," ").split(" ")).slice(0,3);c=this.cleanArray(c[c.length-1].replace(/[\s\s]+/gi," ").split(" ")).slice(0,3);a=a.concat(c);c=6<a.length?6:a.length;for(e=0;e<c;e++)b+=a[e].substring(0,1)}return b},findKey:function(a){var b=this.paragraphList(),c=b.keys.length,e=!1,d=!1,f,g,h;for(f=0;f<c;f++){if(a===
b.keys[f])return{index:f,elm:b.list[f]};e||(g=this.lev(a.slice(0,3),b.keys[f].slice(0,3)),h=this.lev(a.slice(-3),b.keys[f].slice(-3)),3>g+h&&(e=f,d=b.list[f]))}return{index:e,elm:d}},goAnchor:function(a){if(a){var b=isNaN(a)?this.findKey(a).elm:this.paragraphList().list[a-1]||!1;b&&setTimeout(function(){h(window).scrollTop(h(b).offset().top)},500)}},goHighlight:function(a,b){if(a){var c=a.length,e,d,f,g,i,l,j,m,k;for(e=0;e<c;e++)if(d=this.paragraphList().list[a[e]-1]||!1){f=b[a[e].toString()]||!1;
g=!f||0===f.length;i=this.getSentences(d);l=i.length;for(j=0;j<l;j++)i[j]="<span data-num='"+(j+1)+"'>"+i[j]+"</span>";for(j=0;j<l;j++)m=g?j:f[j]-1,(k=i[m]||!1)&&(i[m]=i[m].replace("<span","<span class='"+this.classHighlight+"'"));d.setAttribute("data-sentences",l);d.innerHTML=i.join(". ").replace(/__DOT__/g,".").replace(/<\/span>\./g,".</span>");h(d).addClass("emReady")}}},getSentences:function(a){var a="string"===typeof a?a:a.innerHTML,b="A,B,C,D,E,F,G,H,I,J,K,L,M,m,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,etc,oz,cf,viz,sc,ca,Ave,St,Calif,Mass,Penn,AK,AL,AR,AS,AZ,CA,CO,CT,DC,DE,FL,FM,GA,GU,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MH,MI,MN,MO,MP,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,PR,PW,RI,SC,SD,TN,TX,UT,VA,VI,VT,WA,WI,WV,WY,AE,AA,AP,NYC,GB,IRL,IE,UK,GB,FR,0,1,2,3,4,5,6,7,8,9,www".split(","),
c=b.length,e;for(e=0;e<c;e++)a=a.replace(RegExp(" "+b[e]+"\\.","g")," "+b[e]+"__DOT__");b="Mr,Ms,Mrs,Miss,Msr,Dr,Gov,Pres,Sen,Prof,Gen,Rep,St,Messrs,Col,Sr,Jf,Ph,Sgt,Mgr,Fr,Rev,No,Jr,Snr,0,1,2,3,4,5,6,7,8,9".split(",");c=b.length;for(e=0;e<c;e++)a=a.replace(RegExp(b[e]+"\\.","g"),b[e]+"__DOT__");b="aero,asia,biz,cat,com,coop,edu,gov,info,int,jobs,mil,mobi,museum,name,net,org,pro,tel,travel,xxx".split(",");c=b.length;for(e=0;e<c;e++)a=a.replace(RegExp("\\."+b[e],"g"),"__DOT__"+b[e]);return this.cleanArray(a.split(". "))},
ordinal:function(a){var b=["th","st","nd","rd"],c=a%100;return a+(b[(c-20)%10]||b[c]||b[0])},lev:function(a,b){var c=a.length,e=b.length,d=[],f,g;d[0]=[];c<e&&(f=a,a=b,b=f,f=c,c=e,e=f);for(f=0;f<e+1;f++)d[0][f]=f;for(f=1;f<c+1;f++){d[f]=[];d[f][0]=f;for(g=1;g<e+1;g++)d[f][g]=this.smallest(d[f-1][g]+1,d[f][g-1]+1,d[f-1][g-1]+(a.charAt(f-1)===b.charAt(g-1)?0:1))}return d[c][e]},smallest:function(a,b,c){return a<b&&a<c?a:b<a&&b<c?b:c},rtrim:function(a){return a.replace(/\s+$/,"")},cleanArray:function(a){var b=
[],c;for(c=0;c<a.length;c++)a[c]&&0<a[c].replace(/ /g,"").length&&b.push(a[c]);return b}};h(window).bind("load",function(){k.init()})});
