(function(f){var b=function(n,m,l,k){this.left=Math.round(n);this.top=Math.round(m);this.width=Math.round(l);this.height=Math.round(k);this.right=this.left+this.width;this.bottom=this.top+this.height;this.equals=function(o){return this.left===o.left&&this.top===o.top&&this.width===o.width&&this.height===o.height};this.area=function(){return this.width*this.height};this.intersection=function(s){var u=Math.max(this.left,s.left);var q=Math.min(this.right,s.right);var t=Math.max(this.top,s.top);var p=Math.min(this.bottom,s.bottom);var r=q-u;var o=p-t;if(r<0||o<0){return undefined}return new b(u,t,r,o)};this.envelope=function(s){var u=Math.min(this.left,s.left);var q=Math.max(this.right,s.right);var t=Math.min(this.top,s.top);var p=Math.max(this.bottom,s.bottom);var r=q-u;var o=p-t;return new b(u,t,r,o)};var j=undefined;this.bind=function(o){if(j===undefined){j=new h(this);f(window).bind("scroll",j.check).bind("resize",j.check)}j.bind(o)},this.unbind=function(o){if(j!==undefined){j.unbind(o);if(j.callbacks.length===0){f(window).unbind("scroll",j.check).unbind("resize",j.check);j=undefined}}};this.check=function(){if(j){j.check()}};this.fracs=function(){f.fracs.fracs(this)}};var e=function(n,k,m,o,j,l){this.rects=n===undefined?undefined:{document:n,element:k,viewport:m};this.visible=o||0;this.viewport=j||0;this.possible=l||0;this.equals=function(p){return this.fracsEqual(p)&&this.rectsEqual(p)};this.fracsEqual=function(p){return this.visible===p.visible&&this.viewport===p.viewport&&this.possible===p.possible};this.rectsEqual=function(p){if(this.rects===undefined||p.rects===undefined){return this.rects===p.rects}return this.rects.document.equals(p.rects.document)&&this.rects.element.equals(p.rects.element)&&this.rects.viewport.equals(p.rects.viewport)}};var a=function(k,j){this.element=k;this.fracs=j;this.update=function(){var l=f.fracs.fracs(this.element);var m=this.fracs===undefined||!this.fracs.equals(l);this.fracs=l;return m}};var h=function(j){this.target=j;this.callbacks=[];this.prevFracs=undefined;this.bind=function(k){if(k!==undefined&&f.inArray(k,this.callbacks)===-1){this.callbacks.push(k)}};this.unbind=function(l){if(l===undefined){this.callbacks=[]}else{var k=f.inArray(l,this.callbacks);if(k>=0){this.callbacks.splice(k,1)}}};this.check=f.proxy(function(){var k=this.target instanceof HTMLElement?f.fracs.rect(this.target):this.target;var l=f.fracs.fracs(k,f.fracs.viewport());if(this.prevFracs===undefined||!this.prevFracs.equals(l)){f.each(this.callbacks,f.proxy(function(m,n){n.call(this.target,l,this.prevFracs)},this));this.prevFracs=l}},this)};var c=function(k,l,n){this.targets=[];this.callback=n;this.prevBest=undefined;this.property=l;for(var j in k){var m=k[j];if(m instanceof HTMLElement){this.targets.push(new a(m))}}this.check=f.proxy(function(){var r=undefined;var p=f.fracs.viewport();for(var o in this.targets){var q=this.targets[o];q.update();if(r===undefined||q.fracs[this.property]>r.fracs[this.property]){r=q}}if(r.fracs[this.property]==0){r=undefined}if(this.prevBest!==r){this.callback.call(this,r,this.prevBest);this.prevBest=r}},this)};var i=function(k,j){if(!(k instanceof HTMLElement)||k.nodeName.toLowerCase()!=="canvas"){return undefined}var m={crop:false,duration:100,focusWidth:0.5,focusHeight:0.5,styles:[{selector:"header,footer,section,article",fillStyle:"rgb(230,230,230)"},{selector:"h1",fillStyle:"rgb(240,140,060)"},{selector:"h2",fillStyle:"rgb(200,100,100)"},{selector:"h3",fillStyle:"rgb(100,200,100)"},{selector:"h4",fillStyle:"rgb(100,100,200)"}],viewportStyle:{fillStyle:"rgba(228,77,38,0.3)"},viewportDragStyle:{fillStyle:"rgba(228,77,38,0.6)"},invertViewport:false};this.settings=f.extend({},m,j);this.context=k.getContext("2d");this.$canvas=f(k);this.width=this.$canvas.attr("width");this.height=this.$canvas.attr("height");this.$window=f(window);this.$htmlBody=f("html,body");this.docRect;this.vpRect;this.scale;this.drag=false;var l=this;this.init=function(){var n=f.proxy(this.scroll,this);this.$canvas.css("cursor","pointer").mousedown(f.proxy(function(o){this.drag=true;this.scroll(o);this.$canvas.css("cursor","crosshair").addClass("dragOn");this.$htmlBody.css("cursor","crosshair");this.$window.bind("mousemove",n).one("mouseup",f.proxy(function(p){this.$canvas.css("cursor","pointer").removeClass("dragOn");this.$htmlBody.css("cursor","auto");this.$window.unbind("mousemove",n);this.drag=false;this.draw()},this));return false},this)).attr("unselectable","on").css("-webkit-user-select","none").css("-khtml-user-select","none").css("-moz-user-select","none").css("-o-user-select","none").css("user-select","none").each(function(){this.onselectstart=function(){return false}});this.$window.bind("load resize scroll",f.proxy(this.draw,this));this.draw()};this.applyStyles=function(n){for(idx in this.settings.styles){var o=this.settings.styles[idx];f(o.selector).each(function(){l.drawElement(n,this,o.strokeWidth,o.strokeStyle,o.fillStyle)})}};this.drawElement=function(p,r,t,s,o){var n=f(r);var q=n.fracs("rect");t=t==="auto"?n.css("border-width"):t;s=s==="auto"?n.css("border-color"):s;o=o==="auto"?n.css("background-color"):o;this.drawRect(p,q,t,s,o)};this.drawRect=function(p,q,n,s,o,r){r=r||false;if(n!==undefined&&this.scale!==undefined){n=n>0.2/this.scale?n:0.2/this.scale}if(s!==undefined||o!==undefined){if(r===false){p.beginPath();p.rect(q.left,q.top,q.width,q.height);if(o!==undefined){p.fillStyle=o;p.fill()}if(s!==undefined){p.lineWidth=n;p.strokeStyle=s;p.stroke()}}else{if(o!==undefined){p.beginPath();p.rect(0,0,this.docRect.width,q.top);p.rect(0,q.top,q.left,q.height);p.rect(q.right,q.top,this.docRect.right-q.right,q.height);p.rect(0,q.bottom,this.docRect.width,this.docRect.bottom-q.bottom);p.fillStyle=o;p.fill()}if(s!==undefined){p.beginPath();p.rect(q.left,q.top,q.width,q.height);p.lineWidth=n;p.strokeStyle=s;p.stroke()}}}};this.drawViewport=function(){if(this.drag===true&&this.settings.viewportDragStyle!==undefined){var n=this.settings.viewportDragStyle.storkeWidth;var p=this.settings.viewportDragStyle.strokeStyle;var o=this.settings.viewportDragStyle.fillStyle}else{var n=this.settings.viewportStyle.storkeWidth;var p=this.settings.viewportStyle.strokeStyle;var o=this.settings.viewportStyle.fillStyle}this.drawRect(this.context,this.vpRect,n,p,o,this.settings.invertViewport)};this.draw=function(){this.docRect=f.fracs.document();this.vpRect=f.fracs.viewport();var o=this.width/this.docRect.width;var n=this.height/this.docRect.height;this.scale=o<n?o:n;if(this.settings.crop){this.$canvas.attr("width",this.docRect.width*this.scale).attr("height",this.docRect.height*this.scale)}this.context.clearRect(0,0,this.$canvas.width(),this.$canvas.height());this.context.scale(this.scale,this.scale);this.applyStyles(this.context);this.drawViewport();this.context.scale(1/this.scale,1/this.scale)};this.scroll=function(p){var o=this.$canvas.fracs("rect");var n=p.pageX-o.left;var q=p.pageY-o.top;f.fracs.scrollTo(n/this.scale-this.vpRect.width*this.settings.focusWidth,q/this.scale-this.vpRect.height*this.settings.focusHeight,this.settings.duration)};this.init()};f.fracs={internal:{Rect:b,FracsResult:e,FracsElement:a,FracsData:h,FracsGroup:c,Outline:i},document:function(){var j=f(document);return new b(0,0,j.width(),j.height())},viewport:function(){var j=f(window);return new b(j.scrollLeft(),j.scrollTop(),j.width(),j.height())},rect:function(k){var j=f(k);var l=j.offset();return new b(l.left,l.top,j.outerWidth(),j.outerHeight())},fracs:function(o,l){o=o instanceof HTMLElement?f.fracs.rect(o):o;l=l||f.fracs.viewport();var p=o.intersection(l);if(p===undefined){return new e()}var n=new b(p.left-o.left,p.top-o.top,p.width,p.height);var k=new b(p.left-l.left,p.top-l.top,p.width,p.height);var j=p.area();var m=Math.min(o.width,l.width)*Math.min(o.height,l.height);return new e(p,n,k,1*j/o.area(),1*j/l.area(),1*j/m)},round:function(k,j){if(isNaN(j)||j<=0){return Math.round(k)}return Math.round(k*Math.pow(10,j))/Math.pow(10,j)},scrollTo:function(l,k,j){j=j!==undefined?j:1000;f("html,body").stop(true).animate({scrollLeft:l,scrollTop:k},j)},scroll:function(l,k,j){j=j!==undefined?j:1000;var m=f(window);f("html,body").stop(true).animate({scrollLeft:m.scrollLeft()+l,scrollTop:m.scrollTop()+k},j)},scrollState:function(){var l=f.fracs.document();var k=f.fracs.viewport();var m=l.width-k.width;var j=l.height-k.height;return{right:m<=0?undefined:k.left/m,bottom:j<=0?undefined:k.top/j}}};var g="fracs";var d={bind:function(j){return this.each(function(){var l=f(this);var k=l.data(g);if(k===undefined){k=new h(this);l.data(g,k);f(window).bind("scroll",k.check).bind("resize",k.check)}k.bind(j)})},unbind:function(j){return this.each(function(){var l=f(this);var k=l.data(g);if(k!==undefined){k.unbind(j);if(k.callbacks.length===0){l.removeData(g);f(window).unbind("scroll",k.check).unbind("resize",k.check)}}})},check:function(){return this.each(function(){var j=f(this).data(g);if(j){j.check()}})},fracs:function(){return f.fracs.fracs(f.fracs.rect(this.get(0)),f.fracs.viewport())},rect:function(){return f.fracs.rect(this.get(0))},max:function(l,n){if(n instanceof Function){var k=new c(this,l,n);f(window).bind("scroll",k.check).bind("resize",k.check);k.check();return this}else{if(f.inArray(l,["possible","visible","viewport"])>=0){var j=[];var m=undefined;this.each(function(){var o=f.fracs.fracs(this);if(m===undefined||o[l]>m){j=[this];m=o[l]}else{if(o[l]===m){j.push(this)}}});return f(j)}if(f.inArray(l,["width","height","left","right","top","bottom"])>=0){var j=[];var m=undefined;this.each(function(){var o=f.fracs.rect(this);if(m===undefined||o[l]>m){j=[this];m=o[l]}else{if(o[l]===m){j.push(this)}}});return f(j)}return this}},min:function(l){if(f.inArray(l,["possible","visible","viewport"])>=0){var k=[];var j=undefined;this.each(function(){var m=f.fracs.fracs(this);if(j===undefined||m[l]<j){k=[this];j=m[l]}else{if(m[l]===j){k.push(this)}}});return f(k)}if(f.inArray(l,["width","height","left","right","top","bottom"])>=0){var k=[];var j=undefined;this.each(function(){var m=f.fracs.rect(this);if(j===undefined||m[l]<j){k=[this];j=m[l]}else{if(m[l]===j){k.push(this)}}});return f(k)}return this},envelope:function(){var j=undefined;this.each(function(){var k=f.fracs.rect(this);j=j===undefined?k:j.envelope(k)});return j},scrollTo:function(j,l,m){j=j||0;l=l||0;var k=f.fracs.rect(this.get(0));f.fracs.scrollTo(k.left-j,k.top-l,m);return this},softLink:function(j,k,l){return this.filter("a[href^=#]").each(function(){var n=f(this);var m=n.attr("href");n.click(function(){f(m).fracs("scrollTo",j,k,l)})})},outline:function(j){return this.each(function(){if(j==="redraw"){var k=f(this).data("outline");if(k!==undefined){k.draw()}}else{var k=new i(this,j);if(k!==undefined){f(this).data("outline",k)}}})}};f.fn.fracs=function(j){if(d[j]){return d[j].apply(this,Array.prototype.slice.call(arguments,1))}else{if(j===undefined){return d.fracs.apply(this,arguments)}else{if(j instanceof Function){return d.bind.apply(this,arguments)}else{f.error("Method "+j+" does not exist on jQuery.fracs")}}}}})(jQuery);
