var MoeTabs = new Class({

	options: {
		autoplay:		1,
		classsfx:               '',
		container:		null,
		debug:                  false,
		direction:              1,
		duration:               2000,
		fading:			false,
		ol:			null,
		pausetime:              5000,
		pauseable:		true,
		tabs:			null,
		tabswitch:		'click',
		transition:		Fx.Transitions.Bounce.EaseOut,
		wheelstop:              true
	},

	curslide:	0,
	paused:		false,
	prevslide:	-1,
	slides:		null,
	scrollfx:	null,
	tabs:		null,
	timer:		null,
	usetabs:	false,

	initialize: function(options){

		this.setOptions(options);

		var container	= $(this.options.container),
		ol		= $(this.options.ol),
		real_width	= container.getSize().size.x,
		real_height	= container.getSize().size.y,
		total_width	= 0,
		total_height	= 0;

		if ($type(container) != 'element') { alert('MoeTabs - There is a problem. Please contact support with this message: "Container Not Found"'); return; }

		this.slides = $$('#'+this.options.container+' li.mdtabs_slides'+this.options.classsfx);
		if (this.slides.length < 1) return false;

		if ($type($(this.options.tabs)) == 'element') {

			this.usetabs = true;
			this.tabs = $(this.options.tabs).getChildren('div.mdtabs_tabhead'+this.options.classsfx);

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

				item.addEvent(this.options.tabswitch, function() {
					this.move(i, true);
					if (this.options.autoplay == 1)
						this.timer = $clear(this.timer);
						this.timer = this.next.periodical(this.options.pausetime+this.options.duration, this);
				}.bind(this));

				if ((this.options.autoplay == 1) && (this.options.pauseable)) {
					if (this.options.tabswitch != 'mouseover') item.addEvent('mouseover', function() { this.paused = true; }.bind(this));
					item.addEvent('mouseout', function() { this.paused = false; }.bind(this));
				}

			}.bind(this));

		}

		this.slides.each ( function(item, j) {

			item.setStyles({
				'display': 'inline',
				'width': real_width+'px',
				'height': real_height+'px'
			});

			if (this.options.fading === true) {
				item.setStyles({
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'opacity': 0
				});
			}

			if (this.options.pauseable) {
				item.addEvent('mouseover', function() { this.paused = true;  }.bind(this));
				item.addEvent('mouseout',  function() { this.paused = false; }.bind(this));
			}

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

		}.bind(this));

		if (this.options.fading === false) {
			ol.setStyle('width', total_width+'px');
			this.scrollfx = new Fx.Scroll(container, { transition: this.options.transition, duration: this.options.duration, wait: false, wheelStops: false } );
		}

		this.move(this.curslide, true);

		if (this.options.autoplay == 1)
			this.timer = this.next.periodical(this.options.pausetime+this.options.duration, this);

	},

	move: function(i, pauseoverride) {

		if (typeof(pauseoverride) == 'undefined') pauseoverride = false;

		if (i < 0) i = this.tabs.length;
		if (i > this.tabs.length-1) i = 0;

		this.prevslide = this.curslide;
		this.curslide = i;

		this.tabs[this.prevslide].removeClass('mdtabs_tabhead_active'+this.options.classsfx);
		this.tabs[this.curslide].addClass('mdtabs_tabhead_active'+this.options.classsfx);

		if ((this.paused === false) || (pauseoverride)) {
			if (this.options.fading === true) {

				if ($type(this.slides[this.prevslide]) == 'element') {
					if(this.slides[this.prevslide].fx){this.slides[this.prevslide].fx.stop();}
					this.slides[this.prevslide].fx = this.slides[this.prevslide].effect('opacity', {duration: this.options.duration}).start(0);
				}
				if ($type(this.slides[this.curslide]) == 'element') {
					if(this.slides[this.curslide].fx){this.slides[this.curslide].fx.stop();}
					this.slides[this.curslide].fx = this.slides[this.curslide].effect('opacity', {duration: this.options.duration}).start(1);
				}

			} else {
				this.scrollfx.toElement(this.slides[this.curslide]);
			}

		}

	},

	next: function() {
		this.move(this.curslide+1);
	},

	previous: function() {
		this.move(this.curslide-1);
	}
});

MoeTabs.implement(new Options, Chain);
