/* slideshow of images
Parameters:
  myName: instance name, used with setTimeout
  imagesArray: array with image src's
  elem_id: img element id
  fadeTime: time of fade duration (default 1000ms)
  cycleDelay: slide duration before next fade (default 3000ms)

Usage:
var my_ss = new SlideShow("my_ss", new Array() {"img1.jpg", "img2.jpg"}, "venue_image");
my_ss.run();
my_ss.pickImage(1); 
-OR-
window["venue_ss"] = new SlideShow("venue_ss", images, "SlideShowBig");window["venue_ss"].run();
*/

function SlideShow(myName, imagesArray, elem_id, fadeTime, cycleDelay) {
	// locals
	var i;

	// attributes
	this.myName = myName;
	if (typeof imagesArray != 'undefined') {
		this.images = imagesArray;
	}
	
	// delays
	if ((typeof fadeTime == 'undefined') || (fadeTime == 0)) fadeTime = 1000;
	if ((typeof cycleDelay == 'undefined') || (cycleDelay == 0)) cycleDelay = 3000;
	this.fadeInterval = 100;
	var fadeLength = parseInt(fadeTime / this.fadeInterval);
	this.fadeOpacityInterval = (fadeLength == 0 ? 100 : (100 / fadeLength));
	this.cycleDelay = cycleDelay;
		
	this.curIndex = 0;
	//this.crossFadeDuration = 1;
	this.elem_id = elem_id;
	//this.bLoop = true;
	this.current_cycle_id = 0;

	// preload
	this.preLoad = new Array();
	var p = this.images.length; 
	for (i = 0; i < p; i++) {
		this.preLoad[i] = new Image();
		this.preLoad[i].src = this.images[i];
	}
	
	this.getImageElem = function () {
		return document.getElementById(this.elem_id);
	}
	
	this.setOpacity = function (opacity) {
		elem = this.getImageElem();
		opacity = (opacity == 100)?99.9999:opacity;
		
		// IE/Win
		elem.style.filter = "alpha(opacity:"+opacity+")";
  
		// Safari<1.2, Konqueror
		elem.style.KHTMLOpacity = opacity/100;
  
		// Older Mozilla and Firefox
		elem.style.MozOpacity = opacity/100;
  
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		elem.style.opacity = opacity/100;
	}

	this.fadeIn = function (opacity, depth, cycle_id) {
		if ((cycle_id == this.current_cycle_id) && (document.getElementById)) {
			if (depth < -1) {
				alert('fatal error: invalid depth in SlideShow.fadeOut');
			} else if (depth == 0) {
				// do nothing
			} else {
				if (opacity < 100) {
					this.setOpacity(opacity);
					opacity += this.fadeOpacityInterval;
					window.setTimeout(this.myName + ".fadeIn("+opacity+"," + depth + "," + cycle_id + ")", this.fadeInterval);
				} else {
					// opacity is full, queue next fade with reduced depth
					if (depth > 0) depth -= 1;
					window.setTimeout(this.myName + ".fadeOut(" + opacity + "," + depth + "," + cycle_id + ")", this.cycleDelay);
				}
			}
		}
	}

	this.fadeOut = function (opacity, depth, cycle_id) {
		if (cycle_id == this.current_cycle_id) {
			if (depth < -1) {
				alert('fatal error: invalid depth in SlideShow.fadeOut');
			} else if (depth == 0) {
				// do nothing
			} else {
				if (opacity > 0) {
					opacity -= this.fadeOpacityInterval;
					this.setOpacity(opacity);
					window.setTimeout(myName + ".fadeOut("+opacity+"," + depth + "," + cycle_id + ")", this.fadeInterval);
				} else {
					this.curIndex += 1;
					if (this.curIndex >= this.images.length) {
						this.curIndex = 0;
					}
					var elem = this.getImageElem();
					elem.src = this.preLoad[this.curIndex].src;
					
					if (depth > 0) depth -= 1;
					this.fadeIn(0, depth, cycle_id);
				}
			}
		}
	}
	
	this.run = function () {
		this.current_cycle_id += 1;
		
		// start slideshow
		this.curIndex = 0;
		var elem = this.getImageElem();
		elem.src = this.preLoad[this.curIndex].src;
		
		this.setOpacity(elem, 0);
		elem.style.visibility = 'visible';
		this.fadeIn(0, -1, this.current_cycle_id);
	}
	
	this.pickImage = function (i) {
		this.current_cycle_id += 1;
		
		// fade from invisible
		var elem = this.getImageElem();
		elem.src = this.preLoad[i].src;
		
		this.setOpacity(elem, 100);
		elem.style.visibility = 'visible';
		this.fadeIn(0, 1, this.current_cycle_id);
	}
}

