/* 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 4702 2008-02-09 10:19:47Z kelvin.luck $
 */

/**
 * Replace the vertical scroll bars on any matched elements with a fancy
 * styleable (via CSS) version. With JS disabled the elements will
 * gracefully degrade to the browsers own implementation of overflow:auto.
 * If the mousewheel plugin has been included on the page then the scrollable areas will also
 * respond to the mouse wheel.
 *
 * @example jQuery(".scroll-pane").jScrollPane();
 *
 * @name jScrollPane
 * @type jQuery
 * @param Object	settings	hash with options, described below.
 *								scrollbarWidth	-	The width of the generated scrollbar in pixels
 *								scrollbarMargin	-	The amount of space to leave on the side of the scrollbar in pixels
 *								wheelSpeed		-	The speed the pane will scroll in response to the mouse wheel in pixels
 *								showArrows		-	Whether to display arrows for the user to scroll with
 *								arrowSize		-	The height of the arrow buttons if showArrows=true
 *								animateTo		-	Whether to animate when calling scrollTo and scrollBy
 *								dragMinHeight	-	The minimum height to allow the drag bar to be
 *								dragMaxHeight	-	The maximum height to allow the drag bar to be
 *								animateInterval	-	The interval in milliseconds to update an animating scrollPane (default 100)
 *								animateStep		-	The amount to divide the remaining scroll distance by when animating (default 3)
 *								maintainPosition-	Whether you want the contents of the scroll pane to maintain it's position when you re-initialise it - so it doesn't scroll as you add more content (default true)
 *								scrollbarOnLeft	-	Display the scrollbar on the left side?  (needs stylesheet changes, see examples.html)
 * @return jQuery
 * @cat Plugins/jScrollPane
 * @author Kelvin Luck (kelvin AT kelvinluck DOT com || http://www.kelvinluck.com)
 */
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 K=jQuery(this);if(jQuery(this).parent().is(".jScrollPaneContainer")){var z=A.maintainPosition?K.offset({relativeTo:jQuery(this).parent()[0]}).top:0;var I=jQuery(this).parent();var Y=I.innerWidth();var AA=I.outerHeight();var J=AA;if(I.unmousewheel){I.unmousewheel();}jQuery(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown",I).remove();K.css({top:0});}else{var z=0;this.originalPadding=K.css("paddingTop")+" "+K.css("paddingRight")+" "+K.css("paddingBottom")+" "+K.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(K.css("paddingLeft"))||0)+(parseInt(K.css("paddingRight"))||0);var Y=K.innerWidth();var AA=K.innerHeight();var J=AA;var v=browserIE6?436:432;K.wrap(jQuery("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:v+"px",width:900+"px"}));jQuery(document).bind("emchange",function(AB,AC,p){K.jScrollPane(A);});if(A.autoRefresh){function g(p){window.setTimeout(function(){var AB=parseInt(K.attr("previousInnerHeight"))||parseInt(K.attr("previousInnerHeight","init"));var AC=parseInt(K.innerHeight());if(AB!=AC){K.jScrollPane(A);K.attr("previousInnerHeight",AC);}return g(p);},A.autoRefresh);}g(K);}}var k=this.originalSidePaddingTotal;var h={height:"auto",width:Y-A.scrollbarWidth-A.scrollbarMargin-k+"px"};if(A.scrollbarOnLeft){h.paddingLeft=A.scrollbarMargin+A.scrollbarWidth+"px";}else{h.paddingRight=A.scrollbarMargin+"px";}K.css(h);var i=K.outerHeight();var d=AA/i;if(d<0.99){var E=K.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 w=jQuery(">.jScrollPaneTrack",E);var L=jQuery(">.jScrollPaneTrack .jScrollPaneDrag",E);if(A.showArrows){var b;var y;var O;var m;var e=function(){if(m>4||m%4==0){u(q+y*W);}m++;};var H=function(p){jQuery("html").unbind("mouseup",H);b.removeClass("jScrollActiveArrowButton");clearInterval(O);};var U=function(){jQuery("html").bind("mouseup",H);b.addClass("jScrollActiveArrowButton");m=0;e();O=setInterval(e,100);};E.append(jQuery("<a></a>").attr({href:"javascript:;",className:"jScrollArrowUp"}).css({width:A.scrollbarWidth+"px"}).html("Scroll up").bind("mousedown",function(){b=jQuery(this);y=-1;U();this.blur();return false;}),jQuery("<a></a>").attr({href:"javascript:;",className:"jScrollArrowDown"}).css({width:A.scrollbarWidth+"px"}).html("Scroll down").bind("mousedown",function(){b=jQuery(this);y=1;U();this.blur();return false;}));var M=jQuery(">.jScrollArrowUp",E);var G=jQuery(">.jScrollArrowDown",E);if(A.arrowSize){J=AA-A.arrowSize-A.arrowSize;w.css({height:J+"px",top:A.arrowSize+"px"});}else{var n=M.height();A.arrowSize=n;J=AA-n-G.height();w.css({height:J+"px",top:n+"px"});}}var s=jQuery(this).css({position:"absolute",overflow:"visible"});var B;var T;var W;var q=0;var R=d*AA/2;var V=function(AB,AD){var AC=AD=="X"?"Left":"Top";return AB["page"+AD]||(AB["client"+AD]+(document.documentElement["scroll"+AC]||document.body["scroll"+AC]))||0;};var a=function(){return false;};var r=function(){j();B=L.offset(false);B.top-=q;T=J-L[0].offsetHeight;W=2*A.wheelSpeed*T/i;};var C=function(p){r();R=V(p,"Y")-q-B.top;jQuery("html").bind("mouseup",P).bind("mousemove",c);if(jQuery.browser.msie){jQuery("html").bind("dragstart",a).bind("selectstart",a);}return false;};var P=function(){jQuery("html").unbind("mouseup",P).unbind("mousemove",c);R=d*AA/2;if(jQuery.browser.msie){jQuery("html").unbind("dragstart",a).unbind("selectstart",a);}};var u=function(AB){AB=AB<0?0:(AB>T?T:AB);q=AB;L.css({top:AB+"px"});var AC=AB/T;s.css({top:((AA-i)*AC)+"px"});K.trigger("scroll");if(A.showArrows){M[AB==0?"addClass":"removeClass"]("disabled");G[AB==T?"addClass":"removeClass"]("disabled");}};var c=function(p){u(V(p,"Y")-B.top-R);};var l=Math.max(Math.min(d*(AA-A.arrowSize*2),A.dragMaxHeight),A.dragMinHeight);L.css({height:l+"px"}).bind("mousedown",C);var f;var N;var F;var o=function(){if(N>8||N%4==0){u((q-((q-F)/2)));}N++;};var x=function(){clearInterval(f);jQuery("html").unbind("mouseup",x).unbind("mousemove",Z);};var Z=function(p){F=V(p,"Y")-B.top-R;};var Q=function(p){r();Z(p);N=0;jQuery("html").bind("mouseup",x).bind("mousemove",Z);f=setInterval(o,100);o();};w.bind("mousedown",Q);if(E.mousewheel){E.mousewheel(function(AB,AD){r();j();var AC=q;u(q-AD*W);var p=AC!=q;return !p;},false);}var D;var S;function X(){var p=(D-q)/A.animateStep;if(p>1||p<-1){u(q+p);}else{u(D);j();}}var j=function(){if(S){clearInterval(S);delete D;}};var t=function(AC,p){if(typeof AC=="string"){$e=jQuery(AC,this);if(!$e.length){return ;}AC=$e.offset().top-K.offset().top;}j();var AB=-AC/(AA-i)*T;if(!p||A.animateTo){D=AB;S=setInterval(X,A.animateInterval);}else{u(AB);}};K[0].scrollTo=t;K[0].scrollBy=function(AB){var p=-parseInt(s.css("top"))||0;t(p+AB);};r();t(-z,true);jQuery.jScrollPane.active.push(K[0]);}else{K.css({height:AA+"px",width:Y-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;}});