/*! howmuchread - v0.2.0 - 2015-09-29 * https://github.com/hakatashi/howmuchread * Copyright (c) 2015 ; Licensed MIT */ (function(){var a,b,c,d,e,f,g,h,i,j,k;a=jQuery,h={vertical:"reverse",horizontal:"reverse"},a.fn.howmuchread=function(a){return f[a]?f[a].apply(this,Array.prototype.slice.call(arguments,1)):f.get.apply(this,arguments)},f={},f.get=function(e){var h,i,l,m,n,o,p,q,r;return i={parent:this,baseline:"before",borderline:"before",wrapperId:"howmuchread-wrapper",writingMode:{},getMetric:!1,blacklist:""},p=a.extend({},i,e),r=a.extend(g.call(this),p.writingMode),n=d(p.parent,r),h=c(n,p.borderline),q=f.getLength.call(this),m={},o=b(q,function(b){return function(e){var f,g;return k(a(b),e,p.wrapperId),g=d(a("span#"+p.wrapperId),r),j(a(b),p.wrapperId),m[e]=g,f=c(g,p.baseline),f>=h}}(this)),p.getMetric?o in m?(l=m[o],l.position=o,l):{position:o,top:null,left:null,right:null,bottom:null,before:null,after:null,start:null,end:null,borderSize:null,inlineSize:null}:o},f.scrollTo=function(b,e){var f,h,l,m,n,o,p,q;return m={parent:this,baseline:"before",borderline:"before",wrapperId:"howmuchread-wrapper",getMetric:!1,noScroll:!1,writingMode:{}},o=a.extend({},m,e),f=a(o.parent),q=a.extend(g.call(this),o.writingMode),n=d(o.parent,q),l=c(n,o.borderline),k(a(this),b,o.wrapperId),p=d(a("span#"+o.wrapperId),q),j(a(this),o.wrapperId),o.noScroll||(h=c(p,o.baseline),q.horizontal?q.ttb?f.scrollTop(f.scrollTop()+(h-l)):f.scrollTop(f.scrollTop()-(h-l)):q.rtl?i.call(o.parent,i.call(o.parent,void 0,q)-(h-l),q):i.call(o.parent,i.call(o.parent,void 0,q)+(h-l),q)),o.getMetric?(p.position=b,p):a(this)},f.getLength=function(){var b,c;return b=e(a(this)),c=0,a.each(b,function(a,b){return c+=b.nodeValue.length}),c},k=function(b,c,d){var f,g,h,i,j,k;for(f=0,k=e(b),g=0;gc){i=k[g],h=c-f+k[g].nodeValue.length;break}g++}return i?(j=i.nodeValue,a(i).replaceWith([document.createTextNode(j.slice(0,h)),a("",{id:d,text:j.substr(h,1)}),document.createTextNode(j.slice(h+1))]),!0):!1},j=function(b,c){b.find("span#"+c).each(function(){var b,c,d;c=this.previousSibling,b=this.nextSibling,d="",c&&3===c.nodeType&&(d+=c.nodeValue,a(c).remove()),d+=a(this).text(),b&&3===b.nodeType&&(d+=b.nodeValue,a(b).remove()),a(this).replaceWith(document.createTextNode(d))})},e=function(b){var c;return c=[],b.contents().each(function(){1===this.nodeType?c=c.concat(e(a(this))):3===this.nodeType&&c.push(this)}),c},g=function(){var b,c,d,e,f,g;return b=a(a(this).is(a(window))||a(this).is(a(document))?"body":this),g=b.css("writing-mode")||b.css("-webkit-writing-mode")||b.css("-ms-writing-mode")||b.css("-moz-writing-mode")||b.get(0).style.writingMode||b.get(0).style.msWritingMode,void 0===g||"horizontal-tb"===g||"lr-tb"===g||"rl-tb"===g?(f=!1,c=!0,e=!0,d="rtl"===b.css("direction")||"rl-tb"===g):(f=!0,c=!1,e="sideways-left"===b.css("text-orientation")?"rtl"===b.css("direction"):"rtl"!==b.css("direction"),d=void 0===g||"vertical-rl"===g||"tb"===g||"tb-rl"===g),{vertical:f,horizontal:c,ttb:e,rtl:d}},c=function(a,b){if("before"===b)return a.before;if("after"===b)return a.before+a.blockSize;if("center"===b)return a.before+a.blockSize/2;if("scroll"===b)return null===a.scroll?a.before+a.blockSize:a.before+a.blockSize*a.scroll;if("number"==typeof b)return a.before+a.blockSize*b;throw new TypeError("howmuchread: position must be string or number")},i=function(b,c){var d;return c=c||g.call(this),d=void 0,d=c.rtl?c.vertical?h.vertical:h.horizontal:"default","undefined"==typeof b?"default"===d?a(this).scrollLeft():"negative"===d?a(this).scrollLeft()+a(this).width():"reverse"===d?a(this).width()-a(this).scrollLeft():a(this).scrollLeft():"default"===d?a(this).scrollLeft(b):"negative"===d?a(this).scrollLeft(b-a(this).width()):"reverse"===d?a(this).scrollLeft(a(this).width()-b):a(this).scrollLeft(b)},d=function(b,c){var d,e;return c=c||g.call(b),d=a(b),e=d.is(a(window))?{top:a(window).scrollTop(),left:a(window).scrollLeft(),scrollHeight:a(document).height(),scrollWidth:a(document).width()}:d.is(a(document))?{top:0,left:0,scrollHeight:a(document).height(),scrollWidth:a(document).width()}:d.offset(),e.width=d.width(),e.height=d.height(),e.right=a(document).width()-e.left-e.width,e.bottom=a(document).height()-e.top-e.height,e.scrollTop=d.scrollTop(),e.scrollLeft=i(void 0,c),null==e.scrollHeight&&(e.scrollHeight=d.get(0).scrollHeight),null==e.scrollWidth&&(e.scrollWidth=d.get(0).scrollWidth),c.horizontal?(e.blockSize=e.height,e.inlineSize=e.width,c.ttb?(e.before=e.top,e.after=e.bottom,e.scroll=function(){switch(e.scrollHeight-e.height){case 0:return null;default:return e.scrollTop/(e.scrollHeight-e.height)}}()):(e.before=e.bottom,e.after=e.top,e.scroll=function(){switch(e.scrollHeight-e.height){case 0:return null;default:return 1-e.scrollTop/(e.scrollHeight-e.height)}}()),c.rtl?(e.start=e.right,e.end=e.left):(e.start=e.left,e.end=e.right)):c.vertical&&(e.blockSize=e.width,e.inlineSize=e.height,c.rtl?(e.before=e.right,e.after=e.left,e.scroll=function(){switch(e.scrollWidth-e.width){case 0:return null;default:return 1-e.scrollLeft/(e.scrollWidth-e.width)}}()):(e.before=e.left,e.after=e.right,e.scroll=function(){switch(e.scrollWidth-e.width){case 0:return null;default:return e.scrollLeft/(e.scrollWidth-e.width)}}()),c.ttb?(e.start=e.top,e.end=e.bottom):(e.start=e.bottom,e.end=e.top)),e},a(document).ready(function(){/*! jQuery RTL Scroll Type Detector | Copyright (c) 2012 Wei-Ko Kao | MIT License */ var b,c;c=void 0,c=a("
",{dir:"rtl",text:"A",css:{fontSize:"14px",width:"1px",height:"1px",position:"absolute",top:"-1000px",overflow:"scroll"}}).appendTo("body")[0],h.horizontal="reverse",c.scrollLeft>0?h.horizontal="default":(c.scrollLeft=1,0===c.scrollLeft&&(h.horizontal="negative")),a(c).remove(),b=a("
",{text:"A",css:{fontSize:"14px",width:"1px",height:"1px",position:"absolute",top:"-1000px",overflow:"scroll",writingMode:"tb-rl",OWritingMode:"vertical-rl",MsWritingMode:"tb-rl",MozWritingMode:"vertical-rl",WebkitWritingMode:"vertical-rl"}}),c=b.css({writingMode:"vertical-rl",MsWritingMode:"vertical-rl"}).appendTo("body")[0],h.vertical="reverse",c.scrollLeft>0?h.vertical="default":(c.scrollLeft=1,0===c.scrollLeft&&(h.vertical="negative")),a(c).remove()}),b=function(a,b){var c,d,e,f;for(e=0,c=a;e!==c;)d=Math.floor((e+c)/2),f=b(d),f?c=d:e=d+1;return e}}).call(this);