var SlideShow = Class.create();

SlideShow.prototype = 
{
	initialize: function(container, itemDelay, transitionDuration, pauseButtonImage, playButtonImage)
	{
		this._container = container;
		this._itemDelay = itemDelay || 2;
		this._transitionDuration = transitionDuration || 1;
		this._currentItemIndex = -1;
		var images = container.getElementsBySelector('.image');
		var titles = container.getElementsBySelector('.titles li');
		this._paused = true;
		this._itemElements = [];
		for(var k = 0; k < images.length; k++)
		{
			var img = images[k];
			img.style.display = 'none';
			$(img.getElementsByTagName('img')[0]).observe('load', this.onImageLoaded.bindAsEventListener(this, k));
			this._itemElements[k] = { title: titles[k], image: img, imageLoaded: false };
		}
		var pauseButton = container.getElementsBySelector('.controls a')[0];
		pauseButton.observe('click', this.onPauseButtonClick.bindAsEventListener(this));
		this._pauseButtonImage = pauseButton.getElementsBySelector('img')[0];
		this._pauseButtonImageURL = pauseButtonImage;
		this._playButtonImageURL = playButtonImage;
		this.setCurrentItem(0);
	},

	setCurrentItem: function(index)
	{
		var oldIndex = this._currentItemIndex;
		if(this._transitionEffect)
		{
			this._transitionEffect.cancel();
			this._transitionEffect = null;
		}
		this._currentItemIndex = index;
		if(oldIndex >= 0)
		{
			var oldItem = this._itemElements[oldIndex];
			oldItem.title.removeClassName('current');
			oldItem.image.style.display = 'none';
		}
		var currentItem = this._itemElements[index];
		$(currentItem.title).addClassName('current');
		currentItem.image.style.display = '';
	},

	start: function(noDelay)
	{
		if(this._paused && this._itemElements.length > 1)
		{
			this._paused = false;
			if(this._transitionEffect == null && this._itemElements[this._currentItemIndex].imageLoaded)
			{
				this.startOutTransition(noDelay);
			}
		}
	},

	stop: function()
	{
		this.setCurrentItem(0);
	},

	pause: function()
	{
		this._paused = true;
		if(this._transitionEffect != null && this._transitionEffect.options.to == 0 && this._transitionEffect.currentFrame == 0)
		{
			this._transitionEffect.cancel();
			this._transitionEffect = null;
		}
	},
	
	startOutTransition: function(noDelay)
	{
		this._transitionEffect = new Effect.Opacity(this._itemElements[this._currentItemIndex].image,
			{ from: 1.0, to: 0.0, duration: this._transitionDuration * 0.5, delay: !noDelay ? this._itemDelay : 0, afterFinish: this.onOutTransitionFinished.bind(this) });
	},

	onOutTransitionFinished: function(effect)
	{
		this.setCurrentItem((this._currentItemIndex + 1) % this._itemElements.length);
		var img = this._itemElements[this._currentItemIndex].image;
		img.setOpacity(0);
		this._transitionEffect = new Effect.Opacity(img, { from: 0.0, to: 1.0, duration: this._transitionDuration * 0.5, afterFinish: this.onInTransitionFinished.bind(this) });
	},

	onInTransitionFinished: function(effect)
	{
		if(!this._paused)
		{
			this.startOutTransition();
		}
		else
		{
			this._transitionEffect = null;
		}
	},
	
	onImageLoaded: function(e, index)
	{
		this._itemElements[index].imageLoaded = true;
		if(!this._paused && this._transitionEffect == null && index == this._currentItemIndex)
		{
			this.startOutTransition();
		}
	},
	
	onPauseButtonClick: function(e)
	{
		Event.stop(e);
		if(!this._paused)
		{
			this._pauseButtonImage.setAttribute('src', this._playButtonImageURL);
			this.pause();
		}
		else
		{
			this._pauseButtonImage.setAttribute('src', this._pauseButtonImageURL);
			this.start(true);
		}
	}
};