/********
* cvhFadeStack - A thing that rotates a stack of things with fades
* (c) 2008 Chris VandenHeuvel (fastballweb.com)
* version 1.5.2 (2008-08-20)
* requires cvhUtils and cvhDHTML (fastballweb.com/javascripts)
* Fill a relative-pos. container div with just about anything, call the function, and riggidy-roll
* fadeBoth should be set to true if any part of the slides is transparent. 
********/
function cvhFadeStack(containerId, pauseSeconds, fadeBoth, random, FPS) {
	var that = {};
	var pauseMS = pauseSeconds * 1000;
	var fadeBoth = fadeBoth || false;
	var random = random || false;
	var FPS = FPS || 24; // Set this for smoother motion / better performance
	var interval = parseInt(1000 / FPS);
	// status vars
	var holding = false;
	var opacity = 100;
	var proceedTimeout;
	var recedeTimeout;
	var container = document.getElementById(containerId);
	var rotators = [];
	for (var i = 0; i < container.childNodes.length; i++) {
		if (container.childNodes[i].nodeType == 1) {
			rotators.push(container.childNodes[i]);
		}
	}
	var fisherYates = function(input) {
	  var i = input.length;
		  if (i == 0) { return false };
		  while (--i) {
	    	var j = Math.floor(Math.random() * (i + 1));
    		var tempi = input[i];
		    var tempj = input[j];
		   	input[i] = tempj;
    		input[j] = tempi;
		}
  }
	that.stack = function() {
		opacity = 100;
		for (var i = 0; i < rotators.length; i++) {
			rotators[i].style.zIndex = rotators.length - i;
			cvhDHTML.setOpacity(rotators[i], 100);
			rotators[i].style.position = 'absolute';
			rotators[i].style.top = '0';
			rotators[i].style.left = '0';
			if (i == 0) {
				rotators[i].style.display = 'block';
			} else {
				rotators[i].style.display = 'none';
			}
		}
	}
	that.flip = function() {
		var removed = rotators.shift();
		rotators.push(removed);
		if (random) {
			removed = rotators.shift();
			fisherYates(rotators);
			rotators.unshift(removed);
		}
		that.stack();
	}
	that.proceed = function() {
		window.clearTimeout(recedeTimeout);
		if (!holding) {
			if (opacity > 0) {
				if (opacity == 100) {
					rotators[1].style.display = 'block';
				}
				opacity -= 5;
				if (opacity < 0) { opacity = 0; }
				cvhDHTML.setOpacity(rotators[0], opacity);
				if (fadeBoth) { cvhDHTML.setOpacity(rotators[1], 100 - opacity); }
				proceedTimeout = window.setTimeout(that.proceed, interval);
			} else {
				opacity = 0;
				that.flip();
				proceedTimeout = window.setTimeout(that.proceed, pauseMS);
			}
		} else {
			proceedTimeout = setTimeout(that.proceed, pauseMS);
		}
	}
	that.recede = function() {
		window.clearTimeout(proceedTimeout);
		if (opacity < 100) {
			opacity += 20;
			if (opacity > 100) { opacity = 100; }
			cvhDHTML.setOpacity(rotators[0], opacity);
			if (fadeBoth) { cvhDHTML.setOpacity(rotators[1], 100 - opacity); }
			recedeTimeout = window.setTimeout(that.recede, interval);
		} else {
			opacity = 100;
			rotators[1].style.display = 'none';
			proceedTimeout = window.setTimeout(that.proceed, pauseMS);
		}
	}
	that.hold = function(e) {
		if (cvhUtils.checkMouseMove(e, container)) {
			holding = true;
			that.recede();
		}
	}
	that.release = function(e) {
		if (cvhUtils.checkMouseMove(e, container)) {
			holding = false;
		}
	}
	if (rotators.length > 1) {
		if (!container.style.position || container.style.position == 'static') { container.style.position = 'relative'; }
		that.stack();
		container.onmouseover = that.hold;
		container.onmouseout = that.release;
		proceedTimeout = setTimeout(that.proceed, pauseMS);
	}
	return that;
}
