// JavaScript Document

function Fader(containerId, sources) {
	
	this.containerId = containerId;
	this.container = null;
	
	this.sources =  sources;
	
	this.isIe = navigator.appName.indexOf("Microsoft")!= -1;
	
	return this;
}


Fader.prototype.start = function () {
		this.container = document.getElementById(this.containerId);
		
		this.firstImage = document.createElement("img");
		this.firstImage.src = this.getNextSrc();
		this.firstImage.setAttribute("class", "fadingImage");
		this.firstImage.className = "fadingImage";
		this.container.appendChild(this.firstImage);
		if (this.isIe) {
			this.firstImage.filters.alpha.opacity=100; //IE4 syntax
		} else {
			this.firstImage.style.MozOpacity=1;
			this.firstImage.style.opacity=1; 
			this.firstImage.style.KhtmlOpacity=1; 
		}

		
		this.secondImage = document.createElement("img");
		this.secondImage.src = this.getNextSrc();
		this.secondImage.setAttribute("class", "fadingImage");
		this.secondImage.className = "fadingImage";
		
		this.container.appendChild(this.secondImage);

		//this.secondImage.style.left = "100px";

		
		window.setTimeout("fader.startFade()", 5000);		// set a timer to start a new fade in a few seconds
		
}

Fader.prototype.startFade = function () {
	this.currentFade = 100;
	this.timer = window.setInterval("fader.fade()", 50)

	
	
}

Fader.prototype.fadeFinished = function () {

	window.clearInterval(this.timer);					// cancel the fader
	window.setTimeout("fader.startFade()", 5000);		// set a timer to start a new fade in a few seconds

	// set a new src image on the invisible picture
	this.firstImage.src = this.getNextSrc();
	
	// swap first and second image names around. The variable called 'firstimage' is always faded out...
	var tmp = this.firstImage;
	this.firstImage = this.secondImage;
	this.secondImage = tmp;
	
	this.currentFade = 100;
}


Fader.prototype.fade = function() {
	
	this.currentFade -= 5;
	
	// fade out the first image	
	if (this.isIe) {
		this.firstImage.filters.alpha.opacity=this.currentFade; //IE4 syntax
	} else {
		this.firstImage.style.MozOpacity=this.currentFade / 100;
		this.firstImage.style.opacity=this.currentFade / 100;
		this.firstImage.style.KhtmlOpacity =this.currentFade / 100;
	}
	
	// fade in the second image
	if (this.isIe) {
		this.secondImage.filters.alpha.opacity= 100 - this.currentFade; //IE4 syntax
	} else {
		this.secondImage.style.MozOpacity=1 - (this.currentFade / 100);
		this.secondImage.style.opacity=1 - (this.currentFade / 100);
		this.secondImage.style.KhtmlOpacity =this.currentFade / 100;
	}
	
	// document.getElementById("infoBox").innerHTML = this.currentFade;
	
	if (this.currentFade <=0) {
		this.fadeFinished();
	}

}





//ImageObj.style.filters.alpha.opacity=90 //IE4 syntax
//ImageObj.style.MozOpacity=0.9   


Fader.prototype.getNextSrc = function () {
	
	
/*	var srcs = [
				   "/images/photos/cap008.jpg",
   				   "/images/photos/cap009.jpg",
				   "/images/photos/cap031.jpg",
				   "/images/photos/cap032.jpg",
				   "/images/photos/cap033.jpg",
				   "/images/photos/cap036.jpg",
				   "/images/photos/cap038.jpg",
				   "/images/photos/cap040.jpg",
				   "/images/photos/cap041.jpg",
				   "/images/photos/cap042.jpg",
				   "/images/photos/cap043.jpg",
				   "/images/photos/cap044.jpg",
				   "/images/photos/cap045.jpg",
			   ]
	*/
	
	var srcToReturn = "";
	var count=0;	// a saftey net.
	while ( (count<100) && ((srcToReturn == "") || (srcToReturn == this.lastSrc)) ) {
		count++;
		index = Math.floor(Math.random() * (this.sources.length));
		var srcToReturn = this.sources[index];
		
		//document.getElementById("infoBox2").innerHTML = srcToReturn;
	}
	
	this.lastSrc = srcToReturn;
	return srcToReturn;
	
}	