var Gallery = Class.create({
	initialize: function(id,w,h,fadeTime) {
		this.galleryId = id;
		this.w = w;
		this.h = h;
		this.fadeTime = fadeTime;
		this.items = new Array();
		this.thisItem = 0;
		this.pe = null;
		this.lock = false;
	},
	
	add: function(galleryPhoto)
	{
		this.items.push(galleryPhoto);
	},
	
	place: function(locationId)
	{
		var output = "<div style='text-align: left; width: " + this.w + "px; height: " + this.h + "px;' id='" + this.galleryId + "'>";
		
		if ( this.items.length > 0 )
		{
			output += "<a style='display: block; background: url( /images/blank.png ); position: absolute; text-align: left; width: " + this.w + "px; height: " + this.h + "px; z-index: 13;' id='" + this.galleryId + "-click' href='" + this.items[this.getThisItem()].url + "'></a>";
			output += "<div class='" + this.galleryId + "-hold' style='position: absolute; text-align: left; width: " + this.w + "px; height: " + this.h + "px; line-height: " + this.h + "px; z-index: 11; display: none; text-align: center;' id='" + this.galleryId + "-load'><img id='" + this.galleryId + "-load-img' src='images/spacer.gif' /></div>";
			output += "<div class='" + this.galleryId + "-hold' style='position: absolute; text-align: left; width: " + this.w + "px; height: " + this.h + "px; line-height: " + this.h + "px; z-index: 10;  text-align: center;' id='" + this.galleryId + "-main'><img id='" + this.galleryId + "-main-img' src='" + this.items[this.getThisItem()].src + "' /></div>";
			output += "<div style='position: absolute; z-index: 12;' id='" + this.galleryId + "-caption'>" + this.items[this.getThisItem()].caption + "</div>";
		}
		else
		{
			output += "No images have been added";
		}

		output += "</div>";
		
		$(locationId).innerHTML = output;
	},
	
	check: function()
	{
		return !( !$(this.galleryId + "-main") || !$(this.galleryId + "-load") )
	},

	goThis: function(i)
	{
		if ( !this.lock )
		{
			this.pe.stop();
			this.setThisItem(i);
			this.playSelected();
		}
	},
	
	goNext: function()
	{
		if ( !this.lock )
		{
			this.pe.stop();
			this.playNext();
		}
	},
	
	goLast: function()
	{
		if ( !this.lock )
		{
			this.pe.stop();
			this.playLast();
		}
	},
	
	stop: function()
	{
		if ( !this.lock )
		{
			this.pe.stop();
			return true;
		}
		else
			return false;
	},
	
	play: function()
	{
		if ( !this.check() )
			return;
			
		this.preloadSurrounding();
		this.pe = new PeriodicalExecuter( function(pe){ pe.stop(); this.playNext() }.bind(this), this.items[this.getThisItem()].showTime );																											 
	},
	
	playNext: function()
	{
		this.setThisItem(this.getNextItem());
		this.playSelected();
	},
	
	playLast: function()
	{
		this.setThisItem(this.getLastItem());
		this.playSelected();
	},
	
	playThis: function(i)
	{
		if ( !this.lock )
		{
			this.pe.stop();
			this.setThisItem(i);
			this.playSelected();
		}
	},
	
	playSelected: function()
	{
		if ( !this.check() )
			return;
			
		$(this.galleryId + "-main").show();
		$(this.galleryId + "-load").hide();
		
		Event.observe( $(this.galleryId + "-load-img"), "load", function(event){
			if ( !this.check() )
				return;
				
			Event.stopObserving($(this.galleryId + "-load-img"));

			this.lock = true;
			Effect.Appear(this.galleryId + "-load", {duration: this.fadeTime, afterFinish: function(){

				this.lock = false;
				$(this.galleryId + "-caption" ).innerHTML = this.items[this.getThisItem()].caption;
				$(this.galleryId + "-click" ).setAttribute("href", this.items[this.getThisItem()].url);
				if ( !this.check() )
					return;
				
				$(this.galleryId + "-main").hide();
				
				// make the swap
				$(this.galleryId + "-main").setAttribute("id", this.galleryId + "-tmp");
				$(this.galleryId + "-load").setAttribute("id", this.galleryId + "-main");
				$(this.galleryId + "-tmp").setAttribute("id", this.galleryId + "-load");
				var tmpZIndex = $(this.galleryId + "-main").style.zIndex;
				$(this.galleryId + "-main").style.zIndex = $(this.galleryId + "-load").style.zIndex;
				$(this.galleryId + "-load").style.zIndex = tmpZIndex;

				$(this.galleryId + "-main-img").setAttribute("id", this.galleryId + "-tmp-img");
				$(this.galleryId + "-load-img").setAttribute("id", this.galleryId + "-main-img");
				$(this.galleryId + "-tmp-img").setAttribute("id", this.galleryId + "-load-img");

				this.pe = new PeriodicalExecuter( function(pe){ pe.stop(); this.playNext(); }.bind(this), this.items[this.getThisItem()].showTime );																											 
			}.bind(this)});
		}.bindAsEventListener(this));
		$(this.galleryId + "-load-img").setAttribute( "src", this.items[this.getThisItem()].src );
	},
	
	preloadSurrounding: function()
	{
		var imgNext = new Image();
		imgNext.src = this.items[this.getNextItem()].src;
		//var imgLast = new Image();
		//imgLast.src = this.items[this.getLastItem()].src;
	},	

	getThisItem: function()
	{
		return this.thisItem;
	},

	setThisItem: function(i)
	{
		this.thisItem = i;
	},

	getNextItem: function()
	{
		if ( this.getThisItem() + 1 >= this.items.length )
			return 0;
		else
			return this.getThisItem() + 1;
	},
	
	getLastItem: function()
	{
		if ( this.getThisItem() - 1 >= 0 )
			return this.getThisItem() - 1;
		else
			return this.items.length - 1;
	}
	
});

var GalleryPhoto = Class.create({
	initialize: function(src,showTime,url,caption) {
		this.src = src;
		this.showTime = showTime;
		this.caption = caption;
		this.url = url;
	}
});
