var Slideshow = Class.create({

	seconds: null,

	

	images: null,

	timer: null,

	current: null,

	running: false,

	locked: false,

	

	initialize: function(container, seconds)

	{

		container = $(container);

		this.seconds = seconds || 10;

		

		//this.images = container.getElementsByClassName('image-holder');
		this.images = $$('div.image-holder');
		

		var max_height = Util.getMaxHeight(this.images);

		

		container.setStyle({

			position: 'relative',

			height: max_height + 'px'

		});

		

		this.images.each( function(element) {

			element.setStyle({

				position: 'absolute',

				height: max_height + 'px',

				display: 'none'

			});

		});

		

		this.images[0].show();

		this.current = 0;

		

		// Attach listeners

		$('slideshow-previous').observe('click', this.previous.bind(this));

		$('slideshow-play').observe('click', this.toggle.bind(this));

		$('slideshow-next').observe('click', this.next.bind(this));	

		

		this.play();	

	},

	

	play: function()

	{

		this.timer = setInterval(this.next.bind(this), this.seconds * 1000);

		$('slideshow-play').src = "/images/gallery/pause.png";

		Images.fix('slideshow-play');

		this.running = true;

	},

	

	pause: function()

	{

		clearInterval(this.timer);

		$('slideshow-play').src = "/images/gallery/play.png";

		Images.fix('slideshow-play');

		this.running = false;

	},

	

	toggle: function()

	{

		if ( !this.running )

		{

			this.play();

		}

		else

		{

			this.pause();

		}

	},

	

	previous: function()

	{

		if ( this.current == 0 )

		{

			var previous = this.images.length - 1;

		}

		else

		{

			var previous = this.current - 1;

		}

		

		this.effects(this.current, previous);

	},

	

	next: function()

	{

		if ( this.current == (this.images.length - 1) )

		{

			var next = 0;

		}

		else

		{

			var next = this.current + 1;

		}

		

		this.effects(this.current, next);

	},

	

	effects: function(before, after)

	{

		if ( this.locked )

		{

			return;

		}

		

		this.locked = true;

		

		new Effect.Fade(this.images[before], {duration: 0.75});

		new Effect.Appear(this.images[after], {duration: 0.75, afterFinish: this.unlock.bind(this)});

		

		this.current = after;

	},

	

	unlock: function()

	{

		this.locked = false;

	}

});