/*jslint eqeqeq: true, regexp: true */
/*global document, window, setInterval, clearInterval, handler, jQuery */

/*
 * Scrollbar - a jQuery plugin for custom scrollbars
 *	
 * https://github.com/thomd/jquery-scroll/tree/
 * @author     Thomas Duerr, me@thomd.net
 * @date       03.2010
 * @requires   jquery v1.4.2 
 *
 *
 * Usage:
 *
 *    Append scrollbar to an arbitrary container with overflowed content:
 *
 *         $('selector').scrollbar();
 *
 *
 *    Append scrollbar without arrows on top/bottom:
 *
 *         $('selector').scrollbar({
 *            arrows: false
 *         });
 *
 *
 *
 * A vertical scrollbar is based on the following box model:
 *
 *    +----------------------------------|
 *    |            <----------------------------- content container
 *    |  +-----------------+  +------+   |
 *    |  |                 |  |   <-------------- handle arrow up
 *    |  |                 |  |      |   |
 *    |  |                 |  +------+   |
 *    |  |                 |  | +--+ |   |
 *    |  |                 |  | |  | |   |
 *    |  |                 |  | | <-------------- handle
 *    |  |                 |  | |  | |   |
 *    |  |                 |  | |  | |   |
 *    |  |                 |  | |  | |   |
 *    |  |                 |  | +--+ |   |
 *    |  |                 |  |      |   |
 *    |  |                 |  |   <-------------- handle container
 *    |  |                 |  |      |   |
 *    |  |         <----------------------------- pane
 *    |  |                 |  |      |   |
 *    |  |                 |  |      |   |
 *    |  |                 |  +------+   |
 *    |  |                 |  |      |   |
 *    |  |                 |  |   <-------------- handle arrow down
 *    |  +-----------------+  +------+   |
 *    |                                  |
 *    +----------------------------------|
 *
 *
 */
(function(a,b){a.fn.scrollbar=function(b){var c=a.extend({},a.fn.scrollbar.defaults,b);return this.each(function(){var b=a(this),d={arrows:c.arrows};if(c.containerHeight!="auto"){b.height(c.containerHeight)}d.containerHeight=b.height();d.contentHeight=a.fn.scrollbar.contentHeight(b);if(d.contentHeight<=d.containerHeight){return true}var e=new a.fn.scrollbar.Scrollbar(b,d,c);e.buildHtml().initHandle().appendEvents()})};a.fn.scrollbar.defaults={containerHeight:"auto",arrows:true,handleHeight:"auto",handleMinHeight:30,scrollSpeed:50,scrollStep:20,scrollSpeedArrows:40,scrollStepArrows:3};a.fn.scrollbar.Scrollbar=function(a,b,c){this.container=a;this.props=b;this.opts=c;this.mouse={};this.props.arrows=this.container.hasClass("no-arrows")?false:this.props.arrows};a.fn.scrollbar.Scrollbar.prototype={buildHtml:function(){this.container.wrapInner('<div class="scrollbar-pane"/>');this.container.append('<div class="scrollbar-handle-container"><div class="scrollbar-handle"/></div>');if(this.props.arrows){this.container.append('<div class="scrollbar-handle-up"/>').append('<div class="scrollbar-handle-down"/>')}var a=this.container.height();this.pane=this.container.find(".scrollbar-pane");this.handle=this.container.find(".scrollbar-handle");this.handleContainer=this.container.find(".scrollbar-handle-container");this.handleArrows=this.container.find(".scrollbar-handle-up, .scrollbar-handle-down");this.handleArrowUp=this.container.find(".scrollbar-handle-up");this.handleArrowDown=this.container.find(".scrollbar-handle-down");this.pane.defaultCss({top:0,left:0});this.handleContainer.defaultCss({right:0});this.handle.defaultCss({top:0,right:0});this.handleArrows.defaultCss({right:0});this.handleArrowUp.defaultCss({top:0});this.handleArrowDown.defaultCss({bottom:0});this.container.css({position:this.container.css("position")==="absolute"?"absolute":"relative",overflow:"hidden",height:a});this.pane.css({position:"absolute",overflow:"visible",height:"auto"});this.handleContainer.css({position:"absolute",top:this.handleArrowUp.outerHeight(true),height:this.props.containerHeight-(this.container.outerHeight(true)-this.container.height())-this.handleArrowUp.outerHeight(true)-this.handleArrowDown.outerHeight(true)+"px"});this.handle.css({position:"absolute",cursor:"pointer"});this.handleArrows.css({position:"absolute",cursor:"pointer"});return this},initHandle:function(){this.props.handleContainerHeight=this.handleContainer.height();this.props.contentHeight=this.pane.height();this.props.handleHeight=this.opts.handleHeight=="auto"?Math.max(Math.ceil(this.props.containerHeight*this.props.handleContainerHeight/this.props.contentHeight),this.opts.handleMinHeight):this.opts.handleHeight;this.handle.height(this.props.handleHeight);this.handle.height(2*this.handle.height()-this.handle.outerHeight(true));this.props.handleTop={min:0,max:this.props.handleContainerHeight-this.props.handleHeight};this.props.handleContentRatio=(this.props.contentHeight-this.props.containerHeight)/(this.props.handleContainerHeight-this.props.handleHeight);this.handle.top=0;return this},appendEvents:function(){this.handle.bind("mousedown.handle",a.proxy(this,"startOfHandleMove"));this.handleContainer.bind("mousedown.handle",a.proxy(this,"onHandleContainerMousedown"));this.handleContainer.bind("mouseenter.container mouseleave.container",a.proxy(this,"onHandleContainerHover"));this.handleArrows.bind("mousedown.arrows",a.proxy(this,"onArrowsMousedown"));this.container.bind("mousewheel.container",a.proxy(this,"onMouseWheel"));this.container.bind("mouseenter.container mouseleave.container",a.proxy(this,"onContentHover"));this.handle.bind("click.scrollbar",this.preventClickBubbling);this.handleContainer.bind("click.scrollbar",this.preventClickBubbling);this.handleArrows.bind("click.scrollbar",this.preventClickBubbling);return this},mousePosition:function(a){return a.pageY||a.clientY+(b.documentElement.scrollTop||b.body.scrollTop)||0},startOfHandleMove:function(c){c.preventDefault();c.stopPropagation();this.mouse.start=this.mousePosition(c);this.handle.start=this.handle.top;a(b).bind("mousemove.handle",a.proxy(this,"onHandleMove")).bind("mouseup.handle",a.proxy(this,"endOfHandleMove"));this.handle.addClass("move");this.handleContainer.addClass("move")},onHandleMove:function(a){a.preventDefault();var b=this.mousePosition(a)-this.mouse.start;this.handle.top=this.handle.start+b;this.setHandlePosition();this.setContentPosition()},endOfHandleMove:function(c){a(b).unbind(".handle");this.handle.removeClass("move");this.handleContainer.removeClass("move")},setHandlePosition:function(){this.handle.top=this.handle.top>this.props.handleTop.max?this.props.handleTop.max:this.handle.top;this.handle.top=this.handle.top<this.props.handleTop.min?this.props.handleTop.min:this.handle.top;this.handle[0].style.top=this.handle.top+"px"},setContentPosition:function(){this.pane.top=-1*this.props.handleContentRatio*this.handle.top;this.pane[0].style.top=this.pane.top+"px"},onMouseWheel:function(a,b){this.handle.top-=b;this.setHandlePosition();this.setContentPosition();if(this.handle.top>this.props.handleTop.min&&this.handle.top<this.props.handleTop.max){a.preventDefault()}},onHandleContainerMousedown:function(c){c.preventDefault();if(!a(c.target).hasClass("scrollbar-handle-container")){return false}this.handle.direction=this.handle.offset().top<this.mousePosition(c)?1:-1;this.handle.step=this.opts.scrollStep;var d=this;a(b).bind("mouseup.handlecontainer",function(){clearInterval(e);d.handle.unbind("mouseenter.handlecontainer");a(b).unbind("mouseup.handlecontainer")});this.handle.bind("mouseenter.handlecontainer",function(){clearInterval(e)});var e=setInterval(a.proxy(this.moveHandle,this),this.opts.scrollSpeed)},onArrowsMousedown:function(c){c.preventDefault();this.handle.direction=a(c.target).hasClass("scrollbar-handle-up")?-1:1;this.handle.step=this.opts.scrollStepArrows;a(c.target).addClass("move");var d=setInterval(a.proxy(this.moveHandle,this),this.opts.scrollSpeedArrows);a(b).one("mouseup.arrows",function(){clearInterval(d);a(c.target).removeClass("move")})},moveHandle:function(){this.handle.top=this.handle.direction===1?Math.min(this.handle.top+this.handle.step,this.props.handleTop.max):Math.max(this.handle.top-this.handle.step,this.props.handleTop.min);this.handle[0].style.top=this.handle.top+"px";this.setContentPosition()},onContentHover:function(a){if(a.type==="mouseenter"){this.container.addClass("hover");this.handleContainer.addClass("hover")}else{this.container.removeClass("hover");this.handleContainer.removeClass("hover")}},onHandleContainerHover:function(a){if(a.type==="mouseenter"){this.handleArrows.addClass("hover")}else{this.handleArrows.removeClass("hover")}},preventClickBubbling:function(a){a.stopPropagation()}};a.fn.scrollbar.contentHeight=function(a){var b=a.wrapInner("<div/>").find(":first");var c=b.css({overflow:"hidden"}).height();b.replaceWith(b.contents());return c};a.fn.defaultCss=function(b){var c={right:"auto",left:"auto",top:"auto",bottom:"auto",position:"static"};return this.each(function(){var d=a(this);for(var e in b){if(d.css(e)===c[e]){d.css(e,b[e])}}})};a.event.special.mousewheel={setup:function(){if(this.addEventListener){this.addEventListener("mousewheel",a.fn.scrollbar.mouseWheelHandler,false);this.addEventListener("DOMMouseScroll",a.fn.scrollbar.mouseWheelHandler,false)}else{this.onmousewheel=a.fn.scrollbar.mouseWheelHandler}},teardown:function(){if(this.removeEventListener){this.removeEventListener("mousewheel",a.fn.scrollbar.mouseWheelHandler,false);this.removeEventListener("DOMMouseScroll",a.fn.scrollbar.mouseWheelHandler,false)}else{this.onmousewheel=null}}};a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}});a.fn.scrollbar.mouseWheelHandler=function(b){var c=b||window.event,d=[].slice.call(arguments,1),e=0,f=true,g=0,h=0;b=a.event.fix(c);b.type="mousewheel";if(b.wheelDelta){e=b.wheelDelta/120}if(b.detail){e=-b.detail/3}if(c.axis!==undefined&&c.axis===c.HORIZONTAL_AXIS){h=0;g=-1*e}if(c.wheelDeltaY!==undefined){h=c.wheelDeltaY/120}if(c.wheelDeltaX!==undefined){g=-1*c.wheelDeltaX/120}d.unshift(b,e,g,h);return a.event.handle.apply(this,d)}})(jQuery,document)
