/**







*/
function buildScrollPane(scrollPane, scrollBar, props) {
	var opt=jQuery.extend({
		orientation: "vertical", mouseWheel: true, scrollBarClass: "ui-scrollbar"}, props);
	

	scrollBar.addClass(opt.scrollBarClass);
	
	scrollBar.slider({
		animate: true,
		range: true,
		orientation: opt.orientation,
		start: function(event, ui) {





			var this_slider=$(this).data("slider");

			this_slider.dragRange=$(event.originalEvent.target).is(".ui-slider-range");
			

			if(!this_slider.super_slide) {
				this_slider.super_slide=this_slider._slide;
				
				this_slider._slide=function(event, index, newVal) {
					var position = { x: event.pageX, y: event.pageY };
					index=0;
					newVal=this._normValueFromMouse(position);
					var newValues = this.values();

					if(this.dragRange) {

						var h=scrollPane.height()/2;
						newVal-=h;
					}
					else {



						if(event.type != 'mousedown') return;
						if(newVal<newValues[index]) newVal=newValues[index]-scrollPane.height();
						else newVal=newValues[index]+scrollPane.height();
					}
					if(newVal<0) newVal=0;
					
					newValues[index]=newVal;
					newValues[index+1]=newVal+scrollPane.height();
					
					if(newValues[index+1]>this.options.max) {
						newValues[index+1]=this.options.max;
						newVal=newValues[index]=newValues[index+1]-scrollPane.height();
					}
					
					if(this.dragRange) this.setValuesEx(newValues, false, true);
					else this.setValuesEx(newValues, ( event.type == 'mousedown' && this.options.animate ), false);
				};
			}
		},
		change: function(event, ui) {

			var to=scrollBar.slider("option", "max")-ui.values[1];
			if($(this).data("slider").dragRange) scrollPane.scrollTop(to);
			else scrollPane.animate({scrollTop: to}, 200);
		}
	});
	








	var this_slider=scrollBar.data("slider");
	this_slider._refreshValue=function() {
		var o = this.options, self = this,
			animate = ( !this._animateOff ) ? o.animate : false;
		this.handles.each(function(i, j) {
			var valPercent = (self.values(i) - self._valueMin()) / (self._valueMax() - self._valueMin());
			valPercent=valPercent*( self.orientation == 'horizontal' ? self.element.width() : self.element.height());
			var _set = {}; _set[self.orientation == 'horizontal' ? 'left' : 'bottom'] = valPercent;
			$(this).stop(1,1)[animate ? 'animate' : 'css'](_set, o.animate);
			if (self.options.range === true) {
				(i == 0) && self.range.stop(1,1)[animate ? 'animate' : 'css']({ bottom: (valPercent) }, o.animate);
				(i == 1) && self.range[animate ? 'animate' : 'css']({ height: ((valPercent - lastValPercent)) }, { queue: false, duration: o.animate });
			}
			lastValPercent = valPercent;
		});
	};
	




	this_slider.setValuesEx=function(newValues, animated, triggerChangeEvent) {
		var old_animateOff=this_slider._animateOff;
		if(!animated) this._animateOff=!animated;
		
		var old_keySliding=this._keySliding;
		var old_mouseSliding=this._mouseSliding;
		this._keySliding=false;
		this._mouseSliding=false;


		this._keySliding=this._mouseSliding=!triggerChangeEvent;
		
		this.values(newValues);
		
		this._animateOff=old_animateOff
		this._keySliding=old_keySliding;
		this._mouseSliding=old_mouseSliding;
	};
	

	$(".ui-slider-handle", scrollBar).each(function(i) { $(this).hide(); });
	

	scrollBar[0].resizeScrollBar = function() {
		var maxHeight=scrollPane[0].scrollHeight;
		scrollBar.slider("option", "max", maxHeight);
		
		if(maxHeight>scrollPane.height()) scrollBar.show();
		else scrollBar.hide();
	}
	

	scrollBar[0].updateScrollBar = function(animated) {

		var max=scrollBar.slider("option", "max");
		var v1=max-scrollPane.scrollTop();
		var v0=v1-scrollPane.height();
		scrollBar.slider("setValuesEx", [v0, v1], animated, false);
	}
	

	scrollBar[0].updateScrollPane = function(animated) {

		var to=scrollBar.slider("option", "max")-scrollBar.slider("values", 1);
		scrollPane.scrollTop(to);
	}
	
	scrollBar[0].resizeScrollBar();
	scrollBar[0].updateScrollBar();
	
	if(opt.mouseWheel) {
		scrollPane.bind("mousewheel", function (event, delta) {
			delta = delta || (event.wheelDelta ? event.wheelDelta / 120 : (event.detail) ? -event.detail/3 : 0);
			var mouseWheelMultiplier=50;
			var to="-="+(delta*mouseWheelMultiplier)+"px";






			
			scrollPane.scrollTop(scrollPane.scrollTop()-(delta*mouseWheelMultiplier));
			scrollBar[0].updateScrollBar();
			
			return false;
		});
	}
}

