/**
 * @author Arian Stolwijk
 */

var Scroll = new Class({
	
	Implements: Options,
	
	initialize: function(element, options){
		var width = 0;
		var height = 0;
		element.getChildren().each(function(child){
			var coords = child.getCoordinates()
			width += coords.width;
			height = height < coords.height ? coords.height : height;
		});
				
		this.setOptions({
			steps: 1,
			fps: 55,
			copyTimes: 2,
			pauseOnOver: true,
			width: width,
			height: height
		}, options);
		
		this.element = element;
		
		this.wrapper = new Element('div',{
			styles: {
				top: 0,
				left: 0,
				position: 'absolute',
				height: this.options.height,
				width: this.options.width * this.options.copyTimes
			}
		});
		
		this.contentWrapper = new Element('div',{
			styles: {
				position: 'relative',
				height: this.options.height,
				width: element.getCoordinates().width.toInt(),
				overflow: 'hidden'
			}
		}).inject(this.element,'after').adopt(this.wrapper);
		
		this.options.copyTimes.times(function(){
			var newElement = element.clone().setStyles({
				width: this.options.width,
				'float': 'left',
				height: this.options.height
			});
			this.wrapper.adopt(newElement);
		}.bind(this));		
		element.dispose();
		this.start();
		
		if(this.options.pauseOnOver){
			this.addMouseEvents();
		}
	},
	
    start: function(){
        this.timer = this.animate.periodical(1000/this.options.fps,this);
    },
   
    stop: function(){
        $clear(this.timer);
    },
	
	animate: function(){		
		this.wrapper.setStyle('left',this.wrapper.getStyle('left').toInt()-this.options.steps);
		if(this.wrapper.getStyle('left').toInt() <= -(this.options.copyTimes-1)*this.options.width){
			this.wrapper.setStyle('left',0);
		}
	},
	
	addMouseEvents: function(){
		this.wrapper.addEvents({
	        'mouseenter' : function(me){
	            this.stop();
	        }.bind(this),
	        'mouseleave' : function(me){
	            this.start();
	        }.bind(this)
		});
	}
	
	
});

Element.implement({
	slideScroll: function(options){
		new Scroll(this,options);
		return this;
	}
});
