var MooScrollGallery = new Class({

	presets: {
		steps: 1,
		speed: 75,
		direction: 'left',
		pauseOnOver: true,
		pauseOnContainerOver: true
	},

    initialize: function(element, presets) {
		this.setOptions( $merge(this.presets, presets) );
	    this.container = element;
	    this.build();
	},

    setDirection: function(dir){
        if (dir) this.options.direction = dir;
        this.is_horizontal = (this.options.direction == 'left' || this.options.direction == 'right');
    },

	build: function() {
		this.viewsize = this.container.getCoordinates();
		this.setDirection();
		if (this.is_horizontal) {
			if (!this.options.width) {
				// Build horizontal container
				var cntWidth = 0, cntHeight = 0;
				$each( this.container.children, function(item,index) {
					if (!Browser.Engine.trident4) {
						cntWidth += item.getWidth()
							+ item.getStyle('margin-left').toInt()
							+ item.getStyle('margin-right').toInt();
						cntHeight = Math.max( cntHeight, item.getHeight()
							+ item.getStyle('margin-top').toInt()
							+ item.getStyle('margin-bottom').toInt() );
					} else {
						cntWidth += item.offsetWidth;
						cntHeight = Math.max( cntHeight, item.offsetHeight );
					}
				} );
				this.options.width  = cntWidth * 2;
				this.options.height = cntHeight;
				if (this.viewsize.width >= cntWidth) return;
			}
		} else {
			if (!this.options.height) {
				// Build vertical container
				var cntWidth = 0, cntHeight = 0;
				$each( this.container.children, function(item,index) {
					if (!Browser.Engine.trident4) {
						cntHeight += item.getHeight()
							+ item.getStyle('margin-top').toInt()
							+ item.getStyle('margin-bottom').toInt();
						cntWidth = Math.max( cntWidth, item.getWidth()
							+ item.getStyle('margin-left').toInt()
							+ item.getStyle('margin-right').toInt() );
					} else {
						cntHeight += item.offsetHeight;
						cntWidth = Math.max( cntWidth, item.offsetWidth );
					}
				} );
				this.options.width  = cntWidth;
				this.options.height = cntHeight * 2;
				if (this.viewsize.height >= cntHeight) return;
			}
		}

		var wrappedObj = this.container.innerHTML;
			wrappedObj += wrappedObj;

		this.wrapper = new Element('div',{
			'class': 'scrollWrapper',
			'styles': {
				position: 'absolute',
				left: 0,
				top: 0,
				width: this.options.width,
				height: this.options.height
			}
		}).set('html', wrappedObj);

		this.container.setStyle('position', 'relative');
		this.container.empty().appendChild(this.wrapper);

		this.scrollsize = this.wrapper.getCoordinates();

		if (this.is_horizontal)
			this.scrollsize.width /= 2;
		else
			this.scrollsize.height /= 2;

		this.addMouseEvents();

	    this.timer = this.startScroll.delay(this.options.speed, this);
	},

	addMouseEvents : function(){
		var mousevents = {
            'mouseenter' : function(me){this.clearTimer();}.bind(this),
			'mouseleave' : function(me){this.timer = this.startScroll.delay(this.options.speed, this);}.bind(this)
		};
	    if (this.options.pauseOnOver) {
	        this.wrapper.addEvents( mousevents );
	    } else
	    if (this.options.pauseOnContainerOver) {
	        this.container.addEvents( mousevents );
	    }
	},

	stopScroll: function(){
		this.clearTimer();
		if (this.options.pauseOnOver) this.wrapper.removeEvents();
	},

	resumeScroll: function(){
        this.stopScroll();
        if (this.options.pauseOnOver) this.addMouseEvents();
        this.timer = this.startScroll.delay(this.options.speed, this);
	},

	startScroll: function(){
		var pos, newpos, steps = this.options.steps;
			pos = this.is_horizontal ? this.wrapper.getStyle('left').toInt() : this.wrapper.getStyle('top').toInt();
		switch (this.options.direction) {
			case 'top':
				newpos = pos - steps;
				if (newpos < -this.scrollsize.height) newpos += this.scrollsize.height;
				this.wrapper.setStyle( 'top',  newpos );
				break;
			case 'left':
				newpos = pos - steps;
				if (newpos < -this.scrollsize.width) newpos += this.scrollsize.width;
				this.wrapper.setStyle( 'left', newpos );
				break;
			case 'right':
				newpos = pos + steps;
				if (newpos >= 0) newpos -= this.scrollsize.width;
				this.wrapper.setStyle( 'left', newpos );
				break;
			case 'bottom':
				newpos = pos + steps;
				if (newpos >= 0) newpos -= this.scrollsize.height;
				this.wrapper.setStyle( 'top',  newpos );
				break;
		}
		this.timer = this.startScroll.delay(this.options.speed, this);
	},

    clearTimer: function(){
		$clear(this.timer);
	}

});

MooScrollGallery.implement(new Options);

