var Thumbs = Class.create({
	initialize: function(id,numSpots,numLiveSpots,fadeTime,switchTime) {
		this.thumbsId = id;
		this.fadeTime = fadeTime;
		this.switchTime = switchTime;
		this.numSpots = numSpots;
		this.numLiveSpots = numLiveSpots;
		this.spots = new Array();
		this.items = new Array();
		this.thisItem = 0;
		this.pe = null;
	},
	
	add: function(thumbsPhoto)
	{
		this.items.push(thumbsPhoto);
	},
	
	place: function(locationId)
	{
		var output = "<ul>";
		
		if ( this.numSpots > 0 )
		{
			for( var i = 0; i < this.numSpots; i++ )
			{
				output += "<li><div class='" + this.thumbsId + "-emptythumb'></div></li>";
			}
		}

		output += "</ul>";
		
		$(locationId).innerHTML = output;
		
		for( var j = 0; j < this.numLiveSpots; j++ )
		{
			var spotsLeft = $$("." + this.thumbsId + "-emptythumb");
			var randomItem = Math.floor(Math.random()*spotsLeft.length);
			spotsLeft[randomItem].removeClassName(this.thumbsId + "-emptythumb");
			spotsLeft[randomItem].addClassName(this.thumbsId + "-usedthumb");
			spotsLeft[randomItem].setStyle({
				opacity: 1,
				backgroundImage: "url(" + this.items[this.thisItem].src + ")"
			});

			this.goNext();/**/
		}
	},
	
	goNext: function()
	{
		if ( this.thisItem + 1 >= this.items.length )
			this.thisItem = 0;
		else
			this.thisItem += 1;

		var img = new Image();
		img.src = this.items[this.thisItem].src;
	},
	
	moveOn: function()
	{
		var spotsLeft = $$("." + this.thumbsId + "-emptythumb");
		if ( spotsLeft.length > 0 )
			var randomLeftItem = Math.floor(Math.random()*spotsLeft.length);
			
		var spotsTaken = $$("." + this.thumbsId + "-usedthumb");
		if ( spotsTaken.length > 0 )
			var randomTakenItem = Math.floor(Math.random()*spotsTaken.length);

		if ( randomTakenItem >= 0 )
		{
			spotsLeft[randomLeftItem].removeClassName(this.thumbsId + "-emptythumb");
			spotsLeft[randomLeftItem].addClassName(this.thumbsId + "-usedthumb");
			spotsLeft[randomLeftItem].setStyle({
				opacity: 0.1,
				backgroundImage: "url(" + this.items[this.thisItem].src + ")"
			});
			spotsLeft[randomLeftItem].morph("opacity: 1");
	
			this.goNext();/**/
		}

		if ( randomTakenItem >= 0 )
		{
			spotsTaken[randomTakenItem].removeClassName(this.thumbsId + "-usedthumb");
			spotsTaken[randomTakenItem].addClassName(this.thumbsId + "-emptythumb");
			spotsTaken[randomTakenItem].morph("opacity: 0");
		}
	},
	
	play: function()
	{
		this.pe = new PeriodicalExecuter( function(pe){ this.moveOn() }.bind(this), this.switchTime );																											 
	}	
});

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