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_ul:			null,
	paused:			false,
	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.options.el_ul+' li.mditem'+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);

		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.pauseable) {
			$(this.options.el_container).addEvent('mouseover', this.stopScroller.bind(this));
			$(this.options.el_container).addEvent('mouseout', this.startScroller.bind(this));
		}

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

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

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

			item.setHTML(item.innerHTML);

			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') {
			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+container_width);
			else
				if (this.options.start_offscreen)
					this.el_ul.setStyle('left', container_width+'px');
				else
					this.el_ul.setStyle('left', '0px');

			this.repeater_x();

		} else {
			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+container_height);
			else
				if (this.options.start_offscreen)
					this.el_ul.setStyle('top', container_height+'px');
				else
					this.el_ul.setStyle('top', '0px');

			this.repeater_y();
		}

	},

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

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


	repeater_x: function() {

		if (!this.paused) {

			var skip_step	= false;

			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 />OL Right (Style): '+this.el_ul.getStyle('right').toInt()+
					'<br />OL Right (Cords): '+this.el_ul.getCoordinates().right+
					'<br />OL Size (X): '+this.el_ul.getSize().size.x+
					'<br />OL Size (Y): '+this.el_ul.getSize().size.y+
					'<br />Cont. Left (S): '+this.el_container.getStyle('left').toInt()+
					'<br />Cont. Left (C): '+this.el_container.getCoordinates().left+
					'<br />Cont. Right (S): '+this.el_container.getStyle('right').toInt()+
					'<br />Cont. Right (C): '+this.el_container.getCoordinates().right+
					'<br />Total Width: '+this.total_width
			);

			if (this.options.direction == 1) {

				if ((this.options.start_offscreen) && (this.el_ul.getStyle('left').toInt() > this.el_container.getSize().size.x)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('left', -this.total_width);
						else this.options.direction = -1;
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				} else if ((!this.options.start_offscreen) && (this.el_ul.getCoordinates().left > this.el_container.getCoordinates().left)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('left', -this.total_width+this.el_container.getSize().size.x);
						else this.options.direction = -1;
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				}

			} else if (this.options.direction == -1) {

				if ((this.options.start_offscreen) && (this.el_ul.getStyle('left').toInt() < -this.total_width)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('left', this.el_container.getSize().size.x);
						else this.options.direction = 1;
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				} else if ((!this.options.start_offscreen) && (this.el_ul.getCoordinates().right < this.el_container.getCoordinates().right)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('left', this.el_container.getSize().size.x-this.total_width);
						else this.options.direction = 1;
						this.repeater_x();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				}

			}

			if (!skip_step) {
				new_pos = this.el_ul.getStyle('left').toInt() + this.options.direction*this.options.scroll_jump;
				this.el_ul.setStyle('left', new_pos+'px');
				this.el_ul.setStyle('width', this.total_width+'px');
			}
		}
		if (!skip_step) this.repeater_x.delay(this.options.scroll_speed, this);
		skip_step = false;

	},


	repeater_y: function() {

		if (!this.paused) {

			var skip_step	= false;

			this.debug_msg(
					'<br />Direction: '+this.options.direction+
					'<br />OL Bottom (Style): '+this.el_ul.getStyle('bottom').toInt()+
					'<br />OL Bottom (Cords): '+this.el_ul.getCoordinates().bottom+
					'<br />OL Top (Style): '+this.el_ul.getStyle('top').toInt()+
					'<br />OL Top (Cords): '+this.el_ul.getCoordinates().top+
					'<br />OL Size (X): '+this.el_ul.getSize().size.x+
					'<br />OL Size (Y): '+this.el_ul.getSize().size.y+
					'<br />Cont. Top (S): '+this.el_container.getStyle('top').toInt()+
					'<br />Cont. Top (C): '+this.el_container.getCoordinates().top+
					'<br />Cont. Bottom (S): '+this.el_container.getStyle('bottom').toInt()+
					'<br />Cont. Bottom (C): '+this.el_container.getCoordinates().bottom+
					'<br />Total Height: '+this.total_height+
					'<br />Total Width: '+this.total_width
			);

			if (this.options.direction == 1) {

				if ((this.options.start_offscreen) && (this.el_ul.getStyle('top').toInt() > this.el_container.getSize().size.y)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('top', -this.total_height);
						else this.options.direction = -1;
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				} else if ((!this.options.start_offscreen) && (this.el_ul.getCoordinates().top > this.el_container.getCoordinates().top)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('top', -this.el_ul.getSize().size.y+this.el_container.getSize().size.y);
						else this.options.direction = -1;
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				}

			} else if (this.options.direction == -1) {

				if ((this.options.start_offscreen) && (this.el_ul.getStyle('top').toInt() <= -this.total_height)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('top', this.el_container.getSize().size.y);
						else this.options.direction = 1;
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				} else if ((!this.options.start_offscreen) && (this.el_items[this.el_items.length-1].getCoordinates().top <= this.el_container.getCoordinates().top)) {
					(function() {
						if (this.options.restart_beginning) this.el_ul.setStyle('top', 0);
						else this.options.direction = 1;
						this.repeater_y();
					}).delay(this.options.pausetime, this);
					skip_step = true;
				}

			}

			if (!skip_step) {
				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_step) this.repeater_y.delay(this.options.scroll_speed, this);
		skip_step = false;
	},

	pauseScroller: function() { 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);

