// thumb object holds id of img, delay and opacity
function Thumb(imgId) {
	this.img = document.getElementById(imgId);
	this.opacity = 0;
	this.done = false;
	// set alpha filter to zero if IE
	if (!window.addEventListener) {
		if (this.img.filters.alpha == null)
			this.img.style.filter = 'alpha(opacity = 0)';
	}
	this.delay = 0;
	this.incOpacity = function(increment) {
		if (!this.done) {
			this.opacity += increment;
			if (window.addEventListener) { // if not IE
				this.img.style.opacity = this.opacity;
			} else {
				this.img.filters.alpha.opacity = this.opacity * 100;
			}
			if (!(this.opacity < 1)) {
				this.done = true;
				thumbIndex.activeThumbs -= 1;
			}
		}
	};
}

// thumbIndex object literal sets up thumbs, stores references
// to each and executes timeout
var thumbIndex = {

	setUp : function(index) {
		this.count = index.count;
		this.delay = index.delay;
		this.increment = index.increment;
		this.interval = index.interval;
		this.minDelay = index.minDelay;
		this.maxDelay = index.maxDelay;
		this.thumbs = [];
		this.time = 0;
		this.activeThumbs = index.count;
		for ( var i = 0; i < this.count; i++) {
			this.thumbs[i] = new Thumb(index.idPrefix + i);
			this.thumbs[i].delay = Math.random()
					* (this.maxDelay - this.minDelay);
		}
	},

	start : function() {
		setTimeout(thumbIndex.fadeIn, thumbIndex.minDelay);
	}
};

thumbIndex.fadeStep = function() {
	for ( var i = 0; i < thumbIndex.count; i++) {
		if (!thumbIndex.thumbs[i].done
				&& thumbIndex.time > thumbIndex.thumbs[i].delay) {
			thumbIndex.thumbs[i].incOpacity(this.increment);
		}
	}
	thumbIndex.time += thumbIndex.interval;
};

thumbIndex.fadeIn = function() {
	if (thumbIndex.activeThumbs > 0) {
		thumbIndex.fadeStep();
		setTimeout(thumbIndex.fadeIn, thumbIndex.interval);
	}
};

