/**
*      Picture Fader.
*			 Show a slide show and fade in images.
*			 Assumes <div> divB is nested in <div> divA. It puts the
*			 previous (background) picture into divA and next picture
*			 into divB (with no opacity), then fades in divB by increasing
*			 it's opacity.
*
*			 Input is array of images, delay (in seconds), two div ids and the name of this object.
*
*      @author   Andrew Connick
*      @version  25/03/09
*/
function PictureFader(imgArr, divA, divB, objName) {

	this.imgArr = imgArr;
	this.curImg = 0;
	this.delay = 4;
	this.speed = 2; 						// Lower numbers yield a slower transition (more than 5 is not smooth)
	this.hldr1 = document.getElementById(divA);
	this.hldr2 = document.getElementById(divB);
	this.objName = objName;			// Name of this object, needed by set timeout

	// make sure all images are downloaded at start
	for(var i=0; i<imgArr.length; i++) {
		var img = new Image();
		img.src = imgArr[i];
	}

	// define method to swap images
	this.swapImages = function() {
		// get the previous and next image index. 
		var prvImg = this.curImg;
		if(this.curImg == this.imgArr.length-1) this.curImg = 0;
		else ++this.curImg;
		// swap images	
		this.hldr1.style.backgroundImage = "url('" + this.imgArr[prvImg] + "')";
		setOpacity(this.hldr2, 0);
		this.hldr2.style.backgroundImage = "url('" + this.imgArr[this.curImg] + "')";
		// fade in new image
		this.fadeIn(0);
	}

	// define method to fade in
	this.fadeIn = function(opacity) {
		if (document.getElementById) {
			if (opacity < 100) {
				opacity = (opacity + this.speed) * 1.05;
				setOpacity(this.hldr2, opacity);
				setTimeout(this.objName + ".fadeIn(" + opacity + ")", 50);
			}
			else setTimeout(this.objName + ".swapImages()", this.delay*1000);
		}
	}

	// define method to set initial image and start fading
	this.start = function() {
		this.hldr1.style.backgroundImage = "url('" + imgArr[0] + "')";
		this.fadeIn(100);
	}
}
	
function setOpacity(obj, opacity) {
	if (opacity>=100) opacity = 99.999;
  
	// IE/Win
	obj.style.filter = "alpha(opacity:" + opacity + ")";
  
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
  
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
  
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}
