/* 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.min.js 3579 2007-10-06 17:17:09Z kelvin.luck $
 */

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