var Scroller = new Class({
	
	initialize: function(container_id, scroller_id, property, step, dec_link_id, inc_link_id) { 
		
		this.container = $(container_id);
		//alert(container_id+": "+this.container);
		this.dec_link = $(dec_link_id);
		this.inc_link = $(inc_link_id)
		this.scroller = $(scroller_id);
		this.effect = new Fx.Morph(this.scroller);
		this.property = property;
		this.step = step;
		
		this.container_size = 0;
		this.scroller_size = 0;
		this.scroller_pos = 0;
		
		this.init();
		this.setActionHandlers();
	}
});

Scroller.implement({
	
	init: function(){
		var c_coord = this.container.getCoordinates();
		var s_coord = this.scroller.getCoordinates();
		
		this.scroller_pos = 0;
		
		if(this.property == 'top') {
			
			this.container_size = c_coord.height;
			this.scroller_size = this.getScrollerSize();
			this.scroller.setStyle('top', 0);
		}
		else {
			
			this.container_size = c_coord.width;
			this.scroller_size = this.getScrollerSize();
			this.scroller.setStyle('left', 0);
		}
	},
	
	setActionHandlers: function() {
		
		var obj = this;
		
		this.inc_link.addEvent('click', function(e){
			
			new Event(e).stop();
			obj.inc();
		});
		
		this.dec_link.addEvent('click', function(e){
			//alert('asd');
			new Event(e).stop();
			//alert('asd');
			obj.dec();
		});
		
		window.addEvent('resize', function(){
			
			obj.init();
		});
	}, 
	
	inc: function() {
		
		var obj= this;
		
		if(this.scroller_pos < 0) {
			
			var new_pos = obj.scroller_pos + obj.step;
			
			if(obj.property == 'top') {
				
				obj.effect.start({
					'top': [obj.scroller_pos, new_pos]
				});
			}
			else {
				
				obj.effect.start({
					'left': [obj.scroller_pos, new_pos]
				});
			}
			
			obj.scroller_pos = new_pos;
		}
	}, 
	
	dec: function() {
		
		var obj= this;
		
		if(this.scroller_pos > this.container_size - this.scroller_size) {
			
			var new_pos = obj.scroller_pos - obj.step;
			
			if(obj.property == 'top') {
				
				this.effect.start({
					'top': [obj.scroller_pos, new_pos]
				});
			}
			else {
				
				this.effect.start({
					'left': [obj.scroller_pos, new_pos]
				});
			}
			
			obj.scroller_pos = new_pos;
		}
	}, 
	
	getScrollerSize: function() {
		
		var res = 0;
		var obj = this;
		
		this.scroller.getChildren().each(function(el){
			
			var coord = el.getCoordinates();
			
			if(obj.property == 'top') {
				
				res += coord.height;
			}
			else {
				
				res += coord.width;
			}
		});
		
		return res;
	}
});
