
if(!SymControl) var SymControl = {};
SymControl.Scrollbar = Class.create();

// options:
//  axis: 'vertical', or 'horizontal' (default)
//
// callbacks:
//  onChange(value)
//  onSlide(value)
SymControl.Scrollbar.prototype = {

  initialize: function(scrollable, handle, track, scrollup, scrolldown) {
		this.scrollable = scrollable;
		this.handle = handle;
		this.track = track;
		this.scrollup = scrollup;
		this.scrolldown = scrolldown;
		
		var scrollbar = this;
		
		// vertical slider control
		this.slider = new Control.Slider(this.handle,  this.track, {
			axis: 'vertical',
			onSlide: function(v) { scrollbar.scrollVertical(v, $(scrollbar.scrollable), scrollbar.slider);  },
			onChange: function(v) { scrollbar.scrollVertical(v, $(scrollbar.scrollable), scrollbar.slider); }
		});
		
		this.myObservers = this.addMyObservers.bindAsEventListener(this);
		Event.observe(window, 'load', this.myObservers);
		//Event.observe(window, 'load', disableScrollbar);

	},		
	addMyObservers: function() {
		
		this.scrollupclicked = this.verticalScrollUp.bindAsEventListener(this);
		$(this.scrollup).observe("click", this.scrollupclicked);

		this.scrolldownclicked = this.verticalScrollDown.bindAsEventListener(this);
		$(this.scrolldown).observe("click", this.scrolldownclicked);
		
		this.scrolldown_mousedown = this.verticalScrollDownStart.bindAsEventListener(this);
		$(this.scrolldown).observe("mousedown", this.scrolldown_mousedown);

		this.scrolldown_mouseup= this.verticalScrollDownEnd.bindAsEventListener(this);
		$(this.scrolldown).observe("mouseup", this.scrolldown_mouseup);
		
		this.scrollup_mousedown = this.verticalScrollUpStart.bindAsEventListener(this);
		$(this.scrollup).observe("mousedown", this.scrollup_mousedown);

		this.scrollup_mouseup = this.verticalScrollUpEnd.bindAsEventListener(this);
		$(this.scrollup).observe("mouseup", this.scrollup_mouseup);
	},
	scrollVertical: function (value, element, slider) {
		// scroll the element vertically based on its width and the slider maximum value
		element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
	},
	verticalScrollUp: function (e) {
		this.slider.setValue(this.slider.value - ($(this.handle).offsetHeight / $(this.scrollable).scrollHeight));
	},
	verticalScrollDown: function(e) {
		this.slider.setValue(this.slider.value + ($(this.handle).offsetHeight / $(this.scrollable).scrollHeight));
	},
	stopVerticalScroller: function() {
		if (this.vscroller != null) {
			this.vscroller.stop();
		}
	},
	verticalScrollUpStart: function() {
		this.stopVerticalScroller();
		this.vscroller = new PeriodicalExecuter(this.scrollupclicked, 0.1);
	},
	verticalScrollUpEnd: function() {
		this.stopVerticalScroller();
	},
	verticalScrollDownStart: function() {
		this.stopVerticalScroller();
		this.vscroller = new PeriodicalExecuter(this.scrolldownclicked, 0.1);
	},
	verticalScrollDownEnd: function() {
		this.stopVerticalScroller();
	},
	disableScrollbar: function(){
		// disable vertical scrolling if text doesn't overflow the div
		if ($(this.scrollable).scrollHeight <= $(this.scrollable).offsetHeight) {
			this.slider.setDisabled();
			$(this.track).hide();
		}
	}

}
