var Scroll = Class.create();
Scroll.prototype = {	
	slider_obj: null,		
	scroller_obj: null,
	
	initialize: function(scroller_container, scroller_content, scroller_slider, scroller_slider_handle, scroller_slider_track) {
		this.slider_obj      = this.getControlSlider(scroller_slider_handle, scroller_slider_track);
		this.scroller_obj    = this.getScroller(scroller_container, scroller_content, this.slider_obj);
		this.scroller_slider = $(scroller_slider);
		
		this.setEventSliderOnChange(this.slider_obj);
		this.setEventSliderOnSlide(this.slider_obj);
		this.setEventOnMouseWheel(scroller_container);
		/* this.setEventOnResize(); */
		
		if (!this.scroller_obj.is_scroll_enabled()) {				
			this.scroller_slider.hide();			
		}else {
			var width_content       = this.getNum($(scroller_content).getStyle('width'));
			var width_slider_handle = this.getNum($(scroller_slider_handle).getStyle('width'));
			var margin_left_slider  = this.getNum($(scroller_slider).getStyle('margin-left'));
			var margin_right_slider = this.getNum($(scroller_slider).getStyle('margin-right'));
			$(scroller_content).setStyle({width: (width_content - (width_slider_handle + margin_left_slider + margin_right_slider))+'px'});
		}
	},
	
	execute: function (callback, time) {
		return new PeriodicalExecuter (callback, time);
	},
	
	getControlSlider: function (scroller_slider_handle, scroller_slider_track) {
		return new Control.Slider(scroller_slider_handle, scroller_slider_track, {axis: 'vertical'});
	},
	
	getScroller: function (scroller_container, scroller_content, scroller_slider) {
		return new scroller (scroller_container, scroller_content, scroller_slider);
	},
			
	scrollerStop: function () {
		window.clearInterval(this.scroller_obj.interval);
	},
	
	scrollerDown: function () {
		var obj = this.scroller_obj;
		this.scrollerStop();
		obj.interval = (
			this.execute(
				function (executer) {						
					obj.scroll_down(5);
				},
				0.015
			)
		).timer;
	},
	
	scrollerUp: function () {		
		var obj = this.scroller_obj;
		this.scrollerStop();
		obj.interval = (
			this.execute(
				function (executer) {						
					obj.scroll_up(5);
				},
				0.015
			)
		).timer;
	},
	
	setEventSliderOnChange: function (slider) {
		var obj = this.scroller_obj;
		slider.options.onChange = function(value) {
			if (obj.is_scroll_enabled()) { obj.scroll_to(Math.floor(obj.get_min_y() * value)); }
		};
	},
	
	setEventSliderOnSlide: function (slider) {
		var obj = this.scroller_obj;		
		slider.options.onSlide = function(value) {		
			if (obj.is_scroll_enabled()) { obj.scroll_to(Math.floor(obj.get_min_y() * value)); }
		};
	},
	
	setEventOnMouseWheel: function (scroller_container) {
		var type;
		var obj = this.scroller_obj;		
		var handler = function (event) {
			var scrollDetail;
			if (obj.is_scroll_enabled()) {				
				if (browser.is_ie || browser.is_opera || browser.is_safari) {			
					scrollDetail =  -event.wheelDelta;
				} else {
					scrollDetail = event.detail;
				}
				if (scrollDetail < 0) {
					obj.scroll_up(40);
				}
				else {
					obj.scroll_down(40);
				}
			}
			Event.stop(event);
		};
		
		if (browser.is_ie || browser.is_opera || browser.is_safari) {			
			type = 'mousewheel';
		} else {
			type = 'DOMMouseScroll';
		}
		
		Element.observe(scroller_container, type, handler.bindAsEventListener(this));		
	},
	
	setEventOnResize: function () {
		Element.observe(window, 'resize', this.onResizeContent.bindAsEventListener(this));	
	},
	
	getNum: function (string) {
		return parseInt(string);
	},
	
	reset: function () {
		if (!this.scroller_obj.is_scroll_enabled()) {				
			this.scroller_slider.hide();
		}else {
			this.scroller_slider.show();	
		}
		this.scroller_obj.scroll_to(0);
		this.slider_obj.setValue(0);
	},
	
	onResizeContent: function () {
		var scroller = this.scroller_obj;
		
		if (!scroller.is_scroll_enabled()) {				
			this.scroller_slider.hide();
		}else {
			this.scroller_slider.show();	
		}		
		if (scroller.content_height != scroller.content.offsetHeight) {
			scroller.slider.setValue(scroller.get_y() / scroller.get_min_y());
			if (!scroller.is_scroll_enabled()) 
				scroller.scroll_to(0);
			scroller.content_height = scroller.content.offsetHeight;
		}
	}	
}

function scroller(container_id, content_id, slider) {
	this.container = document.getElementById(container_id);
	this.content = document.getElementById(content_id);
	this.container_height = this.container.offsetHeight;
	this.content_height = this.content.offsetHeight;
	this.step = 5;
	this.interval = '';
	this.slider = slider;

	this.is_scroll_enabled = function() {		
		return this.content.offsetHeight > this.container.offsetHeight;
	}
	
	this.is_resize = function () {
		if (this.container_height != this.container.offsetHeight) {
			this.container_height = this.container.offsetHeight;
			this.scroll_to(0);
		}
	}
		
	this.get_y = function() {
		return parseInt(this.content.style.top);
	}

	this.scroll_to = function(y) {
		this.content.style.top = y + 'px';
	}

	this.scroll_up = function(step) {		
		if (this.is_scroll_enabled()) {
			var to = this.get_y() + step < 0 ? this.get_y() + step : 0;
			this.scroll_to(to);
			if (to == 0) window.clearInterval(this.interval);
			if (this.slider) this.slider.setValue(to / this.get_min_y());
		}
	}

	this.scroll_down = function(step) {		
		if (this.is_scroll_enabled()) {
			var to = (this.get_y() - step < 0 && this.get_y() - step >= this.get_min_y())
				? this.get_y() - step
				: this.get_min_y();

			this.scroll_to(to);
			if (to == this.get_min_y()) window.clearInterval(this.interval);
			if (this.slider) this.slider.setValue(to / this.get_min_y());
		}
	}

	this.get_min_y = function() {
		return this.container.offsetHeight - this.content.offsetHeight;
	}	
	this.scroll_to(0);
}