var SlideShow = new Class({
	
	Implements: Options,
	
	container: 'slideshow',
	
	options: {
			slides: 'img',
			duration: 5000,
			autosize: true,
			onSlide: function() {}
		},

	initialize: function(container, options){
		if(!$(this.container) && $type(container) != 'element'){
			return;
		}
		
		this.container = ($type(container) == 'element' ? container : $(container));
        this.setOptions(options);

		// if no slides, hide container, and exit
		if (this.container.getElement(this.options.slides) == null) {
			this.container.setStyle('display', 'none');
			return;
		}
	
		if (this.container.getStyle('position') != 'absolute') {
			this.container.setStyle('position', 'relative');
		}
		
		if (this.options.autosize) {
			this.container.setStyles({
				'width':  this.container.getElement(this.options.slides).getSize()['x'],
				'height': this.container.getElement(this.options.slides).getSize()['y']
			});
		}
		
		// init all
		this.container.getElements(this.options.slides).reverse().each(function(elm, index) {
			if (index === 0) {
				elm.setStyles({
					'display': '',
					'z-index': 5
				});
			} else {
				elm.setStyles({
					'display': 'none',
					'z-index': 1,
					'position': 'absolute'
				});
			}
		});
		
		// if multiple slides, start show
		if (this.container.getElements(this.options.slides).length > 1) {
			this.next.periodical(this.options.duration, this);
		}
    },
    
    next: function () {
		var slides = this.container.getElements(this.options.slides);
		
		// find current
		active = slides.filter(function(item, index) {
	    	return item.getStyle('z-index') > 1;
		})[0];
		
		// find next
		for (s = 0; s < slides.length; s++) {
			if (slides[s] == active) {
				if (s == slides.length - 1) {
					next = slides[0];
				} else {
					next = slides[s + 1];
				}
			}
		}
		
		next.setStyle('opacity', 0).setStyles({
			'display': '',
			'z-index': 5
		}).set('tween', {
				onStart: function() {
						active.setStyle('z-index', 1);				
					},
				onComplete: function() {
						active.setStyle('display', 'none');
						this.options.onSlide(next);
					}.bind(this)
				})
			.tween('opacity', 1);
		}
});