var MoeScroller = new Class({

	options: {
		btn_previous:		null,
		btn_next:		null,
		btn_pause:		null,
		classsfx:		'',
		container_height:	200,
		container_width:	500,
		debug:			false,
		direction:		1,
		duration:		2000,
		el_container:		null,
		el_ul:			null,
		heightunit:		'px',
		jsresize:		true,
		li_fixedheight:		true,
		li_fixedwidth:		true,
		pause_phrase:		null,
		pauseable:		false,
		pausetime:		2000,
		play_phrase:		null,
		restart_beginning:	true,
		scrollaxis:		'x',
		transition:		Fx.Transitions.Bounce.easeOut,
		wheelstop:		true,
		widthunit:		'px'
	},

	btn_previous:		null,
	btn_pause:		null,
	btn_next:		null,
	cur_pos:		0,
	el_debug:		null,
	el_container:		null,
	el_items:		null,
	el_ul:			null,
	item_length:		0,
	moeScroller:		null,
	paused:			false,
	paused2:		false,
	timer:			null,

	initialize: function(options){
		this.setOptions(options);
		if (this.options.debug === true) { this.el_debug = new Element('div', {'styles': {'border': '1px solid pink', 'display': 'block' }}).injectTop($(document.body)); }

		this.btn_previous	= $(this.options.btn_previous);
		this.btn_pause		= $(this.options.btn_pause);
		this.btn_next		= $(this.options.btn_next);
		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;

		if (this.options.jsresize === true) {
			this.el_container.setStyle('height', this.options.container_height+this.options.heightunit);
			this.el_container.setStyle('width', this.options.container_width+this.options.widthunit);
		}

		this.el_container.setStyle('z-index', '0');
		var container_width	= this.el_container.getSize().size.x;
		var container_height	= this.el_container.getSize().size.y;

		this.el_items.each( function(item,i) {

			if (this.options.jsresize === true) {
				if (this.options.scrollaxis == 'x') {
					if (this.options.li_fixedheight === true) {
						item.setStyle('height', parseFloat(container_height)+'px');
					}
					if (this.options.li_fixedwidth === true) {
						item.setStyle('width', parseFloat(container_width)+'px');
						item.setStyle('overflow', 'hidden');
					}
				} else if (this.options.scrollaxis == 'y') {
					if (this.options.li_fixedwidth === true) {
						item.setStyle('width', parseFloat(container_width)+'px');
					}
					if (this.options.li_fixedheight === true) {
						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.scrollaxis == 'x') {
			this.el_ul.setStyle('width', total_width+'px');
		} else if (this.options.scrollaxis == 'y') {
			this.el_ul.setStyle('height', total_height+'px');
		}

		if (($type(this.btn_pause) == 'element') && (this.btn_pause != null)) {
			this.btn_pause.setStyle('z-index', 99);
			this.btn_pause.addEvent('click', function(event) { this.pauseScroller2(); }.bind(this));
		}

		if (($type(this.btn_next) == 'element')  && (this.btn_next != null)) {
			this.btn_next.setStyle('z-index', 99);
			this.btn_next.addEvent('click', function(event) { this.moveNext(); }.bind(this));
		}

		if (($type(this.btn_previous) == 'element') && (this.btn_previous != null)) {
			this.btn_previous.setStyle('z-index', 99);
			this.btn_previous.addEvent('click', (function(event) { this.movePrevious(); }.bind(this)));
		}

		if (this.options.pauseable) {
			$(this.options.el_container).addEvent('mouseover', function(event) { this.stopScroller(); }.bind(this));
			$(this.options.el_container).addEvent('mouseout', function(event) { if (!this.paused2) this.pauseScroller(); }.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);
		this.debug_msg('Items: '+this.item_length+'\nCur Pos: '+this.cur_pos+'\nDirection: '+this.options.direction+'\n');
	},

	repeater: function() {

		this.debug_msg('Items: '+this.item_length+'\nCur Pos: '+this.cur_pos+'\nDirection: '+this.options.direction+'\n');

		if (!this.paused && !this.paused2) {
			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.paused2) this.startScroller(); else this.stopScroller(); },
	startScroller: function() { if ($(this.options.btn_pause) != null) $(this.options.btn_pause).innerHTML = this.options.pause_phrase; this.paused = false; this.timer = this.repeater.delay(this.options.duration+this.options.pausetime, this); },
	stopScroller: function()  { if ($(this.options.btn_pause) != null) $(this.options.btn_pause).innerHTML = this.options.play_phrase; this.paused = true; $clear(this.timer); $clear(this.repeater); },

	pauseScroller2: function() { if (this.paused2) this.startScroller2(); else this.stopScroller2(); },
	startScroller2: function() { if ($(this.options.btn_pause) != null) $(this.options.btn_pause).innerHTML = this.options.pause_phrase; this.paused = false; this.timer = this.repeater.delay(this.options.duration+this.options.pausetime, this); },
	stopScroller2: function()  { if ($(this.options.btn_pause) != null) $(this.options.btn_pause).innerHTML = this.options.play_phrase; this.paused = true; $clear(this.timer); $clear(this.repeater); },
	debug_msg: function(msg)   { if (this.el_debug != null) this.el_debug.innerHTML = msg; }

});

MoeScroller.implement(new Options);


