var Slideshow = new Class(
{	
	initialize: function(container)
	{
		this.container = $(container);
		this.links = $(container).getElements(".links")[0];
		this.imageLayers = [ ];
		this.currentLayer = null;
		
		this.container.getElements(".photos img").each(function(image)
		{
			var link = $(document.createElement("a"));
			link.href ="#";
			link.addClass("enabled");
			if(this.imageLayers.length == 0)
			{
				link.addClass("current");
			}
			link.innerHTML = "<span>" + 
				(this.imageLayers.length + 1) + 
				"</span>";
			this.links.appendChild(link);
			link.onclick = this.switchTo.bind(this).pass(image.parentNode);
			
			$(image.parentNode).setOpacity(0.0);
			$(image.parentNode).link = link;
			this.imageLayers.push(image.parentNode);
		},
		this);
		
		var link = $(document.createElement("a"));
		link.href ="#";
		link.addClass("enabled");
		link.addClass("next_button");
		link.innerHTML = "<span>Next</span>";
		this.links.appendChild(link);
		link.onclick = this.nextPhoto.bind(this);
				
		this.currentLayer = this.imageLayers[0];
		this.currentLayer.setOpacity(1.0);
	},

	switchTo: function(layer)
	{
		if(layer != this.currentLayer)
		{
			var fadeOut = new Fx.Morph(this.currentLayer, { duration: 400, transition: Fx.Transitions.Sine.easeOut });
			var fadeIn  = new Fx.Morph(layer, { duration: 400, transition: Fx.Transitions.Sine.easeOut  });
			this.currentLayer.setOpacity(1.0);
			layer.setOpacity(0.0);
			fadeOut.start({ opacity: 0.0 });
			fadeIn.start({ opacity: 1.0 });
			
			this.currentLayer.link.removeClass("current");
			layer.link.addClass("current");
			this.currentLayer = layer;
		}
		
		return false;
	},
	
	nextPhoto: function()
	{
		for(var i=0;i<this.imageLayers.length;i++)
		{
			if(this.imageLayers[i] == this.currentLayer)
			{
				this.switchTo(this.imageLayers[i+1] || this.imageLayers[0]);
				break;
			}
		}
		return false;
	}
});

if($("slideshow")) { Slideshow = new Slideshow("slideshow"); }

