
// rotate images with this class
var rotate_class = "rotateimgs";

// min/max delay between rotates (in milliseconds)
var min_rotate_delay = 500;
var max_rotate_delay = 800;

// if false, don't set timeout for the rotate() function
var auto_rotate = true;

<br />
<b>Warning</b>:  opendir(covers/) [<a href='function.opendir'>function.opendir</a>]: failed to open dir: No such file or directory in <b>/Library/WebServer/Sites/digitorial.co.uk/js/rotate.js.php</b> on line <b>34</b><br />
var images = new Array();


// internal globals:
var in_use = new Array();
var last_changed = -1;
var rotate_images = new Array();


// --------------------------------------------------
// place the images  -- we could do this randomly

function initRotate()
{
	// ----------------
	// Find all images of the specified class and populate the rotate_images array
	rotate_images = new Array();

	for(var i=0; i < document.images.length; i++)
	{
		if(document.images[i].className == rotate_class)
			rotate_images.push(document.images[i].id);
	}
	

	// ----------------
	// set the initial images
	var j = 0;
	for(var i in rotate_images)
	{
		var e = document.getElementById(rotate_images[i]);
		
		e.src = images[j];
		in_use[i] = j;
		
		j++;
		if(j == images.length)
			j = 0;
	}
	
	// ----------------
	// start rotating images...
	if(auto_rotate)
		window.setTimeout("rotate()", rand(min_rotate_delay, max_rotate_delay));
}



// --------------------------------------------------
// randomly change an image to one that's not in use

function rotate()
{
	// Find which (if any) images are not in use and populate available_images[]
	var available_images = new Array();
	var available;
	for(var i in images)
	{
		available = true;
		for(j in in_use)
		{
			if(in_use[j] == i)
				available = false;
		}
		
		if(available)
			available_images.push(i);
	}

	if(available_images.length < 1)
		return;

	// which image are we going to rotate? - Don't change the same one twice in a row
	var rotate_image = last_changed;
	while(rotate_image == last_changed)
		rotate_image = Math.round(Math.random() * (rotate_images.length - 1));
	
	last_changed = rotate_image;

	// pick a random unused image
	var new_image;
	if(available_images.length < 2)
		new_image = available_images[0];
	else
		new_image = available_images[Math.round(Math.random() * (available_images.length - 1))];
	
	// ----------------------------------------------------
	// put the newly-selected image on the page and set the in_use value

	var e = document.getElementById(rotate_images[rotate_image]);
	
	e.src = images[new_image];
	in_use[rotate_image] = new_image;

	if(auto_rotate)
		window.setTimeout("rotate()", rand(min_rotate_delay, max_rotate_delay));
}



// --------------------------------------------------
// return a random integer between min and max 
// (min and max must be integers >= 0)

function rand(min, max)
{
	if(min == max)
		return min;
	
	var diff = max - min;
	var n = Math.round(Math.random() * diff) + min;

	return n;
}
