/* 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 33 2008-12-10 22:55:28Z 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)
 *								reinitialiseOnImageLoad - Whether the jScrollPane should automatically re-initialise itself when any contained images are loaded
 * @return jQuery
 * @cat Plugins/jScrollPane
 * @author Kelvin Luck (kelvin AT kelvinluck DOT com || http://www.kelvinluck.com)
 */
(function($){$.jScrollPane={active:[]};$.fn.jScrollPane=function(bj){bj=$.extend({},$.fn.jScrollPane.defaults,bj);var ba=function(){return false};return this.each(function(){var j=$(this);j.css('overflow','hidden');var k=this;if($(this).parent().is('.jScrollPaneContainer')){var l=bj.maintainPosition?j.position().top:0;var m=$(this).parent();var o=m.innerWidth();var q=m.outerHeight();var r=q;$('>.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown',m).remove();j.css({'top':0})}else{var l=0;this.originalPadding=j.css('paddingTop')+' '+j.css('paddingRight')+' '+j.css('paddingBottom')+' '+j.css('paddingLeft');this.originalSidePaddingTotal=(parseInt(j.css('paddingLeft'))||0)+(parseInt(j.css('paddingRight'))||0);var o=j.innerWidth();var q=j.innerHeight();var r=q;j.wrap($('<div></div>').attr({'className':'jScrollPaneContainer'}).css({'height':q+'px','width':o+'px'}));$(document).bind('emchange',function(e,a,b){j.jScrollPane(bj)})}if(bj.reinitialiseOnImageLoad){var s=$.data(k,'jScrollPaneImagesToLoad')||$('img',j);var t=[];if(s.length){s.each(function(i,b){$(this).bind('load',function(){if($.inArray(i,t)==-1){t.push(b);s=$.grep(s,function(n,i){return n!=b});$.data(k,'jScrollPaneImagesToLoad',s);bj.reinitialiseOnImageLoad=false;j.jScrollPane(bj)}}).each(function(i,a){if(this.complete||this.complete===undefined){this.src=this.src}})})}}var p=this.originalSidePaddingTotal;var u={'height':'auto','width':o-bj.scrollbarWidth-bj.scrollbarMargin-p+'px'};if(bj.scrollbarOnLeft){u.paddingLeft=bj.scrollbarMargin+bj.scrollbarWidth+'px'}else{u.paddingRight=bj.scrollbarMargin+'px'};j.css(u);var v=j.outerHeight();var w=q/v;if(w<.99){var x=j.parent();x.append($('<div></div>').attr({'className':'jScrollPaneTrack'}).css({'width':bj.scrollbarWidth+'px'}).append($('<div></div>').attr({'className':'jScrollPaneDrag'}).css({'width':bj.scrollbarWidth+'px'}).append($('<div></div>').attr({'className':'jScrollPaneDragTop'}).css({'width':bj.scrollbarWidth+'px'}),$('<div></div>').attr({'className':'jScrollPaneDragBottom'}).css({'width':bj.scrollbarWidth+'px'}))));var y=$('>.jScrollPaneTrack',x);var z=$('>.jScrollPaneTrack .jScrollPaneDrag',x);if(bj.showArrows){var A;var B;var C;var D;var E=function(){if(D>4||D%4==0){V(O+B*N)}D++};var F=function(a){$('html').unbind('mouseup',F);A.removeClass('jScrollActiveArrowButton');clearInterval(C)};var G=function(){$('html').bind('mouseup',F);A.addClass('jScrollActiveArrowButton');D=0;E();C=setInterval(E,100)};x.append($('<a></a>').attr({'href':'javascript:;','className':'jScrollArrowUp'}).css({'width':bj.scrollbarWidth+'px'}).html('Scroll up').bind('mousedown',function(){A=$(this);B=-1;G();preparaClickFotoramas(GalAudio,'scroll-up',GalAudio.titular,"audios");this.blur();return false}).bind('click',ba),$('<a></a>').attr({'href':'javascript:;','className':'jScrollArrowDown'}).css({'width':bj.scrollbarWidth+'px'}).html('Scroll down').bind('mousedown',function(){A=$(this);B=1;G();preparaClickFotoramas(GalAudio,'scroll-down',GalAudio.titular,"audios");this.blur();return false}).bind('click',ba));var H=$('>.jScrollArrowUp',x);var I=$('>.jScrollArrowDown',x);if(bj.arrowSize){r=q-bj.arrowSize-bj.arrowSize;y.css({'height':r+'px',top:bj.arrowSize+'px'})}else{var J=H.height();bj.arrowSize=J;r=q-J-I.height();y.css({'height':r+'px',top:J+'px'})}}var K=$(this).css({'position':'absolute','overflow':'visible'});var L;var M;var N;var O=0;var P=w*q/2;var Q=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 R=function(){return false};var S=function(){bi();L=z.offset(false);L.top-=O;M=r-z[0].offsetHeight;N=2*bj.wheelSpeed*M/v};var T=function(a){preparaClickFotoramas(GalAudio,'scroll',GalAudio.titular,"audios");S();P=Q(a,'Y')-O-L.top;$('html').bind('mouseup',U).bind('mousemove',W);if($.browser.msie){$('html').bind('dragstart',R).bind('selectstart',R)}return false};var U=function(){$('html').unbind('mouseup',U).unbind('mousemove',W);P=w*q/2;if($.browser.msie){$('html').unbind('dragstart',R).unbind('selectstart',R)}};var V=function(a){a=a<0?0:(a>M?M:a);O=a;z.css({'top':a+'px'});var p=a/M;K.css({'top':((q-v)*p)+'px'});j.trigger('scroll');if(bj.showArrows){H[a==0?'addClass':'removeClass']('disabled');I[a==M?'addClass':'removeClass']('disabled')}};var W=function(e){V(Q(e,'Y')-L.top-P)};var X=Math.max(Math.min(w*(q-bj.arrowSize*2),bj.dragMaxHeight),bj.dragMinHeight);z.css({'height':X+'px'}).bind('mousedown',T);var Y;var Z;var bb;var bc=function(){if(Z>8||Z%4==0){V((O-((O-bb)/2)))}Z++};var bd=function(){clearInterval(Y);$('html').unbind('mouseup',bd).unbind('mousemove',be)};var be=function(a){bb=Q(a,'Y')-L.top-P};var bf=function(a){preparaClickFotoramas(GalAudio,'scroll',GalAudio.titular,"audios");S();be(a);Z=0;$('html').bind('mouseup',bd).bind('mousemove',be);Y=setInterval(bc,100);bc()};y.bind('mousedown',bf);x.bind('mousewheel',function(a,b){S();bi();var d=O;V(O-b*N);var c=d!=O;return!c});var bg;var bh;function animateToPosition(){var a=(bg-O)/bj.animateStep;if(a>1||a<-1){V(O+a)}else{V(bg);bi()}}var bi=function(){if(bh){clearInterval(bh);delete bg}};var bk=function(a,b){if(typeof a=="string"){$e=$(a,j);if(!$e.length)return;a=$e.offset().top-j.offset().top}x.scrollTop(0);bi();var c=-a/(q-v)*M;if(b||!bj.animateTo){V(c)}else{bg=c;bh=setInterval(animateToPosition,bj.animateInterval)}};j[0].scrollTo=bk;j[0].scrollBy=function(a){var b=-parseInt(K.css('top'))||0;bk(b+a)};S();bk(-l,true);$('*',this).bind('focus',function(a){var b=$(this);var c=0;while(b[0]!=j[0]){c+=b.position().top;b=b.offsetParent()}var d=-parseInt(K.css('top'))||0;var e=d+q;var f=c>d&&c<e;if(!f){var g=c-bj.scrollbarMargin;if(c>d){g+=$(this).height()+15+bj.scrollbarMargin-q}bk(g)}});if(location.hash){bk(location.hash)};$(document).bind('click',function(e){$target=$(e.target);if($target.is('a')){var h=$target.attr('href');if(h.substr(0,1)=='#'){bk(h)}}});$.jScrollPane.active.push(j[0])}else{j.css({'height':q+'px','width':o-this.originalSidePaddingTotal+'px','padding':this.originalPadding});j.parent().unbind('mousewheel')}})};$.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};$(window).bind('unload',function(){var a=$.jScrollPane.active;for(var i=0;i<a.length;i++){a[i].scrollTo=a[i].scrollBy=null}})})(jQuery);


