Slideshow = new Class({
	Implements: [Options],
	initialize: function(options){

		// Set options
		this.setOptions(options);

		// Objects
		this.switcher = this.options.container.getElements(this.options.switcher);
		this.numberOfSlides = this.switcher.length - 1;
		this.index = 0;

		// Resume or pause slideshow on mouseenter and mouseleave event of slideshow container
		this.options.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});

		// Navigation buttons
		if(this.options.navigation != null){
			var navigation = [];
			this.options.navigation.getElements('a').addEvent('click', function(event){ event.preventDefault(); });
			this.options.navigation.getElements('a').each(function(item, index){

				navigation.push(item.addEvent('click', function(){
					this.slide = index;

					if(this.index != this.slide){
						this.stop();
						this.show();
					}
				}.bind(this, index)));


			}.bind(this));

			this.navigation = navigation;
		}
		
		if(this.options.linkSize == 1){
			if(this.options.linkContainer != null)
				this.options.linkTarget.setStyle('width', this.switcher[0].getElement(this.options.linkContainer).getSize()['x'] + this.options.linkWidth);
			else
				this.options.linkTarget.setStyle('width', this.switcher[0].getSize()['x'] + this.options.linkWidth);
		}

		// Set fade effect
		this.switcher.set('tween', {property: 'opacity', link: 'chain', duration: this.options.transition, transition: this.options.effect});

		// Start slideshow
		this.start();
    },
	start: function(){ this.interval = this.show.periodical(this.options.duration, this); },
	restart: function(){
		this.index = 0;
		this.switcher.setStyle('z-index', 0);
		this.options.container.getElement(this.options.switcher + '.selected').setStyle('z-index', 2);
		this.interval = this.show.periodical(this.options.duration, this);
	},
	stop: function(){ clearInterval(this.interval); },
	show: function(index){
		
		this.switcher.setStyle('z-index', 0);

		// Fade out current slide
		this.switcher[this.index].setStyle('z-index', 100);
		
		// Deselect navigation element
		if(this.options.navigation != null)
			this.navigation[this.index].removeClass('selected');

		// Get new slide
		if(index == null) this.index = (this.slide != undefined ? this.slide : (this.index < this.numberOfSlides ? this.index + 1 : 0));
		else this.index = index;

		// Fade in new slide
		this.switcher[this.index].setStyles({'z-index': 200, 'opacity': 0}).tween(1);
		
		// Select navigation element
		if(this.options.navigation != null)
			this.navigation[this.index].addClass('selected');
		
		// Obtain new link
		if(this.options.linkTarget != null){
			if(this.options.linkSize == 1)
				if(this.options.linkContainer != null)
					this.options.linkTarget.setStyle('width', this.switcher[this.index].getElement(this.options.linkContainer).getSize()['x'] + this.options.linkWidth);
				else
					this.options.linkTarget.setStyle('width', this.switcher[this.index].getSize()['x'] + this.options.linkWidth);
			if(this.options.linkContainer != null)
				this.options.linkTarget.setProperty('href', 'http://' + this.switcher[this.index].getElement(this.options.linkContainer).getProperty(this.options.linkProperty));
			else
				this.options.linkTarget.setProperty('href', 'http://' + this.switcher[this.index].getProperty(this.options.linkProperty));
		}

		// Reset slides
		this.slide = undefined;
	}
});
