(function(g){g.fn.touchScroll=function(l){l=g.extend({mouse:false,bars:"inside",coast:2E3},l);g.easing.touchScroll||g.extend(g.easing,{touchScroll:function(v,c,h,b,k){return b*((c=c/k-1)*c*c+1)+h}});return this.each(function(v,c){var h=g(c),b=[{x:0,y:0,t:0},{x:0,y:0,t:0}],k=function(){var d=false,e=true,i=false,n=null;return{start:function(a){d=true;e=false;h.stop();if(a.type=="touchstart"){n=a.target;b[0].x=a.originalEvent.targetTouches[0].clientX;b[0].y=a.originalEvent.targetTouches[0].clientY}else{b[0].x=
a.clientX;b[0].y=a.clientY}b[0].t=(new Date).getTime();b[1].x=b[0].x;b[1].y=b[0].y;b[1].t=b[0].t;o.update();o.show();a.preventDefault()},update:function(a){if(!(e||a.type=="touchmove"&&a.target!=n)){i=true;var f,m;if(a.type=="touchmove"){f=a.originalEvent.targetTouches[0].clientX;m=a.originalEvent.targetTouches[0].clientY}else{f=a.clientX;m=a.clientY}c.scrollTop+=b[0].y-m;c.scrollLeft+=b[0].x-f;o.update();b[1].x=b[0].x;b[1].y=b[0].y;b[1].t=b[0].t;b[0].x=f;b[0].y=m;b[0].t=(new Date).getTime();a.preventDefault()}},
end:function(a){if(!(!d||a.type=="touchend"&&a.target!=n)){d=false;e=true;if(!i&&a.type=="touchend"){a=a.originalEvent;var f=document.createEvent("MouseEvents");f.initMouseEvent("click",true,true,a.view,a.detail,a.screenX,a.screenY,a.clientX,a.clientY,a.ctrlKey,a.altKey,a.shiftKey,a.metaKey,a.button,a.relatedTarget);a.changedTouches[0].target.dispatchEvent(f);o.hide();return i=false}i=false;f=50/Math.max(1,Math.sqrt(b[0].t-b[1].t));h.stop(true,true).animate({scrollTop:c.scrollTop+f*(b[1].y-b[0].y),
scrollLeft:c.scrollLeft+f*(b[1].x-b[0].x)},{duration:l.coast,easing:"touchScroll",step:o.update,complete:o.hide});a.preventDefault()}}}}();l.mouse&&h.css("overflow","hidden");var o=function(){for(var d=null,e=null,i=l.bars=="outside",n=navigator.userAgent.indexOf("iPad")!=-1,a=0,f=h;!f.is("body");f=f.parent()){var m=f.css("z-index");isNaN(m)||(a=Math.max(a,m))}return{hide:function(){d&&d.stop(true,true).fadeOut();e&&e.stop(true,true).fadeOut()},show:function(){if(l.bars){if(!d&&h.innerHeight()<c.scrollHeight){d=
g("<div></div>").css({display:"none",position:"absolute","z-index":a,"margin-left":i?1:-8,width:5,"border-radius":"2.5px","-webkit-border-radius":"2.5px","-moz-border-radius":"2.5px",opacity:0.5,border:"1px solid #eee","background-color":"#333"});g(document.body).append(d)}if(!e&&h.innerWidth()<c.scrollWidth){e=g("<div></div>").css({display:"none",position:"absolute","margin-top":i?1:-8,height:5,"border-radius":"2.5px","-webkit-border-radius":"2.5px","-moz-border-radius":"2.5px",opacity:0.5,border:"1px solid #eee",
"background-color":"#333"});g(document.body).append(e)}}d&&d.stop(true,true).show();e&&e.stop(true,true).show()},update:function(){var r=h.offset(),p=h.innerHeight(),q=h.innerWidth(),t=n?-g(window).scrollTop():0,u=n?-g(window).scrollLeft():0;if(d){var s=c.scrollTop/(c.scrollHeight-p),j=p-d.outerHeight();if(e&&!i)j-=e.outerHeight();i||(j-=2);j*=s;j+=r.top;i||(j+=1);d.css({top:j+t,left:r.left+q+u,height:p*p/(p+c.scrollHeight)})}if(e){s=c.scrollLeft/(c.scrollWidth-q);j=q-e.outerWidth();if(d&&!i)j-=d.outerWidth();
i||(j-=2);j*=s;j+=r.left;i||(j+=1);e.css({top:r.top+p+t,left:j+u,width:q*q/(q+c.scrollWidth)})}}}}();g(document).bind("touchmove",k.update).bind("touchend",k.end);h.bind("touchstart",k.start);if(l.mouse){g(document).bind("mousemove",k.update).bind("mouseup",k.end);h.bind("mousedown",k.start)}})}})(jQuery);