// Copyright (c) 2008-2009 Label Media (http://www.labelmedia.co.uk)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For details, see the LabelEd web site: http://labeled.labelmedia.co.uk


var SlideShow = Class.create(
{
	initialize : function(container, options)
	{
		if (!container) {
			return false;
		}
	
		this.images = new Array();
		this.controls = new Array();
		this.container = container;

		this.currentImage = 0;
		this.nextImage = 0;

		this.options = {
			startTime 	:2,
			showTime 	:4,
			fadeTime 	:1,
			elements	:'img',
			height 		:'100px',
			width 		:'600px',
			controls	: false,
			controlsBackForward: false
		};

		if (options)
		{
			for (var key in this.options)
			{
				if (options[key]) {
					this.options[key] = options[key];
				}
			}
		}
		
		this.start();
	},
	
	start : function()
	{
		this.container.setStyle({
			position :'relative',
			width :this.options.width,
			height :this.options.height
		});

		this.container.getElementsBySelector(this.options.elements).each(function(element)
		{
			element.setStyle({
				position :'absolute',
				top :'0',
				left :'0',
				visibility: 'visible',
				display :'block',
				overflow :'hidden',
				width :this.options.width,
				height :this.options.height
			}).show();
			
			element.getElementsBySelector('img').each(function(image)
			{
				if (image)
				{
					image.setStyle({
						position :'absolute',
						top :'0',
						left :'0',
						visibility: 'visible',
						display :'block',
						overflow :'hidden',
						width :this.options.width,
						height :this.options.height
					}).show();
				}
			}.bind(this));
			
			this.images[this.images.length] = element;
		}.bind(this));

		if (!this.images.length) {
			return;
		}
		
		this.buildControls();
		this.setOrder();
		
		this.pe = new PeriodicalExecuter(this.changeImage.bindAsEventListener(this), this.options.startTime);
	},
	
	buildControls: function()
	{
		if (this.options.controls)
		{
			if (this.options.controlsBackForward) {
				this.options.controls.appendChild(new Element('a', {href:'#', className: 'controlBack'}).observe('click', this.changeImage.bindAsEventListener(this, {back: true})));
			}
			
			for (i = 0; i < this.images.length; i++)
			{
				this.controls[this.controls.length] = new Element('a', {href:'#'}).observe('click', this.changeImage.bindAsEventListener(this, {index: i})).update((i + 1));
				this.options.controls.appendChild(this.controls[this.controls.length-1]);
			}
			
			if (this.options.controlsBackForward) {
				this.options.controls.appendChild(new Element('a', {href:'#', className: 'controlForward'}).observe('click', this.changeImage.bindAsEventListener(this, {forward: true})));
			}
		}
	},
	
	changeImage : function(event, memo)
	{
		event.stop();
		
		if (this.pe) {
			this.pe.stop();
		}
		
		this.currentImage = this.nextImage;
		
		if (memo)
		{
			if (memo.index != null) {
				this.nextImage = memo.index % this.images.length;
			}
			else if (memo.back) {
				this.nextImage--;
			}
			else if (memo.forward) {
				this.nextImage++;
			}
		}
		else {
			this.nextImage = (this.nextImage + 1) % this.images.length;
		}

		if (this.nextImage < 0) {
			this.nextImage = 0;
		}
		else if (this.nextImage >= this.images.length) {
			this.nextImage = this.images.length - 1;
		}
		
		this.setOrder();
		
		new Effect.Appear(this.images[this.nextImage], {
			duration: this.options.fadeTime
		});

		this.pe = new PeriodicalExecuter(this.changeImage.bindAsEventListener(this), this.options.showTime);

	},
	
	setOrder : function()
	{
		for (i = 0; i < this.images.length; i++)
		{
			if (i == this.currentImage)
			{
				this.images[i].setStyle({
					zIndex :'100'
				});
			}
			else if (i == this.nextImage)
			{
				this.images[i].setStyle({
					zIndex :'200'
				}).setOpacity(0);
			}
			else
			{
				this.images[i].setStyle({
					zIndex :'0'
				});
			}
			
			if (this.options.controls && this.controls[i])
			{
				if (i == this.nextImage) {
					this.controls[i].addClassName('active');
				}
				else {
					this.controls[i].removeClassName('active');
				}
			}
		}
	}
});

