/**
 * Auto Image Rotator.
 */
function init_rotator(id, timeout) {
	var ul = document.getElementById(id);
	if (ul && ul.hasChildNodes()) {
		var li = null, fnd = false;
		// set the opacity of all images to 0
		for (var i = 0; i < ul.childNodes.length; i++) {
			li = ul.childNodes[i];
			// display the first image
			if (li.tagName && li.tagName.toUpperCase() == 'LI' && li.className != 'show') {
				fnd = true; li.className = ''; setOpacity(li, 0.0);
			}
		}
		// call the rotator function to run the slideshow
		if (fnd) window.setInterval('rotate("' + id + '")', timeout);
	}
}

function rotate(id) {
	var ul = document.getElementById(id);
	// find the current displayed image
	var cr = 0, n = ul.childNodes.length;
	for (; cr < n; cr++) {
		if (ul.childNodes[cr].className == 'show') break;
	}
	if (cr < n) {
		// get next image, when it reaches the end, rotate it back to the first image
		var nx = (cr >= n - 1) ? 0 : (cr + 1);
		while (nx < n && (!ul.childNodes[nx].tagName || ul.childNodes[nx].tagName.toUpperCase() != 'LI')) {
			if (++nx >= n) nx = 0;
		}
		// hide the current image
		ul.childNodes[cr].className = '';
		ul.childNodes[nx].className = 'show';
		// set the fade in effect for the next image
		for (var i = 1; i <= 100; i++) {
			window.setTimeout('animate("' + id + '",' + cr + ',' + nx + ',' + i + ')', i * 10);
		}
	}
}

function animate(id, curr, next, i) {
	var ul = document.getElementById(id);
	setOpacity(ul.childNodes[curr], (1.0 - i / 100));
	setOpacity(ul.childNodes[next], i / 100);
}
