var SlideShow = new Class({
	Implements: [Options],

	options: {
		paginate: true,
		autoRun: true,
		cont: null
	},

	initialize: function (items, options) {
		this.setOptions(options);
		this.items = items;

		if (this.items.length < 1)
			return false;

		if (!this.options.cont)
			this.cont = this.items[0].getParent();
		
		this.setOptions(options);

		this.active = 0;

		this.items.each(function (item, i) {
			$extend(item, {
				fx: new Fx.Morph(item, {duration: 500})
			});

			if (i > this.active) {
				item.setStyle('display', 'none');
			}
		}, this);

		if (this.options.paginate) {
			this.pageinate();
		}
		
		
		
		if (this.options.autoRun) {
			this.autoRun();	
		}		
	},
	
	autoRun: function () {
		this.items.addEvent('mouseover', function () {
			if (this.timer) {
				$clear(this.timer);
				this.timer = null;
			}
		}.bind(this));
		
		this.items.addEvent('mouseout', function () {
			if (!this.timer) {
				this.setUpTimer();	
			}
		}.bind(this));
		
		this.setUpTimer();
	},
	
	setUpTimer: function () {
		this.timer = function () {
			var next = this.active+1;
			if (next >= this.items.length) {
				next = 0;	
			}
			
			this.showItem(next);
		}.periodical(5000, this);
	},

	pageinate: function () {
		this.pages = new Element('ol', {
			'class': 'pageinate'
		});
		$extend(this.pages, {page: []});

		this.items.each(function (item, i) {
			this.pages.page[i] = {
				li: new Element('li'),
				a: new Element('a', {
					text: i+1,
					events: {
						click: this.showItem.pass(i, this)
					}
				})
			};

			if (i === this.active) {
				this.pages.page[i].li.addClass('active');
			}

			this.pages.page[i].a.inject(this.pages.page[i].li);
			this.pages.page[i].li.inject(this.pages);
		}, this);


		this.pages.inject(this.options.cont);
	},

	showItem: function (i) {
		var cur = this.items[this.active];
		var next = this.items[i];

		cur.setStyles({'display': 'none'});
		next.setStyles({display: 'inline',opacity: 0});
		next.fx.start({opacity: 1});

		if ($defined(this.pages)) {
			this.pages.page[this.active].li.removeClass('active');
			this.pages.page[i].li.addClass('active');
		}
		
		

		this.active = i;
	}
});