var MoeScroller_smooth = new Class({

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

	btn_next:		null,
	btn_pause:		null,
	btn_previous:		null,
	el_debug:		null,
	el_container:		null,
	el_items:		null,
	el_userstops:		null,
	el_ul:			null,
	limit_0:		0,
	limit_1:		0,
	paused:			false,
	paused2:		false,
	stops:			new Array(),
	userstops:		new Array(),
	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.mditem'+this.options.classsfx);
		this.el_userstops	= this.el_ul.getElements('div.mdstop'+this.options.classsfx);

		if (this.options.debug) {
			this.el_debug = new Element('div', {
				'styles': {
					'background': '#fff',
					'border': '1px solid pink',
					'display': 'block',
					'clear': 'both',
					'position': 'fixed',
					'top': '50px',
					'right': '50px',
					'width': '200px',
					'height': 'auto',
					'z-index': '100',
					'opacity': '1'
				}
			}).injectTop($(document.body));
		}

		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.options.container_width     = this.el_container.getSize().size.x;
		this.options.container_height    = this.el_container.getSize().size.y;

		if (this.options.scroll_speed < 10) this.options.scroll_speed = 10;

		if (($type(this.btn_pause) == 'element') && (this.btn_pause != null)) {
			this.btn_pause.setStyle('z-index', 99);
			this.btn_pause.addEvent('click', (function() { 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.paused2 = false; this.paused = false; this.goRight(); }.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.paused2 = false; this.paused = false; this.goLeft(); }.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.startScroller(); }.bind(this));
		}

		this.el_userstops.each( function(userstop,h) {
			this.userstops[this.userstops.length] = this.el_container.getTop()-userstop.getTop();
		}.bind(this));

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

			if (this.options.li_fixedheight) item.setStyle('height', parseFloat(this.options.container_height)+'px');
			if (this.options.li_fixedwidth)  item.setStyle('width', parseFloat(this.options.container_width)+'px');
			if ((this.options.li_fixedheight) && (this.options.li_fixedwidth)) item.setStyle('overflow', 'hidden');

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

		}.bind(this));

		this.total_height	= Math.floor(this.total_height);
		this.total_width	= Math.floor(this.total_width);

		if (this.options.scrollaxis == 'x') {

			this.el_ul.setStyle('width', this.total_width+'px');

			if (this.options.start_offscreen) {
				this.limit_0 = -(this.total_width-1);
				this.limit_1 = this.options.container_width+1;
			} else {
				this.limit_0 = -this.total_width+this.options.container_width;
				this.limit_1 = 0;
			}

			if (this.options.direction == 1)
				if (this.options.start_offscreen)
					this.el_ul.setStyle('left', -this.total_width+'px');
				else
					this.el_ul.setStyle('left', -this.total_width+this.options.container_width+'px');
			else
				if (this.options.start_offscreen)
					this.el_ul.setStyle('left', this.options.container_width+'px');
				else
					this.el_ul.setStyle('left', '0px');

			( function() { this.repeater_x(); }.delay(this.options.pausetime, this) );

		} else {	// Scroll Axis = Y

			if (this.options.start_offscreen) {
				this.limit_0 = -this.total_height-1;
				this.limit_1 = this.options.container_height+1;
			} else {
				this.limit_0 = -this.total_height+this.options.container_height;;
				this.limit_1 = 0;
			}

			if (this.options.direction == 1)
				if (this.options.start_offscreen)
					this.el_ul.setStyle('top', -this.total_height);
				else
					this.el_ul.setStyle('top', -this.total_height+this.options.container_height);
			else
				if (this.options.start_offscreen)
					this.el_ul.setStyle('top', this.options.container_height+'px');
				else
					this.el_ul.setStyle('top', '0px');

			( function() { this.repeater_y(); }.delay(this.options.pausetime, this) );
		}

	},

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

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


	repeater_y: function() {

		var skip_repeat	= false;
		var skip_move	= false;
		var curpos = this.el_ul.getStyle('top').toInt();

		this.debug_msg(
				'<br />Direction: '+this.options.direction+
				'<br />OL Top (Style): '+this.el_ul.getStyle('top').toInt()+
				'<br />OL Top (Cords): '+this.el_ul.getCoordinates().top+
				'<br />Cont. Top (C): '+this.el_container.getCoordinates().top+
				'<br />Total Height: '+this.total_height+
				'<br />Total Width: '+this.total_width+
				'<br />Limit 0: '+this.limit_0+
				'<br />Limit 1: '+this.limit_1+
				'<br />User Stops: '+this.userstops+
				'<br />Cur Top: '+curpos+
				'<br />Paused: '+this.paused+
				'<br />Paused2: '+this.paused2
		);

		if (!this.paused && !this.paused2) {

			if (false) {
/*
			if ((this.userstops.length > 0) && (this.userstops.contains(curpos))) {
				this.userstops.each( function(userstop, i) {
					if (userstop == curpos) {
						tmppause = this.el_userstops[i].getProperty('rel');
						if (tmppause != parseInt(tmppause)) tmppause = this.options.pausetime;
						this.repeater_y.delay(tmppause, this);
						skip_repeat = true;
					}
				}.bind(this));

*/
			} else {
				if (curpos < this.limit_0) {

					if (this.options.restart_beginning) {
						if (this.options.start_offscreen) {
							( function() {
								this.el_ul.setStyle('top', this.options.container_height+'px');
								this.repeater_y();
							}.delay(this.options.pausetime, this) );
							skip_repeat = true;
							skip_move   = true;
						} else {
							( function() {
								this.el_ul.setStyle('top', '0px');
								this.repeater_y();
							}.delay(this.options.pausetime, this) );
							skip_repeat = true;
							skip_move   = true;
						}
					} else {
						skip_repeat	= true;
						skip_move	= true;
						( function() {
							this.options.direction = 1;
							new_pos = this.el_ul.getStyle('top').toInt() + this.options.direction*this.options.scroll_jump;
							this.el_ul.setStyle('top', new_pos+'px');
							this.repeater_y();
						}.delay(this.options.pausetime, this) );
					}

				}

				if (curpos > this.limit_1) {

					if (this.options.restart_beginning) {
						if (this.options.start_offscreen) {
							( function() {
								this.el_ul.setStyle('top', -this.total_height+'px');
								this.repeater_y();
							}.delay(this.options.pausetime, this) );
							skip_repeat = true;
							skip_move   = true;
						} else {
							( function() {
								this.el_ul.setStyle('top', -this.total_height+this.options.container_height+'px');
								this.repeater_y();
							}.delay(this.options.pausetime, this) );
							skip_repeat = true;
							skip_move   = true;
						}
					} else {
						skip_move   = true;
						skip_repeat	= true;
						( function() {
							this.options.direction = -1;
							new_pos = this.el_ul.getStyle('top').toInt() + this.options.direction*this.options.scroll_jump;
							this.el_ul.setStyle('top', new_pos+'px');
							this.repeater_y();
						}.delay(this.options.pausetime, this) );
					}

				}

			}

			if (skip_move === false) {
				new_pos = this.el_ul.getStyle('top').toInt() + this.options.direction*this.options.scroll_jump;
				this.el_ul.setStyle('top', new_pos+'px');
			}

		}

		if (skip_repeat === false) this.repeater_y.delay(this.options.scroll_speed, this);
	},


	repeater_x: function() {

		var skip_repeat	= false;
		var skip_move	= false;
		var curpos = this.el_ul.getStyle('left').toInt();

		this.debug_msg(
				'<br />Direction: '+this.options.direction+
				'<br />OL Left (Style): '+this.el_ul.getStyle('left').toInt()+
				'<br />OL Left (Cords): '+this.el_ul.getCoordinates().left+
				'<br />Cont. Left (C): '+this.el_container.getCoordinates().left+
				'<br />Total Height: '+this.total_height+
				'<br />Total Width: '+this.total_width+
				'<br />Limit 0: '+this.limit_0+
				'<br />Limit 1: '+this.limit_1+
				'<br />User Stops: '+this.userstops+
				'<br />Cur Pos: '+curpos+
				'<br />Paused: '+this.paused+
				'<br />Paused2: '+this.paused2
		);

		if (!this.paused && !this.paused2) {

			if (curpos < this.limit_0) {

				if (this.options.restart_beginning) {
					if (this.options.start_offscreen) {
						( function() {
							this.el_ul.setStyle('left', this.options.container_width+'px');
							this.repeater_x();
						}.delay(this.options.pausetime, this) );
						skip_repeat = true;
					} else {
						( function() {
							this.el_ul.setStyle('left', '0px');
							this.repeater_x();
						}.delay(this.options.pausetime, this) );
						skip_repeat = true;
					}
				} else {
					skip_repeat	= true;
					skip_move	= true;
					( function() {
						this.options.direction = 1;
						new_pos = this.el_ul.getStyle('left').toInt() + this.options.direction*this.options.scroll_jump;
						this.el_ul.setStyle('left', new_pos+'px');
						this.repeater_x();
					}.delay(this.options.pausetime, this) );
				}

			}

			if (curpos > this.limit_1) {

				if (this.options.restart_beginning) {
					if (this.options.start_offscreen) {
						( function() {
							this.el_ul.setStyle('left', -this.total_width+'px');
							this.repeater_x();
						}.delay(this.options.pausetime, this) );
						skip_repeat = true;
					} else {
						( function() {
							this.el_ul.setStyle('left', -this.total_width+this.options.container_width+'px');
							this.repeater_x();
						}.delay(this.options.pausetime, this) );
						skip_repeat = true;
					}
				} else {
					skip_repeat	= true;
					skip_move	= true;
					( function() {
						this.options.direction = -1;
						new_pos = this.el_ul.getStyle('left').toInt() + this.options.direction*this.options.scroll_jump;
						this.el_ul.setStyle('left', new_pos+'px');
						this.repeater_x();
					}.delay(this.options.pausetime, this) );
				}

			}

			new_pos = this.el_ul.getStyle('left').toInt() + this.options.direction*this.options.scroll_jump;
			if (!skip_move) this.el_ul.setStyle('left', new_pos+'px');

		}

		if (!skip_repeat) this.repeater_x.delay(this.options.scroll_speed, this);
	},

	pauseScroller2: function() { if (this.paused2) this.startScroller2(); else this.stopScroller2(); },
	startScroller2: function() { this.paused2 = false; if (this.btn_pause != null) this.btn_pause.innerHTML = this.options.pause_phrase; },
	stopScroller2: function() { this.paused2 = true; if (this.btn_pause != null) this.btn_pause.innerHTML = this.options.play_phrase; },

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

});

MoeScroller_smooth.implement(new Options, Chain);
