/**
 * BluScroller
 *
 *
 * Written by the Ipster 2008.
 * Hell yeah, it's good.
 *
 * Requires Fx.Scroll (Mootools 1.2)
 */
var BluScroller = new Class({
	
	Implements: Options,
	
	container: null,
	fx: null,
	
	options: {
		speed: 1.5,
		fxOptions: {}
	},
	
	initialize: function(container, options) {
		this.container = $(container);
		this.content = this.container.getElement('div.content');
		this.setOptions(options);
		
		/* Get scroll size */
		this.scrollSize = this.content.getScrollSize();
		
		/* Build scroll FX */
		this.fx = new Fx.Scroll(this.content, this.options.fxOptions);
		this.content.setStyle('overflow', 'hidden');
		
		/* Add scroller events */
		this.container.getElements('.right').addEvents({
			'mouseenter': function(event){
				this.scrollTo('right');
			}.bind(this),
			'mouseleave': function(event){
				this.stop();
			}.bind(this)
		});
		this.container.getElements('.left').addEvents({
			'mouseenter': function(event){
				this.scrollTo('left');
			}.bind(this),
			'mouseleave': function(event){
				this.stop();
			}.bind(this)
		});

		return this;
	},
			
	scrollTo: function(s){
		this.fx.cancel();
		
		/* Get size of current element */
		var size = this.content.getSize();
		
		/* Get current scroll position */
		var scrollPos = this.content.getScroll();
		var x = 0, y = 0;
		
		/* Determine new co-ordinates from direction string/object */
		var type = $type(s);
		switch ($type(s)) {
			case 'string':
				switch(s) {
					case 'bottom':
						y = this.scrollSize.y - size.y;
						break;
					case 'right':
						x = this.scrollSize.x - size.x;
						break;
				}
				break;
			case 'object':
				if ($type(s.x) == 'number' && $type(s.y) == 'number') {
					x = s.x.toInt();
					y = s.y.toInt();
				}
				break;
		}
		
		/* Calculate duration to get to new offset */
		var xOffset = Math.max(scrollPos.x, x) - Math.min(scrollPos.x, x);
		var yOffset = Math.max(scrollPos.y, y) - Math.min(scrollPos.y, y);
		this.fx.options.duration = Math.max(xOffset, yOffset) * this.options.speed;

		/* Start FX */
		this.fx.start(x.toInt(), y.toInt());
		
		return this;
	},	
	
	stop: function(direction){
		this.fx.cancel();
		return this;
	}
	
});
