﻿jQuery.jScrollPane={active:[]};jQuery.fn.jScrollPane=function(a){a=jQuery.extend({scrollbarWidth:10,scrollbarMargin:5,wheelSpeed:18,showArrows:false,arrowSize:0,animateTo:false,dragMinHeight:1,dragMaxHeight:99999,animateInterval:100,animateStep:3,maintainPosition:true,scrollbarOnLeft:false},a);return this.each(function(){var b=jQuery(this);if(jQuery(this).parent().is(".jScrollPaneContainer")){var S=a.maintainPosition?b.offset({relativeTo:jQuery(this).parent()[0]}).top:0,j=jQuery(this).parent(),B=j.innerWidth(),c=j.outerHeight(),i=c;if(j.unmousewheel)j.unmousewheel();jQuery(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown",j).remove();b.css({top:0})}else{var S=0;this.originalPadding=b.css("paddingTop")+" "+b.css("paddingRight")+" "+b.css("paddingBottom")+" "+b.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(b.css("paddingLeft"))||0)+(parseInt(b.css("paddingRight"))||0);var B=b.innerWidth(),c=b.innerHeight(),i=c;b.wrap(jQuery("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:c+"px",width:B+"px"}));jQuery(document).bind("emchange",function(){b.jScrollPane(a)})}var X=this.originalSidePaddingTotal,A={height:"auto",width:B-a.scrollbarWidth-a.scrollbarMargin-X+"px"};if(a.scrollbarOnLeft)A.paddingLeft=a.scrollbarMargin+a.scrollbarWidth+"px";else A.paddingRight=a.scrollbarMargin+"px";b.css(A);var q=b.outerHeight(),r=c/q;if(r<.99){var e=b.parent();e.append(jQuery("<div></div>").attr({className:"jScrollPaneTrack"}).css({width:a.scrollbarWidth+"px"}).append(jQuery("<div></div>").attr({className:"jScrollPaneDrag"}).css({width:a.scrollbarWidth+"px"}).append(jQuery("<div></div>").attr({className:"jScrollPaneDragTop"}).css({width:a.scrollbarWidth+"px"}),jQuery("<div></div>").attr({className:"jScrollPaneDragBottom"}).css({width:a.scrollbarWidth+"px"}))));var D=jQuery(">.jScrollPaneTrack",e),u=jQuery(">.jScrollPaneTrack .jScrollPaneDrag",e);if(a.showArrows){var k,v,F,n,G=function(){if(n>4||n%4==0)f(d+v*w);n++},L=function(){jQuery("html").unbind("mouseup",L);k.removeClass("jScrollActiveArrowButton");clearInterval(F)},J=function(){jQuery("html").bind("mouseup",L);k.addClass("jScrollActiveArrowButton");n=0;G();F=setInterval(G,100)};e.append(jQuery("<a></a>").attr({href:"javascript:;",className:"jScrollArrowUp"}).css({width:a.scrollbarWidth+"px"}).html("Scroll up").bind("mousedown",function(){k=jQuery(this);v=-1;J();this.blur();return false}),jQuery("<a></a>").attr({href:"javascript:;",className:"jScrollArrowDown"}).css({width:a.scrollbarWidth+"px"}).html("Scroll down").bind("mousedown",function(){k=jQuery(this);v=1;J();this.blur();return false}));var Q=jQuery(">.jScrollArrowUp",e),O=jQuery(">.jScrollArrowDown",e);if(a.arrowSize){i=c-a.arrowSize-a.arrowSize;D.css({height:i+"px",top:a.arrowSize+"px"})}else{var z=Q.height();a.arrowSize=z;i=c-z-O.height();D.css({height:i+"px",top:z+"px"})}}var R=jQuery(this).css({position:"absolute",overflow:"visible"}),h,g,w,d=0,s=r*c/2,E=function(b,a){var c=a=="X"?"Left":"Top";return b["page"+a]||b["client"+a]+(document.documentElement["scroll"+c]||document.body["scroll"+c])||0},m=function(){return false},t=function(){o();h=u.offset(false);h.top-=d;g=i-u[0].offsetHeight;w=2*a.wheelSpeed*g/q},V=function(a){t();s=E(a,"Y")-d-h.top;jQuery("html").bind("mouseup",P).bind("mousemove",N);if(jQuery.browser.msie)jQuery("html").bind("dragstart",m).bind("selectstart",m);return false},P=function(){jQuery("html").unbind("mouseup",P).unbind("mousemove",N);s=r*c/2;if(jQuery.browser.msie)jQuery("html").unbind("dragstart",m).unbind("selectstart",m)},f=function(e){e=e<0?0:e>g?g:e;d=e;u.css({top:e+"px"});var f=e/g;R.css({top:(c-q)*f+"px"});b.trigger("scroll");if(a.showArrows){Q[e==0?"addClass":"removeClass"]("disabled");O[e==g?"addClass":"removeClass"]("disabled")}},N=function(a){f(E(a,"Y")-h.top-s)},W=Math.max(Math.min(r*(c-a.arrowSize*2),a.dragMaxHeight),a.dragMinHeight);u.css({height:W+"px"}).bind("mousedown",V);var H,p,I,M=function(){if(p>8||p%4==0)f(d-(d-I)/2);p++},K=function(){clearInterval(H);jQuery("html").unbind("mouseup",K).unbind("mousemove",y)},y=function(a){I=E(a,"Y")-h.top-s},U=function(a){t();y(a);p=0;jQuery("html").bind("mouseup",K).bind("mousemove",y);H=setInterval(M,100);M()};D.bind("mousedown",U);if(e.mousewheel)e.mousewheel(function(e,b){t();o();var c=d;f(d-b*w);var a=c!=d;return !a},false);var l,x;function T(){var b=(l-d)/a.animateStep;if(b>1||b<-1)f(d+b);else{f(l);o()}}var o=function(){if(x){clearInterval(x);delete l}},C=function(d,h){if(typeof d=="string"){$e=jQuery(d,this);if(!$e.length)return;d=$e.offset().top-b.offset().top}o();var e=-d/(c-q)*g;if(h||!a.animateTo)f(e);else{l=e;x=setInterval(T,a.animateInterval)}};b[0].scrollTo=C;b[0].scrollBy=function(b){var a=-parseInt(R.css("top"))||0;C(a+b)};t();C(-S,true);jQuery.jScrollPane.active.push(b[0])}else b.css({height:c+"px",width:B-this.originalSidePaddingTotal+"px",padding:this.originalPadding})})};jQuery(window).bind("unload",function(){var b=jQuery.jScrollPane.active;for(var a=0;a<b.length;a++)b[a].scrollTo=b[a].scrollBy=null})
