var RuzafaSlideshow = Class.create(
{
	slideshow:	false,
	cache:		false,
	interval:	false,
	poll:		false,
	playMutex:	false,
	clickMutex:	false,
	
	initialize: function(slideshow)
	{
		this.slideshow	=	slideshow;
		this.cache		=	this.slideshow.down(".cache");
		this.playMutex	=	false;
		this.clickMutex	=	false;
		
		var playState	=	this.slideshow.down("input[name='play']");
		
		if(playState && playState.value == "1") this.play();
		
		this.setupInterface();
	},
	
	setupInterface: function()
	{
		var po = this;
		var ui = this.slideshow.down(".user_interface");
		
		if(this.slideshow.down(".prevImage") && ui){
			//	Setup the action for when the mouse hovers on the slideshow
			this.slideshow
					.observe("mouseenter",function(){
						ui.morph("opacity:1",{
							before: function(){
								ui.setStyle({display:"block"});
							}
						});
					})
					.observe("mouseleave",function(){
						ui.morph("opacity:0",{
							after: function(){
								ui.setStyle({display:"none"});
							}
						});
					});
			
			//	Setup the action for when the prev or next buttons are clicked
			var galleryPrev = ui.down(".prev");
			var galleryNext = ui.down(".next");
			
			if(galleryPrev && galleryNext){
				galleryPrev.observe("click",function(event){
					if(po.clickMutex == false){
						po.clickMutex = true;
						po.prevImage();
					}
					
					event.stop();
					return false;
				});
				
				galleryNext.observe("click",function(event){
					if(po.clickMutex == false){
						po.clickMutex = true;
						po.nextImage();
					}
					
					event.stop();
					return false;
				});
			}
			
			//	Setup the action for the maximise button, to show the gallery
			ui.down(".max").observe("click",function(event){
				if(ui.up(".gallery")) $(document.body).down(".gallery").remove();
				
				var slideshow = ui.up(".slideshow");
				
				var options = {
					method: "post",
					parameters: {
						first:		slideshow.down(".prevImage img").src,
						slideshow:	Object.toJSON(slideshow.select(".cache img").pluck("src"))
					},
					onSuccess: function(t){
						$(document.body).scrollTo();
						
						var text = t.responseText;
						
						if(text && text.length){
							$(document.body).insert({bottom:text});
							
							$(document.body).down(".gallery .slideshow .prevImage img").observe("load",function(){
								//	Center these both in the window and image
								po.centerGallerySlideshow();
								po.centerGalleryInterface();
								
								var slideshow = new RuzafaSlideshow($(document.body).down(".gallery .slideshow"));
								
								$(document.body).down(".gallery").morph("opacity:1",1);
							});
						}
					}
				};
				var request = new Ajax.Request("/gallery/",options);
				
				event.stop();
				return false;
			});
			
			//	Setup the action where the minimise button is clicked, to destroy and remove the gallery
			ui.down(".min").observe("click",function(event){
				var gallery = $(document.body).down(".gallery").morph("opacity:0",{
					duration: 1,
					after: function(n){
						gallery.remove();
					}
				});
				
				event.stop();
				return false;
			});
		}
	},
	
	play: function()
	{
		this.poll = new PeriodicalExecuter(function(){
			if(this.playMutex == false){
				this.playMutex = true;
				this.nextImage();
			}
		}.bind(this),5);
	},
	
	pause: function()
	{
		this.poll.stop();
	},
	
	nextImage: function()
	{
		var list = this.cache.descendants();
		
		if(list.length){
			var image	=	list.first().remove();
			var box		=	new Element("div",{className:"nextImage"});
			box.insert(image);
			
			this.slideshow.insert({bottom:box});
			this.showImage("bottom");
		}
	},
	
	prevImage: function()
	{
		var list = this.cache.descendants();
		
		if(list.length){
			var image	=	list.last().remove();
			var box		=	new Element("div",{className:"nextImage"});
			box.insert(image);
			
			this.slideshow.insert({bottom:box});
			this.showImage("top");
		}
	},
	
	showImage: function(position)
	{
		var po		=	this;
		var next	=	this.slideshow.down(".nextImage");
		var prev	=	this.slideshow.down(".prevImage");
		
		next.morph("opacity:1",{
			duration:	0.5,
			after:		function(){	
				next.removeClassName("nextImage").addClassName("prevImage");
			}
		});
		
		prev.morph("opacity:0",{
			duration:	0.5,
			after:		function(){
				var image = prev.remove().down("img");
				
				if(position == "top")		po.cache.insert({top:image});
				if(position == "bottom")	po.cache.insert({bottom:image});
				
				po.playMutex	=	false;
				po.clickMutex	=	false;
			}
		});
	},
	
	centerGallerySlideshow: function()
	{
		var gallery		=	$(document.body).down(".gallery");
		var content		=	gallery.down(".content");
		var slideshow	=	content.down(".slideshow");
		var image		=	slideshow.down(".prevImage img");
		
		//		Center content
		position = ((gallery.offsetHeight/2)-(content.offsetHeight/2));
		content.setStyle({top: (position+"px")});
		
		//	Center slideshow
		position = ((content.offsetHeight/2)-(image.offsetHeight/2));
		slideshow.setStyle({top: (position+"px")});
	},
	
	centerGalleryInterface: function()
	{
		var content		=	$(document.body).down(".gallery .content");
		var image		=	content.down(".slideshow .prevImage img");
		var ui			=	content.down(".slideshow .user_interface");
		
		position = ((content.offsetHeight/2)-(ui.getDimensions().height/2));
		ui.setStyle({top: (position+"px")});
		
		ui.down(".center").setStyle({width: (image.offsetWidth+"px")});
	}
});

RuzafaSlideshow.create = function(selector)
{
	var slideshow = $(document.body).down(selector);
	if(slideshow) return new RuzafaSlideshow(slideshow);
}
