/*** 
 * imageswapper.js: Javascript utility for swapping and pre-loading images in loops.
 *
 * Copyright (C) 2007 Almer S. Tigelaar <almer at tigelaar dot net>
 *
 * This code (the parts not supplied by Dynamic Drive) can be distributed
 * under the terms of the GNU General Public License (GPL) version 2 (or higher).
 * See the GNU homepage at http://www.gnu.org/licenses/ for details about the
 * license and terms of usage.
 */	
	 
var chainArray = new Array();        // Array of chains (with filenames of images)
var chainLinks = new Array(); 		 // Array of chains (with links)

var chainOffsets = new Array();      // Current offset in each array
var chainTimers = new Array();       // Initial values for the timers
var chainTimeCounters = new Array(); // Array with the current count value
var chainTimeTargets = new Array();  // Target images to update when timers expire
var chainLinkTargets = new Array();  // Target links to change (anchor tags)


/* Creates a new chain and returns the identifier associated with it */
function makeChain() {
	chainArray.push(new Array());
	chainLinks.push(new Array());
	chainOffsets.push(0);
	chainTimers.push(0);
	chainTimeCounters.push(0);
	chainTimeTargets.push(0);
	chainLinkTargets.push(0);
	return chainArray.length - 1;
}

/* Preloads an image file in a chain */
function preloadImage(chain, filename, linkto) {
	/* Preload image */
	//var newImage = new Image();
	//newImage.src = filename;
	
	/* Register image in array */
	var imageArray = chainArray[chain];
	imageArray.push(filename);
	
	var linkArray = chainLinks[chain];
	linkArray.push(linkto);
}

/* Advances the image in a chain */
function nextImage(imageName, linkTargetName, sourceChain) {
	/* Increase retrieval offset */
	var newOffset = chainOffsets[sourceChain] + 1; 
	
	/* Sanity check, can't call next on an empty chain */
	if (chainArray[sourceChain].length == 0) { 
		throw "Chain is empty!";
	}
	
	/* Cycle back to beginning if needed */
	if (newOffset >= chainArray[sourceChain].length) { 
		newOffset = 0;
	}

	/* Set document element to the item in the chain */
	document.getElementById(imageName).src = (chainArray[sourceChain])[newOffset];
	/* Links */
	if (linkTargetName != null) {
		if ((chainLinks[sourceChain])[newOffset] != null) {
			document.getElementById(linkTargetName).href = (chainLinks[sourceChain])[newOffset];
			document.getElementById(linkTargetName).target = "_new";
		} else {
			document.getElementById(linkTargetName).href = "index.php?page=welcome"; // Hack link back to the index page.
			document.getElementById(linkTargetName).target = "_self";
		}
	}
	
	/* Write back the offset used */
	chainOffsets[sourceChain] = newOffset;
}

/* Callback. This examines the chains, see if any counters are set
 * and advanced the counters if needed
 */
function timerCallback()
{
	for (i = 0; i < chainTimers.length; i++) {
		if (chainTimers[i] > 0) { /* A time-out has been set on this chain */
			chainTimeCounters[i] -= 10;
			if (chainTimeCounters[i] < 0) {
				chainTimeCounters[i] = chainTimers[i]; /* Re-init */
				nextImage(chainTimeTargets[i], chainLinkTargets[i], i); /* Cycle to next image */
			}
		}
	}
	timerID = self.setTimeout("timerCallback()", 10000); /* Callback once every ten seconds */
}

var timerID = null;

/* Sets a chain to auto-cycle. Note: refreshTime should be a multiple of 10.
   To disable cycling set the chain refreshTime back to zero */
function setChainCycleTimer(chain, refreshTime, target, linktarget)
{
	chainTimeTargets[chain] = target;
	chainLinkTargets[chain] = linktarget;
	chainTimers[chain] = refreshTime; /* Set chain time */
	
	/* Reset the Timer */
	if (timerID != null) {
		clearTimeout(timerID);
	}
	timerID = self.setTimeout("timerCallback()", 10000); /* Callback once every ten seconds */
}


