(function(m){function t(b,a,c){var d=0==c,e=b.$n(a);b=b.$n(a+"-embed");e&&(e=e.style,e.display=d?"none":"block",e.opacity=c);b&&(e=b.style,e.display=d?"block":"none")}function w(b,a,c){if(a>c)return b;b<a&&(b=a);b>c&&(b=c);return b}zul.Scrollbar=m.$extends(m.Object,{_pos:null,_barPos:null,_pressTimer:null,_frozenScroll:0,_frozenLimit:!1,$n:function(b){return jq(this.uid+"-"+b,m)[0]},$init:function(b,a,c){if(!b||!a)throw"Both wrapper and scroller dom element are required to generate scrollbar";this.cave=
b;var d=b.style;d.position="relative";d.overflow="hidden";this.scroller=a;this.opts=m.$default(c,{embed:!1,step:20,wheelAmountStep:3,startPositionX:0,startPositionY:0});this.widget=m.Widget.$(b);this.uid=this.widget.uuid;this._pos=[0,0];this._barPos=[0,0];this.currentPos={x:this._pos[0],y:this._pos[1]};jq(b).bind("scroll",this.proxy(this._fixScroll)).bind("mouseenter",this.proxy(this._mouseEnter)).bind("mouseleave",this.proxy(this._mouseLeave))},destroy:function(){jq(this.cave).unbind("scroll",this.proxy(this._fixScroll)).unbind("mouseenter",
this.proxy(this._mouseEnter)).unbind("mouseleave",this.proxy(this._mouseLeave));this._unbindMouseEvent("hor");this._unbindMouseEvent("ver");var b=this.$n("hor"),a=this.$n("ver");b&&jq(b).remove();a&&jq(a).remove();this.opts.embed&&(b=this.$n("hor-embed"),a=this.$n("ver-embed"),b&&jq(b).remove(),a&&jq(a).remove());this._pos=this._barPos=this.currentPos=null},hasVScroll:function(){return this.needV},hasHScroll:function(){return this.needH},syncSize:function(b){this._checkBarRequired();var a=this.widget;
var c=a.frozen;var d=0;var e=a.$n("tpad");var f=a.$n("bpad");var g=this.cave,h=this.scroller,q=this.$n("hor");a=this.$n("ver");var y=this.needH,n=this.needV,u=this.opts,v=h.scrollHeight;e&&f&&(v+=e.offsetHeight+f.offsetHeight);c&&(d=50*(c._scrollScale||0));if(y){c=q.style.display;q.style.display="block";e=this.$n("hor-embed");var m=this.$n("hor-left"),w=this.$n("hor-right");f=this.$n("hor-wrapper");var r=f.style;var k=u.startPositionX,p=g.offsetWidth-k;h=h.scrollWidth-k+d;var x=m.offsetWidth,z=w.offsetWidth,
l=p-x-z;h<p&&(h=p+d);k&&(m.style.left=jq.px(k),r.left=jq.px(k+x),e&&(e.style.left=jq.px(k)));n?(r.width=jq.px(l-z),w.style.right=jq.px(z),r=a.style.display,a.style.display="block",h+=jq(a).outerWidth(!0),a.style.display=r):r.width=jq.px(l);r=this.$n("hor-indicator");f=f.offsetWidth;d=Math.round(f*p/h);d=15<d?d:15;r.style.width=d+"px";e&&(e.style.width=d+x+z+"px");this.hLimit=h-p;e=f-d;0>=e?(this.hBarLimit=0,r.style.display="none"):this.hBarLimit=e;this.hRatio=Math.abs(this.hLimit/this.hBarLimit);
q.style.display=c}n&&(c=a.style.display,a.style.display="block",e=this.$n("ver-embed"),n=this.$n("ver-up"),p=this.$n("ver-down"),f=this.$n("ver-wrapper"),r=f.style,h=u.startPositionY,u=g.offsetHeight-h,v-=h,n=n.offsetHeight,d=p.offsetHeight,x=u-n-d,h&&(a.style.top=jq.px(g.offsetTop+h),p.style.bottom=jq.px(h),e&&(e.style.top=a.style.top)),y?(r.height=jq.px0(x-d),p.style.bottom=jq.px(h+d),g=q.style.display,q.style.display="block",v+=jq(q).outerHeight(!0),q.style.display=g):r.height=jq.px(x),r=this.$n("ver-indicator"),
q=f.offsetHeight,g=Math.round(q*u/v),g=15<g?g:15,r.style.height=g+"px",e&&(e.style.height=g+n+d+"px"),this.vLimit=v-u,e=q-g,0>=e?(this.vBarLimit=0,r.style.display="none"):this.vBarLimit=e,this.vRatio=Math.abs(this.vLimit/this.vBarLimit),a.style.display=c);this.scrollTo(this._pos[0],this._pos[1]);b&&(t(this,"hor",1),t(this,"ver",1))},scrollTo:function(b,a){if(this.needH){b=w(b,0,this.hLimit);var c=b/this.hRatio;this._syncPosition("hor",b);this._syncBarPosition("hor",c);this._syncEmbedBarPosition("hor",
b+c)}this.needV&&(a=w(a,0,this.vLimit),c=a/this.vRatio,this._syncPosition("ver",a),this._syncBarPosition("ver",c),this._syncEmbedBarPosition("ver",a+c));this._onScrollEnd()},scrollToElement:function(b){var a=this.cave,c=jq(b).offset().top,d=c+b.offsetHeight,e=jq(b).offset().left,f=e+b.offsetWidth,g=jq(a).offset().top,h=g+a.offsetHeight,q=jq(a).offset().left,y=q+a.offsetWidth,n=!0;a=b.offsetWidth==a.offsetWidth;(f>=q&&f<=y||e>=q&&e<=y||e<=q&&f>=y)&&(c>=g&&c<=h||d>=g&&d<=h||c<=g&&d>=h)||(c<g&&(n=!1),
e<q&&(a=!1),c=n?d-h:g-c,n=this._pos[1]+(n?c:-c),e=a?f-y:q-e,e=this._pos[0]+(a?e:-e),this.needV&&(n=w(n,0,this.vLimit),f=n/this.vRatio,this._syncPosition("ver",n),this._syncBarPosition("ver",f),this._syncEmbedBarPosition("ver",n+f)),this.needH&&(e=w(e,0,this.hLimit),f=e/this.hRatio,this._syncPosition("hor",e),this._syncBarPosition("hor",f),this._syncEmbedBarPosition("hor",e+f)),this._onScrollEnd(),m(b).scrollIntoView())},isScrollIntoView:function(b){var a=this.cave,c=jq(b).offset().top,d=c+b.offsetHeight,
e=jq(b).offset().left;b=e+b.offsetWidth;var f=jq(a).offset().top,g=f+a.offsetHeight,h=jq(a).offset().left;a=h+a.offsetWidth;return this.needV&&d<=g&&c>=f||this.needH&&b<=a&&e>=h?!0:!1},getCurrentPosition:function(){return this.currentPos},setBarPosition:function(b){var a=this.widget.frozen;a&&this.needH&&(b*=this.hBarLimit/a._scrollScale,this._syncBarPosition("hor",b),this._syncEmbedBarPosition("hor",b))},_checkBarRequired:function(){var b=this.cave,a=this.scroller;this.needH=this.widget.frozen?!0:
b.offsetWidth<a.scrollWidth;var c=this.$n("hor");this.needH?this.$n("hor")||(this.redraw(b,"hor"),this._bindMouseEvent("hor")):c&&(this._unbindMouseEvent("hor"),this._syncPosition("hor",0),jq(c).remove(),this.opts.embed&&(c=this.$n("hor-embed"))&&jq(c).remove());this.needV=b.offsetHeight<a.offsetHeight;a=this.$n("ver");this.needV?a||(this.redraw(b,"ver"),this._bindMouseEvent("ver")):a&&(this._unbindMouseEvent("ver"),this._syncPosition("ver",0),jq(a).remove(),this.opts.embed&&(b=this.$n("ver-embed"))&&
jq(b).remove())},_bindMouseEvent:function(b){var a=this.cave,c="hor"==b,d=this.$n(b),e=this.$n(b+"-indicator"),f=this.$n(b+"-rail"),g=this.$n(b+(c?"-left":"-up"));b=this.$n(b+(c?"-right":"-down"));!c||11>m.ie&&m.opera?jq(a).mousewheel(this.proxy(this._mousewheelY)):jq(a).mousewheel(this.proxy(this._mousewheelX));jq(d).click(m.$void);jq(e).bind("mousedown",this.proxy(this._dragStart));jq(f).bind("mousedown",this.proxy(this._mouseDown)).bind("mouseup",this.proxy(this._mouseUp));jq(g).bind("mousedown",
this.proxy(this._mouseDown)).bind("mouseup",this.proxy(this._mouseUp));jq(b).bind("mousedown",this.proxy(this._mouseDown)).bind("mouseup",this.proxy(this._mouseUp))},_unbindMouseEvent:function(b){var a=this.cave,c="hor"==b,d=this.$n(b),e=this.$n(b+"-indicator"),f=this.$n(b+"-rail"),g=this.$n(b+(c?"-left":"-up"));b=this.$n(b+(c?"-right":"-down"));!c||11>m.ie&&m.opera?jq(a).unmousewheel(this.proxy(this._mousewheelY)):jq(a).unmousewheel(this.proxy(this._mousewheelX));jq(d).unbind("click",m.$void);jq(e).unbind("mousedown",
this.proxy(this._dragStart));jq(f).unbind("mousedown",this.proxy(this._mouseDown)).unbind("mouseup",this.proxy(this._mouseUp));jq(g).unbind("mousedown",this.proxy(this._mouseDown)).unbind("mouseup",this.proxy(this._mouseUp));jq(b).unbind("mousedown",this.proxy(this._mouseDown)).unbind("mouseup",this.proxy(this._mouseUp))},_fixScroll:function(b){b=this.cave;this.dragging||this.scrollTo(b.scrollLeft,b.scrollTop)},_mouseEnter:function(b){t(this,"hor",1);t(this,"ver",1)},_mouseLeave:function(b){this.dragging||
(t(this,"hor",0),t(this,"ver",0))},_dragStart:function(b){this._pressTimer&&(clearInterval(this._pressTimer),this._pressTimer=null);b.preventDefault();var a=this.$n("hor-indicator")==b.currentTarget?"hor":"ver",c="hor"==a;b={orient:a,point:c?b.pageX:b.pageY,pos:c?this._barPos[0]:this._barPos[1]};jq(document).bind("mousemove",b,this.proxy(this._dragMove)).bind("mouseup",this.proxy(this._dragEnd))},_dragEnd:function(b){var a=b.pageX;b=b.pageY;var c=this.cave,d=jq(c).offset().left,e=jq(c).offset().top;
jq(document).unbind("mousemove",this.proxy(this._dragMove)).unbind("mouseup",this.proxy(this._dragEnd));this.dragging=!1;if(a<d||a>d+c.offsetWidth||b<e||b>e+c.offsetHeight)t(this,"hor",0),t(this,"ver",0)},_dragMove:function(b){var a=b.data;var c=a.orient,d=a.point;a=a.pos;var e="hor"==c,f=(e?b.pageX:b.pageY)-d;b=e?this.hBarLimit:this.vBarLimit;d=e?this.hRatio:this.vRatio;var g=this.widget.frozen;this.dragging=!0;f=w(a+f,0,b);this._syncBarPosition(c,f);g&&e?(g._doScroll(f/(b/g._scrollScale)),this._syncEmbedBarPosition("hor",
f)):(a=f*d,this._syncPosition(c,a),this._syncEmbedBarPosition(c,a+f),this._onScrollEnd())},_mousewheelX:function(b,a,c,d){a=this.opts;a=a.step*a.wheelAmountStep;d=this._pos[0];c&&(b.stop(),d=w(d+(0<c?-a:a)+a,0,this.hLimit),b=this.widget.frozen,c=d/this.hRatio,b?(a=this.hBarLimit/b._scrollScale,c/=a,b._doScroll(c),this._syncEmbedBarPosition("hor",c)):(this._syncPosition("hor",d),this._syncEmbedBarPosition("hor",d+c)),this._syncBarPosition("hor",c),this._onScrollEnd())},_mousewheelY:function(b,a,c,
d){a=this.opts;c=a.step*a.wheelAmountStep;a=this._pos[1];d&&(d=0<d,d&&0==a||!d&&a==this.vLimit||(b.stop(),a=w(a+(d?-c:c),0,this.vLimit),b=a/this.vRatio,this._syncPosition("ver",a),this._syncBarPosition("ver",b),this._syncEmbedBarPosition("ver",a+b),this._onScrollEnd()))},_mouseUp:function(b){clearInterval(this._pressTimer);this._pressTimer=null},_mouseDown:function(b){var a;this._pressTimer&&(clearInterval(this._pressTimer),this._pressTimer=null);var c=b.currentTarget,d=this.$n("hor-rail"),e=this.$n("ver-rail"),
f=this.$n("ver-up"),g=this.$n("ver-down"),h=this.$n("hor-left"),q=this.$n("hor-right"),m=this.widget.frozen,n=this.hBarLimit,u=m?n/m._scrollScale:this.opts.step;if(c==d||c==e){var v=(a=c==d)?"hor":"ver";d=jq(c).offset();d=a?d.left:d.top;e=this.$n(v+"-indicator");var t=(a?b.pageX:b.pageY)-d-(a?e.offsetWidth:e.offsetHeight)/2;var A=a?this.hLimit:this.vLimit;var r=a?this.hRatio:this.vRatio;var k=a?this._pos[0]:this._pos[1];var p=a?this._barPos[0]:this._barPos[1];var x=t*r,z=10*u;var l=this;m?(t=w(t,
0,n),l._syncBarPosition("hor",t),l._syncEmbedBarPosition("hor",t),m._doScroll(t/u)):l._pressTimer=setInterval(function(){var a=t>p;var b=a?k:x;b=0>b?0:b;var c=a?x:k;c=c>A?A:c;k+=a?z:-z;k=w(k,b,c);a=k/r;if(k==x||k<=b||k>=c)clearInterval(l._pressTimer),l._pressTimer=null;l._syncPosition(v,k);l._syncBarPosition(v,a);l._syncEmbedBarPosition(v,k+a);l._onScrollEnd()},50)}if(c==h||c==q||c==f||c==g)l=this,v=(a=c==h||c==q)?"hor":"ver",A=a?l.hLimit:l.vLimit,r=a?l.hRatio:l.vRatio,k=a?l._pos[0]:l._pos[1],p=l._barPos[0],
l._pressTimer=setInterval(function(){m&&a?(p+=c==h?-u:u,p=w(p,0,n),l._syncBarPosition(v,p),l._syncEmbedBarPosition(v,p),m._doScroll(p/u)):(k=a?k+(c==h?-u:u):k+(c==f?-u:u),k=w(k,0,A),p=k/r,l._syncPosition(v,k),l._syncBarPosition(v,p),l._syncEmbedBarPosition(v,k+p),l._onScrollEnd())},50)},_syncPosition:function(b,a){if(this._pos){var c="hor"==b,d=this.cave;b=this.$n(b);var e=this.opts.embed;b&&(a=Math.round(a),this._pos[c?0:1]=a,b.style[c?"left":"top"]=a+"px",c&&this.needV&&(this.$n("ver").style.right=
-a+"px",e&&(this.$n("ver-embed").style.right=-a+"px")),!c&&this.needH&&(this.$n("hor").style.bottom=-a+"px",e&&(this.$n("hor-embed").style.bottom=-a+"px")),d[c?"scrollLeft":"scrollTop"]=a,a=this.opts.onSyncPosition)&&(this.currentPos={x:this._pos[0],y:this._pos[1]},a.call(this))}},_syncBarPosition:function(b,a){var c="hor"==b;b=this.$n(b+"-indicator");this._barPos[c?0:1]=a;b.style[c?"left":"top"]=a+"px"},_syncEmbedBarPosition:function(b,a){if(this.opts.embed){var c="hor"==b;b=this.$n(b+"-embed");
var d=this.opts;b.style[c?"left":"top"]=a+(c?d.startPositionX:d.startPositionY)+"px"}},_onScrollEnd:function(){var b=this.opts.onScrollEnd;b&&(this.currentPos={x:this._pos[0],y:this._pos[1]},b.call(this))},redraw:function(b,a){var c="hor"==a;a=this.uid+"-"+a;var d=c?"horizontal":"vertical",e=c?"left":"up";c=c?"right":"down";var f="";this.opts.embed&&(f+='<div id="'+a+'-embed" class="z-scrollbar-'+d+'-embed"></div>');f+='<div id="'+a+'" class="z-scrollbar z-scrollbar-'+d+'"><div id="'+a+"-"+e+'" class="z-scrollbar-'+
e+'"><i class="z-icon-caret-'+e+'"></i></div><div id="'+a+'-wrapper" class="z-scrollbar-wrapper"><div id="'+a+'-indicator" class="z-scrollbar-indicator"><i class="z-scrollbar-icon z-icon-reorder"></i></div><div id="'+a+'-rail" class="z-scrollbar-rail"></div></div><div id="'+a+"-"+c+'" class="z-scrollbar-'+c+'"><i class="z-icon-caret-'+c+'"></i></div></div>';jq(b).append(f)}})})(zk);
