var Scroller = Class.create();

Scroller.prototype = {
	initialize: function(id, parent, contentEl) {
		this.id = id;
		this.parentEl = $(parent);
		this.contentEl = $(contentEl);
		this.contentHeight = Element.getHeight(this.contentEl);
		this.containerHeight = Element.getHeight(this.parentEl);
		
		this.options = Object.extend({
	      heightCorr: 0
    	}, arguments[3] || {});
		if ((Element.getHeight(this.contentEl) - this.options.heightCorr) <= Element.getHeight(this.parentEl)) return false;
		
		sliderHTML = '<div id="'+this.id+'_bar" class="sliderV_bar"><div id="'+this.id+'_scrollUpBtn" class="scrollUpBtn"></div><div id="'+this.id+'_track" class="sliderV_track"><div id="'+this.id+'_handle" class="sliderV_handle"> </div></div><div id="'+this.id+'_scrollDownBtn" class="scrollDownBtn"></div></div>';
		new Insertion.Top(this.parentEl, sliderHTML);
		
		fadeinoutHTML = '<div class="scrolltextfadein"> </div>';
		fadeinoutHTML += '<div class="scrolltextfadeout"> </div>';
		new Insertion.Bottom(this.parentEl, fadeinoutHTML);
		
		this.sliderHandle = $(this.id+'_handle');
		this.setHandleHeight();
		
		this.scrollUpBtn = $(this.id+'_scrollUpBtn');
		this.scrollDownBtn = $(this.id+'_scrollDownBtn');
		
		Event.observe(this.scrollUpBtn, 'mousedown', this.scrollUpStart.bindAsEventListener(this), false);
		Event.observe(this.scrollDownBtn, 'mousedown', this.scrollDownStart.bindAsEventListener(this), false);
/*		Event.observe(this.scrollUpBtn, 'mouseover', this.scrollUpStart.bindAsEventListener(this), false);
		Event.observe(this.scrollDownBtn, 'mouseover', this.scrollDownStart.bindAsEventListener(this), false);
*/
		Event.observe(this.scrollUpBtn, 'mouseup', this.scrollStop.bindAsEventListener(this), false);
		Event.observe(this.scrollDownBtn, 'mouseup', this.scrollStop.bindAsEventListener(this), false);
/*		Event.observe(this.scrollUpBtn, 'mouseout', this.scrollStop.bindAsEventListener(this), false);
		Event.observe(this.scrollDownBtn, 'mouseout', this.scrollStop.bindAsEventListener(this), false);
*/		
		this.control = new Control.Slider(this.id+'_handle', this.id+'_track',  Object.extend({axis:'vertical', onSlide:this.scrollContent.bind(this), onChange:this.scrollContent.bind(this)}, arguments[3]||{}));
	},
	
	scrollContent: function(v){
		this.contentEl.style.top=(-1 * parseInt(v * (this.contentHeight - this.containerHeight + 20)))+'px';
	},
	
	scrollUpStart: function(){
		this.timer = setInterval(this.scrollUp.bind(this), 40);
	},

	scrollDownStart: function(){
		this.timer = setInterval(this.scrollDown.bind(this), 40);
	},
	
	scrollStop: function(){
		clearInterval(this.timer);
	},
	
	scrollUp: function(){
		if (this.control.value > 0)	{
			this.control.setValue(this.control.value - this.scrollScale);
		}
	},
	
	scrollDown: function(){
		if (this.control.value < 1)	{
			this.control.setValue(this.control.value + this.scrollScale);
		}
	},
		
	setHandleHeight: function(){
		this.sliderHandle.style.height = parseInt(((Element.getHeight(this.parentEl)-24) / Element.getHeight(this.contentEl)) * Element.getHeight(this.parentEl))+'px';
		this.scrollScale = Element.getHeight(this.parentEl) / Element.getHeight(this.contentEl) / (Element.getHeight(this.contentEl) / 40);
	}
}