Extension:Javascript Slideshow/slideshow.js

From mediawiki.org
var slideshowDivs = [];
var currentDivIndexes = [];

function getChildDivs(id) {
	var parent = document.getElementById(id);
	var childDivs = [];
	var childDivCount = 0;
	var i;
	var maxHeight = 0;
	var maxWidth = 0;
	for (i=0; i < parent.childNodes.length; i++) {
		var child = parent.childNodes[i];
		if (child.tagName == 'DIV') {
			childDivs[childDivCount++] = child;
			if (maxHeight < child.offsetHeight) {
				maxHeight = child.offsetHeight
			}
			if (maxWidth < child.offsetWidth) {
				maxWidth = child.offsetWidth
			}
			child.style.position = 'absolute';
			child.style.display = 'none';
		}
	}

	parent.style.height = maxHeight + 'px';
	parent.style.width = maxWidth + 'px';
	for (i=0; i < childDivs.length; i++) {
		childDivs[i].style.height = parent.style.height;
		childDivs[i].style.width = parent.style.height;
	}

	
	return childDivs;
}

function getInitialDivIndex(id, sequence) {
	var index = -1;
	if (sequence == 'forward') {
	  index = 0;
	} else if (sequence == 'backward') {
	  index = (slideshowDivs[id].length)-1;
	} else if (sequence == 'random') {
		index = Math.floor(Math.random()*slideshowDivs[id].length);
	}
	slideshowDivs[id][index].style.display = '';
	return index;
}

function getNextDivIndex(id, sequence) {
	var index = -1;
	if (sequence == 'forward') {
		index = currentDivIndexes[id] + 1;
		if (index == slideshowDivs[id].length) {
			index = 0;
		}
	} else if (sequence == 'backward') {
		index = currentDivIndexes[id] - 1;
		if (index == -1) {
			index = slideshowDivs[id].length - 1;
		}
	} else if (sequence == 'random') {
		index = currentDivIndexes[id];
		while (index == currentDivIndexes[id]) {
			index = Math.floor(Math.random()*slideshowDivs[id].length);
		}
	}

	return index;
}

function getNode(id, index) {
	return slideshowDivs[id][index];
}

function doTransition(currentNode, newNode, transition) {
	if (transition == 'cut') {
		currentNode.style.display = 'none';
		newNode.style.display = '';
	} else if (transition == 'fade') {
		currentNode.style.zIndex = 2;
		newNode.style.zIndex = 1;
		newNode.style.display = '';
		newNode.style.opacity = 0;
		new Effect.Opacity(currentNode, {from: 1.0, to: 0.0, duration: 1.0});
		new Effect.Opacity(newNode, {from: 0.0, to: 1.0, duration: 1.0});
	} else if (transition == 'blindDown') {
		currentNode.style.zIndex = 1;
		newNode.style.zIndex = 2;
		new Effect.BlindDown(newNode, {duration: 1.0});
	}
}

function slideshow(id, sequence, transition) {
	var newIndex = getNextDivIndex(id, sequence);
	doTransition(getNode(id, currentDivIndexes[id]), getNode(id, newIndex), transition);
	currentDivIndexes[id] = newIndex;
}

function startSlideshow(id, refresh, sequence, transition) {
	slideshowDivs[id] = getChildDivs(id);
	currentDivIndexes[id] = getInitialDivIndex(id, sequence);
	setInterval("slideshow('" + id + "', '" + sequence + "', '" + transition + "');", refresh);
}