var Scroll = new Class({
	
	//implements
	Implements: [Options],
 
	//options
	options: {
		up_trigger: false,
		down_trigger: false,
		direction: 'vertical',	// Direction the scroller will move
		speed: 10,							// Milliseconds for refresh rate
		distance: 3							// Number of pixels we want to move our scroller content by on each refresh
	},
	
	//initialization
	initialize: function(container,options) {
		// Grab container and set options
		this.container = $(container);
		this.setOptions(options);
		var self = this;
				
		// Check we have some buttons
		if(!this.options.up_trigger || !this.options.down_trigger){
			alert('You don\'t seem to have specified valid scrolling triggers!');
			return false;
		}
		// Create wrapper around container contents
		this.wrapper = new Element('div',{
			styles: {
				position: 'absolute',
				top: '0px',
				left: '0px'
			}
		});
		this.wrapper.adopt(this.container.getChildren());
		this.wrapper.inject(this.container);
		
		// Measure container and wrapper
		this.wrapper_size = this.wrapper.getSize();
		this.container_size = this.container.getSize();
 
		// Set max and min scroll values
		this.max_scroll = this.options.direction=='horizontal' ? this.container_size.x-this.wrapper_size.x : this.container_size.y-this.wrapper_size.y;
		this.min_scroll = 0;
 
		// Add button events
		this.options.up_trigger.addEvents({
			'mousedown': function(){self.scroll_up();},
			'mouseup': function(){self.kill_animation();}
		});
		this.options.down_trigger.addEvents({
			'mousedown': function(){self.scroll_down();},
			'mouseup': function(){self.kill_animation();}
		});
	},
	
	/**
	 * Scroll up
	 * ---------
	 * Trigger event to run on mouse up
	 */
	scroll_up: function() {
		this.scroller_id = this.move.periodical(this.options.speed, this, 'pos');
	},
	
	/**
	 * Scroll down
	 * -----------
	 * Trigger event to run on mouse down
	 */
	scroll_down: function() {
		this.scroller_id = this.move.periodical(this.options.speed, this, 'neg');
	},
	
	/**
	 * kill_animation
	 * --------------
	 * Stops all animations
	 */
	kill_animation: function(){
		clearInterval(this.scroller_id);
	},
	
	/**
	 * Move
	 * ----
	 * Handles the actual moving of the content div
	 * the direction of the movement is set by the calling function
	 */
	move: function(direction) {
		var pos = this.wrapper.getStyles('top','left');
		var pos = {
			top: parseInt(pos.top),
			left: parseInt(pos.left)
		};
		if(this.options.direction=='vertical'){
			if((direction=='pos' && pos.top >= this.max_scroll) || (direction=='neg' && pos.top <= this.min_scroll)){
				var new_pos = direction=='pos' ? pos.top-this.options.distance : pos.top+this.options.distance ;
				this.wrapper.setStyle('top', new_pos+'px');
			}
		} else{
			if((direction=='pos' && pos.left >= this.max_scroll) || (direction=='neg' && pos.left <= this.min_scroll)){
				var new_pos = direction=='pos' ? pos.left-this.options.distance : pos.left+this.options.distance ;
				this.wrapper.setStyle('left', new_pos+'px');
			}
		}
	}
});
