// vertical slider control

ScrollV = Class.create(Abstract, {
	
	initialize: function (scrollZone, handleWrapper, handleTrack, scrollHandle, textContent, options) {
		
		this.scrollZone = scrollZone;
		this.handleWrapper = handleWrapper;
		this.handleTrack = handleTrack;
		this.scrollHandle = scrollHandle;
		this.textContent = textContent;
		
		this.options = Object.extend({ axis: 'vertical'}, options || {});
		
		this.sliderV = new Control.Slider(scrollHandle, handleTrack, {
			axis: this.options.axis,
			range: $R(0,20),
			values: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
			onSlide: function(v) { 
				this.scrollVertical(v, $(textContent), this.sliderV);  
			}.bind(this),
			onChange: function(v) { 
				this.scrollVertical(v, $(textContent), this.sliderV); 
			}.bind(this)
		});
		
		/*if ($(textContent).scrollHeight <= $(textContent).offsetHeight) {
			this.sliderV.setDisabled();
			$(handleWrapper).hide();
		}*/
		// disable vertical scrolling if text doesn't overflow the div
		  if ($(textContent).scrollHeight <= $(textContent).offsetHeight) {
			$(handleWrapper).setStyle({
			  //'visibility':'hidden'
			  'display' : 'none' 
			}
			);
		  } else {
			$(handleWrapper).setStyle({
			  //'visibility':'visible'
			  'display' : 'block'
			})
		  }
		
		//alert($(textContent).scrollHeight+' '+$(textContent).offsetHeight);
		// mozilla
		Event.observe(scrollZone, 'DOMMouseScroll', this.wheel.bindAsEventListener(this));
		
		// IE/Opera
		Event.observe(scrollZone, 'mousewheel', this.wheel.bindAsEventListener(this));
	
	},
	
	scrollVertical: function (value, element, slider) {
		element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
	},
	
	// mouse wheel code from http://adomas.org/javascript-mouse-wheel/
	handle: function(delta) {
		this.sliderV.setValueBy(-delta);
	},

	/** Event handler for mouse wheel event. */
	wheel: function(event) {
		var delta = 0;
		if (!event) /* For IE. */
			event = window.event;
		if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;
			/** In Opera 9, delta differs in sign as compared to IE. */
			if (window.opera)
				delta = -delta;
		} else if (event.detail) { /** Mozilla case. */
			/** In Mozilla, sign of delta is different than in IE.
			* Also, delta is multiple of 3.
			*/
			delta = -event.detail/3;
		}

		/** If delta is nonzero, handle it.
		* Basically, delta is now positive if wheel was scrolled up,
		* and negative, if wheel was scrolled down.
		*/
		if (delta)
			this.handle(delta);

		/** Prevent default actions caused by mouse wheel.
		* That might be ugly, but we handle scrolls somehow
		* anyway, so don't bother here..
		*/
		if (event.preventDefault)
			event.preventDefault();
		
		event.returnValue = false;
	}
});
