
var Slideshow = function (id, timeout) {
	this.id = id;
	this.timeout = timeout;
	this.images = $("familybox_" + this.id).getElementsByTagName("img");
	this.current = 0;
	this.initiated = false;
	this.inFade = false;
	this.inPause = false;
	this.reStart = false;
	this.running = false;
	return this;
}

with (Slideshow) {

	prototype.start = function () {
		if (this.inFade && !this.running) {
			this.reStart = true;
			return;
		}
		if (!this.initiated) {
			this.init();
		}
		this.running = true;
		
		this.doFade();
	}

	prototype.stop = function () {
		this.running = false;
		this.reStart = false;
	}

	prototype.init = function () {
		for(i=0;i<this.images.length;i++) {
			if (i==this.current) {
				this.images[i].style.display = "block";
				this.images[i].xOpacity = .99;
			} else {
				this.images[i].xOpacity = 0;
			}
		}
		this.initiated = true;
	}

	prototype.doFade = function (unpause) {
		if (unpause) {
			this.inPause = false;
		}
		if ((!this.running && !this.inFade) || this.inPause) {
			return;
		}
		cOpacity	= this.images[this.current].xOpacity;
		nIndex		= this.images[this.current+1]?this.current+1:0;
		nOpacity	= this.images[nIndex].xOpacity;
		
		cOpacity-=.05;
		nOpacity+=.05;
		
		this.images[nIndex].style.display = "block";
		this.images[this.current].xOpacity = cOpacity;
		this.images[nIndex].xOpacity = nOpacity;
		
		this.setOpacity(this.current); 
		this.setOpacity(nIndex);
		
		if(cOpacity<=0) {
			this.inFade = false;
			this.inPause = true;
			this.images[this.current].style.display = "none";
			this.current = nIndex;
			
			if (this.reStart) {
				this.running = true;
			}
			setTimeout((function(){this.doFade(true)}).bind(this), this.timeout);
		} else {
			this.inPause = false;
			this.inFade = true;
			setTimeout((function(){this.doFade()}).bind(this), 50);
		}
		
	}
	
	prototype.setOpacity = function (image) {
		var obj = this.images[image];
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}

}
