var SlideShow = new Class({
	
	Implements: Options,
	
	options: {
		fadeTime: 1000,
		stayTime: 3000,
		source: 'SlideIndex',
		description_height: 46
	},
	
	slideRotationPaused: false,
	
	pauseSlideRotation: function() {
		this.slideRotationPaused = true;
	},
	
	resumeSlideRotation: function() {
		this.slideRotationPaused = false;
		//this.play();
	},
	
	initialize: function(wrapper, options) {
		var params = Array.link(arguments, {wrapper: $defined, options: Object.type});
		this.wrapper = $(params.wrapper);
		this.setOptions(params.options);
		
		this.slides = $(this.options.source).getElements('div.slide');
		this.index = 0;
		this.topSlide = this.slides[0].inject(this.wrapper);
		
		//var enterSlideFx = new Fx.Tween();
		
		this.slides.each(function(slide){
			var infoButton = slide.getElement('img.info-button');
			
			var fxMouseleave = function(){
				infoButton.setStyle('opacity', 0);
			};
			
			if (Browser.Engine.name != 'webkit'){
				var slideTween = new Fx.Tween(slide);
			}
			
			var infoPanelDisplayed = false;
			
			slide.addEvents({
				mouseenter: function(){
					infoButton.setStyle('opacity', 1);
				},
				mouseleave: fxMouseleave
			});
			
			infoButton.addEvent('click', function(){
				if (!infoPanelDisplayed) {
					if (Browser.Engine.name == 'webkit'){
						slide.setStyle('margin-top', -54);
					} else {
						slideTween.start('margin-top', [0, -54]);
					}
					slide.removeEvent('mouseleave', fxMouseleave);
					infoPanelDisplayed = true;
				} else {
					if (Browser.Engine.name == 'webkit') {
						slide.setStyle('margin-top', 0);
					} else {
						slideTween.start('margin-top', [-54, 0]);
					}
					slide.addEvent('mouseleave', fxMouseleave);
					infoPanelDisplayed = false;
				}
				
				if (infoPanelDisplayed){
					this.pauseSlideRotation(); // pause slide rotation
				}
				else {
					this.resumeSlideRotation(); // resume slide rotation
				}
			}.bind(this));
			
			/*
			var slide_description = slide.getElement('p');
			
			if (slide_description){
				
				var slideFx = new Fx.Tween(slide);
				
				var margin_delta = 46;
				
				if (slide_description.hasClass('long'))
				{
					margin_delta = 58;
					//this.options.description_height = 56;
				}
				
				var mouseEnter = function(){
					this.pause(true);
					//var margin_delta = this.options.description_height;
					slideFx.start('margin-top', [0, -margin_delta]);
				};
				slide.addEvent('mouseenter', mouseEnter.bind(this));
				
				var mouseLeave = function(){
					//var margin_delta = this.options.description_height;
					slideFx.start('margin-top', [-margin_delta, 0]).chain(function(){this.pause(false)}.bind(this));
				};
				slide.addEvent('mouseleave', mouseLeave.bind(this));
			}
			*/
		}, this);
		
		this.play.delay(this.options.stayTime, this);
	},
	
	play: function() {
		if (!this.slideRotationPaused){
			this.index = (this.index + 1) % this.slides.length;
			this.bottomSlide = this.topSlide;
			this.topSlide = this.slides[this.index]
				.fade('hide')
				.inject(this.wrapper)
				.fade('in');
				
			(function(){ this.bottomSlide.fade(0); }).delay(500, this);
		}
		this.play.delay(this.options.stayTime + this.options.fadeTime, this);
	}
	
});

