var MoeScroller = new Class({

	options: {
		restart_beginning:		true,					// false = Restart at the beginning, true = Scroll Backwards
		btn_previous:		null,
		btn_next:		null,
		btn_pause:		null,
		container_height:	200,					// Height for the Container Div
		container_width:	500,					// Width for the Container Div
		widthunit:		'px',
		heightunit:		'px',
		debug:			false,
		direction:		1,					// Direction to scroll 1 = normal, -1 = opposite
		duration:		2000,					// Duration for Transition
		el_container:		null,					// The container div that houses the UL
		el_ul:			null,					// The UL element that houses the LI's
		li_fixedwidth:		true,					// Should we give the LI's a fixed width (horizontal scroll)
		li_fixedheight:		true,					// Should we give the LI's a fixed height (vertical scroll)
		next_image:		null,
		prev_image:		null,
		play_image:		null,
		pause_image:		null,
		pauseable:		false,					// Is the scroller pausable?
		pausetime:		2000,					// Pause time after transition
		scrolltype:		'x',	 				// Horizontal = x, Vertical = y, ZigZag = coming soon
		smoothscroll:		false,
		transition:		Fx.Transitions.Bounce.easeOut,		// Transition effect to use
		wheelstop:		true,					// Whether or not the mouse wheel halts the transition
		classsfx:		''
	},

	btn_next:		null,
	btn_pause:		null,
	btn_previous:		null,
	cur_pos:		0,
	el_debug:		null,
	el_container:		null,
	el_items:		null,
	el_ul:			null,
	item_length:		0,
	moeScroller:		null,
	next_image:		null,
	prev_image:		null,
	pause_image:		null,
	paused:			false,
	play_image:		null,
	timer:			null,

	initialize: function(options){
		this.setOptions(options);
		this.btn_next		= $(this.options.btn_next);
		this.btn_pause		= $(this.options.btn_pause);
		this.btn_previous	= $(this.options.btn_previous);
		this.el_container	= $(this.options.el_container);
		this.el_ul		= $(this.options.el_ul);
		this.el_items		= $$('#'+this.options.el_ul+' li.mditem'+this.options.classsfx);
		this.item_length	= this.el_items.length;
		this.moeScroller	= new Fx.Scroll(this.el_container, {
						duration: this.options.duration,
						wait: false,
						wheelStops: this.options.wheelstop,
						transition: this.options.transition
					});
		var total_width		= 0;
		var total_height	= 0;

		this.el_container.setStyle('height', this.options.container_height+this.options.heightunit);
		this.el_container.setStyle('width', this.options.container_width+this.options.widthunit);

		var container_width	= this.el_container.getSize().size.x;
		var container_height	= this.el_container.getSize().size.y;

		if (this.options.debug) { this.el_debug = new Element('div', {'styles': {'border': '1px solid pink', 'display': 'block' }}).injectAfter(this.el_container); }

		this.el_items.addEvent('mouseover', this.stopScroller.bind(this));
		this.el_items.addEvent('mouseout', this.startScroller.bind(this));

		this.el_items.each( function(item,i) {

			if (this.options.scrolltype == 'x') {
				if (this.options.li_fixedheight) item.setStyle('height', parseFloat(container_height)+'px');
				if (this.options.li_fixedwidth) {
					item.setStyle('width', parseFloat(container_width)+'px');
					item.setStyle('overflow', 'hidden');
				}
				total_height += item.getSize().size.y;
				total_width += item.getSize().size.x;
			} else if (this.options.scrolltype == 'y') {
				item.setStyle('display', 'block');
				item.setStyle('clear', 'both');
				item.setStyle('width', parseFloat(container_width)+'px');
				if (this.options.li_fixedheight) {
					item.setStyle('height', parseFloat(container_height)+'px');
					item.setStyle('overflow', 'hidden');
				}
				total_width += item.getSize().size.x;
				total_height += item.getSize().size.y;
			}

		}.bind(this));

		if (this.options.scrolltype == 'x') {
			this.el_ul.setStyle('width', total_width+'px');
		} else if (this.options.scrolltype == 'y') {
			this.el_ul.setStyle('height', total_height+'px');
		}

		if (this.btn_pause != null) this.btn_pause.addEvent('click', this.pauseScroller.bind(this));
		if (this.btn_previous != null) this.btn_previous.addEvent('click', this.movePrevious.bind(this));
		if (this.btn_next != null) this.btn_next.addEvent('click', this.moveNext.bind(this));

		if (this.options.direction == -1) this.cur_pos = this.item_length-1;
		else this.cur_pos = 0;

		this.moeScroller.options.duration = 0;
		this.moeScroller.toElement(this.el_items[this.cur_pos]).chain( function() {
			this.moeScroller.options.duration = this.options.duration;
		}.bind(this));
		this.timer = this.repeater.delay(this.options.pausetime, this);
	},

	repeater: function() {
		if (!this.paused) {
			this.moeScroller.options.duration = this.options.duration;
			if ((this.cur_pos == 0) && (this.options.direction == -1))
				if (!this.options.restart_beginning) this.options.direction = 1;
				else {
					this.cur_pos = this.item_length;
					this.moeScroller.options.duration = 0;
					this.moveToElement(this.cur_pos);
					return;
				}					
			else if ((this.cur_pos == this.item_length-1) && (this.options.direction == 1))
				if (!this.options.restart_beginning) this.options.direction = -1;
				else {
					this.cur_pos = -1;
					this.moeScroller.options.duration = 0;
					this.moveToElement(this.cur_pos);
					return;
				}					

			if (this.options.direction == 1) this.moveNext();
			else this.movePrevious();
		}
	},

	moveNext: function() {
		$clear(this.repeater);
		$clear(this.timer);
		if (this.cur_pos == this.item_length-1)  this.cur_pos = 0;
		else this.cur_pos++;
		this.moveToElement(this.cur_pos);
	},

	movePrevious: function() {
		$clear(this.repeater);
		$clear(this.timer);
		if (this.cur_pos == 0) this.cur_pos = this.item_length-1;
		else this.cur_pos--;
		this.moveToElement(this.cur_pos);
	},

	moveToElement:( function(pos) {
		if (pos > this.item_length-1) pos = this.item_length-1;
		if (pos < 0) pos = 0;
		this.cur_pos = pos;
		this.moeScroller.toElement(this.el_items[pos]);
		this.timer = this.repeater.delay(this.options.duration+this.options.pausetime, this);
	}),

	pauseScroller: function() { if (this.paused) this.startScroller(); else this.stopScroller(); },
	startScroller: function() { if (this.btn_pause != null) this.btn_pause.innerHTML = 'Pause'; this.paused = false; this.timer = this.repeater.delay(this.options.duration+this.options.pausetime, this); },
	stopScroller: function() { if (this.btn_pause != null) this.btn_pause.innerHTML = 'Unpause'; this.paused = true; $clear(this.timer); $clear(this.repeater); },
	debug_msg: function(msg) { if (this.el_debug != null) this.el_debug.appendText(msg); }

});

MoeScroller.implement(new Options);

