/*
 * jScrollPane - v2.0.0beta3 - 2010-08-27
 * http://jscrollpane.kelvinluck.com/
 *
 * Copyright (c) 2010 Kelvin Luck
 * Dual licensed under the MIT and GPL licenses.
 */
(function(b,a,c){b.fn.jScrollPane=function(f){function d(A,J){var ar,L=this,T,af,u,ah,O,U,w,p,at,az,an,i,F,h,j,V,P,aj,S,r,y,ak,aa,ai,D,l,am,aq,v,ao,aC,g,ay,ae=true,K=true,aB=false,k=false,X=b.fn.mwheelIntent?"mwheelIntent.jsp":"mousewheel.jsp";aC=A.css("paddingTop")+" "+A.css("paddingRight")+" "+A.css("paddingBottom")+" "+A.css("paddingLeft");g=(parseInt(A.css("paddingLeft"))||0)+(parseInt(A.css("paddingRight"))||0);al(J);function al(aF){var aJ,aI,aH,aE,aD,aG;ar=aF;if(T==c){A.css({overflow:"hidden",padding:0});af=A.innerWidth()+g;u=A.innerHeight();A.width(af);T=b('<div class="jspPane" />').wrap(b('<div class="jspContainer" />').css({width:af+"px",height:u+"px"}));A.wrapInner(T.parent());ah=A.find(">.jspContainer");T=ah.find(">.jspPane");T.css("padding",aC)}else{A.css("width",null);aG=A.outerWidth()+g!=af||A.outerHeight()!=u;if(aG){af=A.innerWidth()+g;u=A.innerHeight();ah.css({width:af+"px",height:u+"px"})}ay=T.innerWidth();if(!aG&&T.outerWidth()==O&&T.outerHeight()==U){if(az||at){T.css("width",ay+"px");A.css("width",(ay+g)+"px")}return}T.css("width",null);A.css("width",(af+g)+"px");ah.find(">.jspVerticalBar,>.jspHorizontalBar").remove().end()}aJ=T.clone().css("position","absolute");aI=b('<div style="width:1px; position: relative;" />').append(aJ);b("body").append(aI);O=Math.max(T.outerWidth(),aJ.outerWidth());aI.remove();U=T.outerHeight();w=O/af;p=U/u;at=p>1;az=w>1;if(!(az||at)){A.removeClass("jspScrollable");T.css({top:0,width:ah.width()+"px"});n();B();M();ad()}else{A.addClass("jspScrollable");aH=ar.maintainPosition&&(F||V);if(aH){aE=aw();aD=au()}aA();x();C();if(aH){I(aE);H(aD)}G();ab();if(ar.enableKeyboardNavigation){N()}z();if(ar.hijackInternalLinks){m()}}if(ar.autoReinitialise&&!ao){ao=setInterval(function(){al(ar)},ar.autoReinitialiseDelay)}else{if(!ar.autoReinitialise&&ao){clearInterval(ao)}}A.trigger("jsp-initialised",[az||at])}function aA(){if(at){ah.append(b('<div class="jspVerticalBar" />').append(b('<div class="jspCap jspCapTop" />'),b('<div class="jspTrack" />').append(b('<div class="jspDrag" />').append(b('<div class="jspDragTop" />'),b('<div class="jspDragBottom" />'))),b('<div class="jspCap jspCapBottom" />')));P=ah.find(">.jspVerticalBar");aj=P.find(">.jspTrack");an=aj.find(">.jspDrag");if(ar.showArrows){ak=b('<a class="jspArrow jspArrowUp" />').bind("mousedown.jsp",ax(0,-1)).bind("click.jsp",av);aa=b('<a class="jspArrow jspArrowDown" />').bind("mousedown.jsp",ax(0,1)).bind("click.jsp",av);if(ar.arrowScrollOnHover){ak.bind("mouseover.jsp",ax(0,-1,ak));aa.bind("mouseover.jsp",ax(0,1,aa))}ag(aj,ar.verticalArrowPositions,ak,aa)}r=u;ah.find(">.jspVerticalBar>.jspCap:visible,>.jspVerticalBar>.jspArrow").each(function(){r-=b(this).outerHeight()});an.hover(function(){an.addClass("jspHover")},function(){an.removeClass("jspHover")}).bind("mousedown.jsp",function(aD){b("html").bind("dragstart.jsp selectstart.jsp",function(){return false});an.addClass("jspActive");var s=aD.pageY-an.position().top;b("html").bind("mousemove.jsp",function(aE){Q(aE.pageY-s,false)}).bind("mouseup.jsp mouseleave.jsp",ap);return false});o();t()}}function o(){aj.height(r+"px");F=0;S=ar.verticalGutter+aj.outerWidth();T.width(af-S-g);if(P.position().left==0){T.css("margin-left",S+"px")}}function x(){if(az){ah.append(b('<div class="jspHorizontalBar" />').append(b('<div class="jspCap jspCapLeft" />'),b('<div class="jspTrack" />').append(b('<div class="jspDrag" />').append(b('<div class="jspDragLeft" />'),b('<div class="jspDragRight" />'))),b('<div class="jspCap jspCapRight" />')));ai=ah.find(">.jspHorizontalBar");D=ai.find(">.jspTrack");h=D.find(">.jspDrag");if(ar.showArrows){aq=b('<a class="jspArrow jspArrowLeft" />').bind("mousedown.jsp",ax(-1,0)).bind("click.jsp",av);v=b('<a class="jspArrow jspArrowRight" />').bind("mousedown.jsp",ax(1,0)).bind("click.jsp",av);if(ar.arrowScrollOnHover){aq.bind("mouseover.jsp",ax(-1,0,aq));
v.bind("mouseover.jsp",ax(1,0,v))}ag(D,ar.horizontalArrowPositions,aq,v)}h.hover(function(){h.addClass("jspHover")},function(){h.removeClass("jspHover")}).bind("mousedown.jsp",function(aD){b("html").bind("dragstart.jsp selectstart.jsp",function(){return false});h.addClass("jspActive");var s=aD.pageX-h.position().left;b("html").bind("mousemove.jsp",function(aE){R(aE.pageX-s,false)}).bind("mouseup.jsp mouseleave.jsp",ap);return false});l=ah.innerWidth();ac();q()}else{}}function ac(){ah.find(">.jspHorizontalBar>.jspCap:visible,>.jspHorizontalBar>.jspArrow").each(function(){l-=b(this).outerWidth()});D.width(l+"px");V=0}function C(){if(az&&at){var aD=D.outerHeight(),s=aj.outerWidth();r-=aD;b(ai).find(">.jspCap:visible,>.jspArrow").each(function(){l+=b(this).outerWidth()});l-=s;u-=s;af-=aD;D.parent().append(b('<div class="jspCorner" />').css("width",aD+"px"));o();ac()}if(az){T.width((ah.outerWidth()-g)+"px")}U=T.outerHeight();p=U/u;if(az){am=1/w*l;if(am>ar.horizontalDragMaxWidth){am=ar.horizontalDragMaxWidth}else{if(am<ar.horizontalDragMinWidth){am=ar.horizontalDragMinWidth}}h.width(am+"px");j=l-am}if(at){y=1/p*r;if(y>ar.verticalDragMaxHeight){y=ar.verticalDragMaxHeight}else{if(y<ar.verticalDragMinHeight){y=ar.verticalDragMinHeight}}an.height(y+"px");i=r-y}}function ag(aE,aG,aD,s){var aI="before",aF="after",aH;if(aG=="os"){aG=/Mac/.test(navigator.platform)?"after":"split"}if(aG==aI){aF=aG}else{if(aG==aF){aI=aG;aH=aD;aD=s;s=aH}}aE[aI](aD)[aF](s)}function ax(aD,s,aE){return function(){E(aD,s,this,aE);this.blur();return false}}function E(aF,aD,aI,aH){aI=b(aI).addClass("jspActive");var aG,s=function(){if(aF!=0){R(V+aF*ar.arrowButtonSpeed,false)}if(aD!=0){Q(F+aD*ar.arrowButtonSpeed,false)}},aE=setInterval(s,ar.arrowRepeatFreq);s();aG=aH==c?"mouseup.jsp":"mouseout.jsp";aH=aH||b("html");aH.bind(aG,function(){aI.removeClass("jspActive");clearInterval(aE);aH.unbind(aG)})}function ap(){b("html").unbind("dragstart.jsp selectstart.jsp mousemove.jsp mouseup.jsp mouseleave.jsp");an&&an.removeClass("jspActive");h&&h.removeClass("jspActive")}function Q(s,aD){if(!at){return}if(s<0){s=0}else{if(s>i){s=i}}if(aD==c){aD=ar.animateScroll}if(aD){L.animate(an,"top",s,Y)}else{an.css("top",s);Y(s)}}function Y(aD){if(aD==c){aD=an.position().top}ah.scrollTop(0);F=aD;var aG=F==0,aE=F==i,aF=aD/i,s=-aF*(U-u);if(ae!=aG||aB!=aE){ae=aG;aB=aE;A.trigger("jsp-arrow-change",[ae,aB,K,k])}t(aG,aE);T.css("top",s);A.trigger("jsp-scroll-y",[-s,aG,aE])}function R(aD,s){if(!az){return}if(aD<0){aD=0}else{if(aD>j){aD=j}}if(s==c){s=ar.animateScroll}if(s){L.animate(h,"left",aD,Z)}else{h.css("left",aD);Z(aD)}}function Z(aD){if(aD==c){aD=h.position().left}ah.scrollTop(0);V=aD;var aG=V==0,aF=V==i,aE=aD/j,s=-aE*(O-af);if(K!=aG||k!=aF){K=aG;k=aF;A.trigger("jsp-arrow-change",[ae,aB,K,k])}q(aG,aF);T.css("left",s);A.trigger("jsp-scroll-x",[-s,aG,aF])}function t(aD,s){if(ar.showArrows){ak[aD?"addClass":"removeClass"]("jspDisabled");aa[s?"addClass":"removeClass"]("jspDisabled")}}function q(aD,s){if(ar.showArrows){aq[aD?"addClass":"removeClass"]("jspDisabled");v[s?"addClass":"removeClass"]("jspDisabled")}}function H(s,aD){var aE=s/(U-u);Q(aE*i,aD)}function I(aD,s){var aE=aD/(O-af);R(aE*j,s)}function W(aL,aJ,aD){var aH,aF,s=0,aE,aI,aK;try{aH=b(aL)}catch(aG){return}aF=aH.outerHeight();ah.scrollTop(0);while(!aH.is(".jspPane")){s+=aH.position().top;aH=aH.offsetParent();if(/^body|html$/i.test(aH[0].nodeName)){return}}aE=au();aI=aE+u;if(s<aE||aJ){aK=s-ar.verticalGutter}else{if(s+aF>aI){aK=s-u+aF+ar.verticalGutter}}if(aK){H(aK,aD)}}function aw(){return -T.position().left}function au(){return -T.position().top}function ab(){ah.unbind(X).bind(X,function(aG,aH,aF,aD){var aE=V,s=F;R(V+aF*ar.mouseWheelSpeed,false);Q(F-aD*ar.mouseWheelSpeed,false);return aE==V&&s==F})}function n(){ah.unbind(X)}function av(){return false}function G(){T.unbind("focusin.jsp").bind("focusin.jsp",function(s){if(s.target===T[0]){return}W(s.target,false)})}function B(){T.unbind("focusin.jsp")}function N(){var aD,s;A.attr("tabindex",0).unbind("keydown.jsp").bind("keydown.jsp",function(aH){if(aH.target!==A[0]){return
}var aF=V,aE=F,aG=aD?2:16;switch(aH.keyCode){case 40:Q(F+aG,false);break;case 38:Q(F-aG,false);break;case 34:case 32:H(au()+Math.max(32,u)-16);break;case 33:H(au()-u+16);break;case 35:H(U-u);break;case 36:H(0);break;case 39:R(V+aG,false);break;case 37:R(V-aG,false);break}if(!(aF==V&&aE==F)){aD=true;clearTimeout(s);s=setTimeout(function(){aD=false},260);return false}});if(ar.hideFocus){A.css("outline","none");if("hideFocus" in ah[0]){A.attr("hideFocus",true)}}else{A.css("outline","");if("hideFocus" in ah[0]){A.attr("hideFocus",false)}}}function M(){A.attr("tabindex","-1").removeAttr("tabindex").unbind("keydown.jsp")}function z(){if(location.hash&&location.hash.length>1){var aE,aD;try{aE=b(location.hash)}catch(s){return}if(aE.length&&T.find(aE)){if(ah.scrollTop()==0){aD=setInterval(function(){if(ah.scrollTop()>0){W(location.hash,true);b(document).scrollTop(ah.position().top);clearInterval(aD)}},50)}else{W(location.hash,true);b(document).scrollTop(ah.position().top)}}}}function ad(){b("a.jspHijack").unbind("click.jsp-hijack").removeClass("jspHijack")}function m(){ad();b("a[href^=#]").addClass("jspHijack").bind("click.jsp-hijack",function(){var s=this.href.split("#"),aD;if(s.length>1){aD=s[1];if(aD.length>0&&T.find("#"+aD).length>0){W("#"+aD,true);return false}}})}b.extend(L,{reinitialise:function(aD){aD=b.extend({},aD,ar);al(aD)},scrollToElement:function(aE,aD,s){W(aE,aD,s)},scrollTo:function(aE,s,aD){I(aE,aD);H(s,aD)},scrollToX:function(aD,s){I(aD,s)},scrollToY:function(s,aD){H(s,aD)},scrollBy:function(aD,s,aE){L.scrollByX(aD,aE);L.scrollByY(s,aE)},scrollByX:function(s,aE){var aD=aw()+s,aF=aD/(O-af);R(aF*j,aE)},scrollByY:function(s,aE){var aD=au()+s,aF=aD/(U-u);Q(aF*i,aE)},animate:function(aD,aG,s,aF){var aE={};aE[aG]=s;aD.animate(aE,{duration:ar.animateDuration,ease:ar.animateEase,queue:false,step:aF})},getContentPositionX:function(){return aw()},getContentPositionY:function(){return au()},getIsScrollableH:function(){return az},getIsScrollableV:function(){return at},getContentPane:function(){return T},scrollToBottom:function(s){Q(i,s)},hijackInternalLinks:function(){m()}})}f=b.extend({},b.fn.jScrollPane.defaults,f);var e;this.each(function(){var g=b(this),h=g.data("jsp");if(h){h.reinitialise(f)}else{h=new d(g,f);g.data("jsp",h)}e=e?e.add(g):g});return e};b.fn.jScrollPane.defaults={showArrows:false,maintainPosition:true,autoReinitialise:false,autoReinitialiseDelay:500,verticalDragMinHeight:0,verticalDragMaxHeight:99999,horizontalDragMinWidth:0,horizontalDragMaxWidth:99999,animateScroll:false,animateDuration:300,animateEase:"linear",hijackInternalLinks:false,verticalGutter:4,horizontalGutter:4,mouseWheelSpeed:10,arrowButtonSpeed:10,arrowRepeatFreq:100,arrowScrollOnHover:false,verticalArrowPositions:"split",horizontalArrowPositions:"split",enableKeyboardNavigation:true,hideFocus:false}})(jQuery,this);
