/*
 Copyright (c) 2006 Kelvin Luck (kelvin AT kelvinluck DOT com || http://www.kelvinluck.com)
 Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.

 See http://kelvinluck.com/assets/jquery/jScrollPane/
 $Id: jScrollPane.js 56 2009-02-23 03:50:43Z kelvin.luck $
*/
(function(b){b.jScrollPane={active:[]};b.fn.jScrollPane=function(c){c=b.extend({},b.fn.jScrollPane.defaults,c);var l=function(){return false};return this.each(function(){var d=b(this);d.css("overflow","hidden");var Q=this;if(b(this).parent().is(".jScrollPaneContainer")){var R=c.maintainPosition?d.position().top:0,h=b(this).parent(),k=h.innerWidth(),g=h.outerHeight(),o=g;b(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown",h).remove();d.css({top:0})}else{R=0;this.originalPadding=d.css("paddingTop")+
" "+d.css("paddingRight")+" "+d.css("paddingBottom")+" "+d.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(d.css("paddingLeft"))||0)+(parseInt(d.css("paddingRight"))||0);k=d.innerWidth();o=g=d.innerHeight();d.wrap(b("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:g+"px",width:k+"px"}).attr("tabindex",c.tabIndex));b(document).bind("emchange",function(){d.jScrollPane(c)})}if(c.reinitialiseOnImageLoad){var s=b.data(Q,"jScrollPaneImagesToLoad")||b("img",d),S=[];s.length&&
s.each(function(a,e){b(this).bind("load readystatechange",function(){if(b.inArray(a,S)==-1){S.push(e);s=b.grep(s,function(ba){return ba!=e});b.data(Q,"jScrollPaneImagesToLoad",s);var f=b.extend(c,{reinitialiseOnImageLoad:false});d.jScrollPane(f)}}).each(function(){if(this.complete||this.complete===undefined)this.src=this.src})})}h={height:"auto",width:k-c.scrollbarWidth-c.scrollbarMargin-this.originalSidePaddingTotal+"px"};if(c.scrollbarOnLeft)h.paddingLeft=c.scrollbarMargin+c.scrollbarWidth+"px";
else h.paddingRight=c.scrollbarMargin+"px";d.css(h);var t=d.outerHeight(),y=g/t;if(y<0.99){var j=d.parent();j.append(b("<div></div>").attr({className:"jScrollPaneTrack"}).css({width:c.scrollbarWidth+"px"}).append(b("<div></div>").attr({className:"jScrollPaneDrag"}).css({width:c.scrollbarWidth+"px"}).append(b("<div></div>").attr({className:"jScrollPaneDragTop"}).css({width:c.scrollbarWidth+"px"}),b("<div></div>").attr({className:"jScrollPaneDragBottom"}).css({width:c.scrollbarWidth+"px"}))));k=b(">.jScrollPaneTrack",
j);var z=b(">.jScrollPaneTrack .jScrollPaneDrag",j),u,p=[],q,r=function(){if(q>4||q%4==0)m(i+u*M);q++};c.enableKeyboardNavigation&&j.bind("keydown.jscrollpane",function(a){switch(a.keyCode){case 38:u=-1;q=0;r();p[p.length]=setInterval(r,100);return false;case 40:u=1;q=0;r();p[p.length]=setInterval(r,100);return false;case 32:case 33:case 34:return false;default:}}).bind("keyup.jscrollpane",function(a){if(a.keyCode==38||a.keyCode==40){for(a=0;a<p.length;a++)clearInterval(p[a]);return false}});if(c.showArrows){var A,
T,B=function(){b("html").unbind("mouseup",B);b("html").unbind("mouseout",B);A.removeClass("jScrollActiveArrowButton");clearInterval(T)},U=function(){b("html").bind("mouseup",B);b("html").bind("mouseout",B);A.addClass("jScrollActiveArrowButton");q=0;r();T=setInterval(r,100)};j.append(b("<a></a>").attr({href:"#",className:"jScrollArrowUp",tabindex:-1}).css({width:c.scrollbarWidth+"px"}).html("Scroll up").bind("mousedown",function(){A=b(this);u=-1;U();this.blur();return false}).bind("click",l),b("<a></a>").attr({href:"#",
className:"jScrollArrowDown",tabindex:-1}).css({width:c.scrollbarWidth+"px"}).html("Scroll down").bind("mousedown",function(){A=b(this);u=1;U();this.blur();return false}).bind("click",l));var V=b(">.jScrollArrowUp",j),W=b(">.jScrollArrowDown",j);if(c.arrowSize){o=g-c.arrowSize-c.arrowSize;k.css({height:o+"px",top:c.arrowSize+"px"})}else{h=V.height();c.arrowSize=h;o=g-h-W.height();k.css({height:o+"px",top:h+"px"})}}var N=b(this).css({position:"absolute",overflow:"visible"}),v,n,M,i=0,C=y*g/2,D=function(a,
e){var f=e=="X"?"Left":"Top";return a["page"+e]||a["client"+e]+(document.documentElement["scroll"+f]||document.body["scroll"+f])||0},E=function(){return false},G=function(){F();v=z.offset(false);v.top-=i;n=o-z[0].offsetHeight;M=2*c.wheelSpeed*n/t},H=function(){b("html").unbind("mouseup",H).unbind("mousemove",X);b("html").unbind("mouseleave",H);C=y*g/2;b.browser.msie&&b("html").unbind("dragstart",E).unbind("selectstart",E)},m=function(a){i=a=a<0?0:a>n?n:a;z.css({top:a+"px"});var e=a/n;d.data("jScrollPanePosition",
(g-t)*-e);N.css({top:(g-t)*e+"px"});d.trigger("scroll");if(c.showArrows){V[a==0?"addClass":"removeClass"]("disabled");W[a==n?"addClass":"removeClass"]("disabled")}},X=function(a){m(D(a,"Y")-v.top-C)};h=Math.max(Math.min(y*(g-c.arrowSize*2),c.dragMaxHeight),c.dragMinHeight);z.css({height:h+"px"}).bind("mousedown",function(a){G();C=D(a,"Y")-i-v.top;b("html").bind("mouseup",H).bind("mousemove",X);b("html").bind("mouseleave",H);b.browser.msie&&b("html").bind("dragstart",E).bind("selectstart",E);return false});
var Y,I,Z,$=function(){if(I>8||I%4==0)m(i-(i-Z)/2);I++},J=function(){clearInterval(Y);b("html").unbind("mouseup",J).unbind("mouseout",J).unbind("mousemove",O)},O=function(a){Z=D(a,"Y")-v.top-C};k.bind("mousedown",function(a){G();O(a);I=0;b("html").bind("mouseup",J).bind("mouseout",J).bind("mousemove",O);Y=setInterval($,100);$();return false});j.bind("mousewheel",function(a,e){G();F();a=i;m(i-e*M);return a==i});var K,P;function ca(){var a=(K-i)/c.animateStep;if(a>1||a<-1)m(i+a);else{m(K);F()}}var F=
function(){if(P){clearInterval(P);delete K}},w=function(a,e){if(typeof a=="string"){$e=b(a,d);if(!$e.length)return;a=$e.offset().top-d.offset().top}j.scrollTop(0);F();var f=t-g;a=a>f?f:a;d.data("jScrollPaneMaxScroll",f);a=a/f*n;if(e||!c.animateTo)m(a);else{K=a;P=setInterval(ca,c.animateInterval)}};d[0].scrollTo=w;d[0].scrollBy=function(a){var e=-parseInt(N.css("top"))||0;w(e+a)};G();w(-R,true);b("*",this).bind("focus",function(){for(var a=b(this),e=0;a[0]!=d[0];){e+=a.position().top;a=a.offsetParent()}a=
-parseInt(N.css("top"))||0;var f=a+g;if(!(e>a&&e<f)){f=e-c.scrollbarMargin;if(e>a)f+=b(this).height()+15+c.scrollbarMargin-g;w(f)}});b(document).bind("click",function(a){$target=b(a.target);if($target.is("a")){var e=$target.attr("href");if(e&&e!="#"&&e.substr(0,1)=="#")setTimeout(function(){w(e,!c.animateToInternalLinks)},b.browser.safari?100:0)}});function da(){b(document).bind("mousemove.jScrollPaneDragging",ea);b(document).bind("mouseup.jScrollPaneDragging",fa)}var L,x;function ga(){direction=
L<0?-1:1;d[0].scrollBy(L/2)}function aa(){if(x){clearInterval(x);x=undefined}}function ea(a){var e=d.parent().offset().top,f=e+g;a=D(a,"Y");L=a<e?a-e:a>f?a-f:0;if(L==0)aa();else x||(x=setInterval(ga,100))}function fa(){b(document).unbind("mousemove.jScrollPaneDragging").unbind("mouseup.jScrollPaneDragging");aa()}j.bind("mousedown.jScrollPane",da);b.jScrollPane.active.push(d[0])}else{d.css({height:g+"px",width:k-this.originalSidePaddingTotal+"px",padding:this.originalPadding});d.parent().unbind("mousewheel").unbind("mousedown.jScrollPane").unbind("keydown.jscrollpane").unbind("keyup.jscrollpane")}})};
b.fn.jScrollPaneRemove=function(){b(this).each(function(){$this=b(this);var c=$this.parent();if(c.is(".jScrollPaneContainer")){$this.css({top:"",height:"",width:"",padding:"",overflow:"",position:""});c.after($this).remove()}})};b.fn.jScrollPane.defaults={scrollbarWidth:10,scrollbarMargin:5,wheelSpeed:18,showArrows:false,arrowSize:0,animateTo:false,dragMinHeight:1,dragMaxHeight:99999,animateInterval:100,animateStep:3,maintainPosition:true,scrollbarOnLeft:false,reinitialiseOnImageLoad:false,tabIndex:0,
enableKeyboardNavigation:true,animateToInternalLinks:false};b(window).bind("unload",function(){for(var c=b.jScrollPane.active,l=0;l<c.length;l++)c[l].scrollTo=c[l].scrollBy=null})})(jQuery);

