var MoeScroller_smooth = new Class({

	options: {
		restart_beginning:	true,
		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
		heightunit:		'px',
		widthunit:		'px',
		debug:			false,
		direction:		1,					// Direction to scroll 1 = normal, -1 = opposite
		duration:		50,					// 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
		shownav:		false,
		scrollaxis:		'x',	 				// Horizontal = horz, Vertical = vert, ZigZag = zig
		scroll_jump:		2,
		scroll_speed:		2,
		startoffscreen:		false,
		transition:		'Fx.Transitions.Bounce.easeOut',	// Transition effect to use
		wheelstop:		true					// Whether or not the mouse wheel halts the transition
	},

	btn_next:		null,
	btn_pause:		null,
	btn_previous:		null,
	el_debug:		null,
	el_container:		null,
	el_items:		null,
	el_ul:			null,
	item_length:		0,
	limit_top:		0,
	limit_bottom:		0,
	limit_left:		0,
	limit_right:		0,
	moeScroller:		null,
	next_image:		null,
	prev_image:		null,
	pause_image:		null,
	paused:			false,
	play_image:		null,
	starting_coords:	0,
	total_width:		0,
	total_height:		0,

	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.el_ul.getElements('li');
		this.moeScroller	= new Fx.Scroll(this.el_container, {
						duration: this.options.duration,
						wait: false,
						wheelStops: this.options.wheelstop,
						transition: this.options.transition
					});

		if (this.options.debug) { this.el_debug = new Element('div', {'styles': {'border': '1px solid pink', 'display': 'block', 'clear': 'both' }}).injectAfter(this.el_container); }

		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.scroll_speed > 10) this.options.scroll_speed = 10;
		if (this.options.scroll_speed > 0) this.options.duration = ((11 - this.options.scroll_speed) * 10) + 20;

		$(this.options.el_container).addEvent('mouseover', this.stopScroller.bind(this));
		$(this.options.el_container).addEvent('mouseout', this.startScroller.bind(this));

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

			if (this.options.scrollaxis == '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');
				}
			} else if (this.options.scrollaxis == 'y') {
				if (this.options.li_fixedwidth) item.setStyle('width', parseFloat(container_width)+'px');
				if (this.options.li_fixedheight) item.setStyle('height', parseFloat(container_height)+'px');
				item.setStyle('overflow', 'hidden');
				item.setStyle('display', 'block');
				item.setStyle('clear', 'both');
				item.setStyle('width', parseFloat(container_width)+'px');
			}

			this.total_height += item.getSize().size.y;
			this.total_width += item.getSize().size.x;

			if (i == 0) {
				this.limit_left += item.getSize().size.x;
				this.limit_top += item.getSize().size.y;
			}
			if (i < this.el_items.length-1) {
				this.limit_right += item.getSize().size.x;
				this.limit_bottom += item.getSize().size.y;
			}

		}.bind(this));

		this.total_height	= Math.floor(this.total_height);
		this.total_width	= Math.floor(this.total_width);
		this.item_length	= this.el_items.length;

		if (this.options.scrollaxis == 'x') {
			this.el_ul.setStyle('width', this.total_width+'px');
			this.starting_coords	= this.el_ul.getCoordinates().left;
		} else if (this.options.scrollaxis == 'y') {
			this.el_ul.setStyle('height', this.total_height+'px');
			this.starting_coords	= this.el_ul.getCoordinates().top;
		}

		if (this.btn_pause != null) this.btn_pause.addEvent('click', this.pauseScroller.bind(this));
		if (this.btn_next != null) this.btn_next.addEvent('click', this.goRight.bind(this));
		if (this.btn_previous != null) this.btn_previous.addEvent('click', this.goLeft.bind(this));

		if (this.options.scrollaxis == 'x') {
			if (this.options.startoffscreen) {
				if (this.options.direction == 1) this.el_ul.setStyle('left', (-1 * this.total_width));
				else this.el_ul.setStyle('left', this.options.container_width);
				this.limit_left = this.options.container_width;
				this.limit_right  = this.total_width;
				this.repeater_x();
			} else {
				if (this.options.direction == 1) this.el_ul.setStyle('left', (-1 * this.limit_right));
				else this.el_ul.setStyle('left', 0);
				this.limit_left = 0;
				this.repeater_x.delay(this.options.pausetime, this);
			}
		} else {
			if (this.options.startoffscreen) {
				if (this.options.direction == 1) this.el_ul.setStyle('top', (-1 * this.total_height));
				else this.el_ul.setStyle('top', this.options.container_height);
				this.limit_top = this.options.container_height;
				this.limit_bottom  = this.total_height;
				this.repeater_y();
			} else {
				if (this.options.direction == 1) this.el_ul.setStyle('top', (-1 * this.limit_bottom));
				this.limit_top = 0;
				this.repeater_y.delay(this.options.pausetime, this);
			}
		}
	},

	goLeft: function() {
		this.startScroller();
		this.options.direction = -1;
	},

	goRight: function() {
		this.startScroller();
		this.options.direction = 1;
	},

	repeater_x: function() {
		if (!this.paused) {
			var cur_pos = this.el_ul.getCoordinates().left-this.starting_coords;

			if (this.options.direction == 1) {
				if (cur_pos > (this.limit_left)) {
					if (!this.options.restart_beginning) this.options.direction = -1;
					else cur_pos = (-1 * this.limit_right);
					(function() {
						this.el_ul.setStyle('left', cur_pos+'px');
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					return;
				}
			} else if (this.options.direction == -1) {
				if (cur_pos < (-1 * this.limit_right)) {
					if (!this.options.restart_beginning) this.options.direction = 1;
					else cur_pos = this.limit_left;
					(function() {
						this.el_ul.setStyle('left', cur_pos+'px');
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					return;
				}
			}
			this.el_ul.setStyle('left', (cur_pos += this.options.direction*this.options.scroll_jump)+'px');
		}
		this.repeater_x.delay(this.options.duration, this);
	},


	repeater_y: function() {
		if (!this.paused) {
			var cur_pos = this.el_ul.getCoordinates().top-this.starting_coords;

			if (this.options.direction == 1) {
				if (cur_pos > (this.limit_top)) {
					if (!this.options.restart_beginning) this.options.direction = -1;
					else cur_pos = (-1 * this.limit_bottom);
					(function() {
						this.el_ul.setStyle('top', cur_pos+'px');
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					return;
				}
			} else if (this.options.direction == -1) {
				if (cur_pos < (-1 * this.limit_bottom)) {
					if (!this.options.restart_beginning) this.options.direction = 1;
					else cur_pos = this.limit_top;
					(function() {
						this.el_ul.setStyle('top', cur_pos+'px');
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					return;
				}
			}
			this.el_ul.setStyle('top', (cur_pos += this.options.direction*this.options.scroll_jump)+'px');
		}
		this.repeater_y.delay(this.options.duration, this);
	},

	pauseScroller: function() { if (this.paused) this.startScroller(); else this.stopScroller(); },
	startScroller: function() { this.paused = false; if (this.btn_pause != null) this.btn_pause.innerHTML = "Pause"; },
	stopScroller: function() { this.paused = true; if (this.btn_pause != null) this.btn_pause.innerHTML = "Unpause"; },
	debug_msg: function(msg) { if (this.el_debug != null) this.el_debug.innerHTML = msg+'<br style="clear: both" />'; }

});

MoeScroller_smooth.implement(new Options, Chain);
