/*
 This script allows for any number of images to be rotated at random
 with each image having its own list of images to be randomly selected.
 One image is selected at random within a certain interval (2-4 seconds
 by default).  -Steven Chu 1/30/07
 
 -To apply rotating pictures to the page, you will need to tell the
  script which <img> you would like to rotate and the images.
  Below is an example of the code that goes in <head> of page.
  It would probably be best to copy the example so nothing is
  accidently left out (like the extra init loop after imageElements).

 -Include the rotate.js
 
 -Add id attribute to <img> tags that you wish to rotate
 
 -Assign each imageElement with corresponding id from <img> tag
 
 -Assign images and corresponding alt tags for each <img>

 -Place the line: <script language="javascript">init();</script>
  before closing </body> tag
 
 ------------------------ Begin <head> Code ------------------------
 
 <script type="text/javascript" src="/KSU_resources/js/rotate.js"></script>
 <script language="javascript">
 function init() {
    // delay in seconds (optional if want to overwrite default of 2-4 sec
    min = 1; // minimum time before rotating an image
    max = 5; // maximum time before rotating an image
 
    // <image> needs id attribute
    // *note: starts at index 0
    imageElements[0] = document.getElementById("banner1");
    imageElements[1] = document.getElementById("banner2");
    imageElements[2] = document.getElementById("banner3");
 
    // **More initialization that is required after imageElements
    for(i = 0; i < imageElements.length; i++) {
       images[i] = new Array();
       alttag[i] = new Array();
    }
 
    // images stored as: images[element# (from above)][picture #]
    // alt tags stored as: alttag[element# (from above)][picture #]
    // *note: starts at index 0
    images[0][0] = "/KSU_resources/page-art/510-it-student-library.jpg";
    alttag[0][0] = "Student studying in library";

    images[0][1] = "/KSU_resources/page-art/510-it-tech-class-3.jpg";
    alttag[0][1] = "IT Technical classroom";

    images[0][2] = "/KSU_resources/page-art/510-it-tech-class.jpg";
    alttag[0][2] = "IT Technical classroom";
 
    images[1][0] = "/KSU_resources/page-art/it-cell-phone.jpg";
    images[1][1] = "/KSU_resources/page-art/it-students-outdoors.jpg";
 
    images[2][0] = "/KSU_resources/page-art/it-cell-phone.jpg";
    images[2][1] = "/KSU_resources/page-art/it-students-outdoors.jpg";
 
    window.setTimeout('cycle();',delay());
 }
 </script>

 ------------------------- End <head> Code -------------------------
*/

// Default values and initialization of arrays
imageElements = new Array();
images = new Array(); // Array of images for each element
alttag = new Array(); // Array of alt tags for corresponding images
lastChanged = 0;
lastImage = new Array();
min = 2; // Default minimum delay
max = 4; // Default maximum delay
src = "";
alt = "";

function cycle() {
	// Randomly select image element to change
	n = Math.floor(Math.random()*imageElements.length);

	// if image is selected twice in a row
	if (lastChanged == n)
	{
		// increment to next image element
		if (n == imageElements.length-1) n = 0;
		else n++;
	}
	lastChanged = n;

	getImage(n);
	imageElements[n].src = src;
	imageElements[n].alt = alt;
	window.setTimeout('cycle();',delay());
}

function getImage(n) {
	m = Math.floor(Math.random()*images[n].length);
	if (lastImage[n] == m)
	{
		// increment to next image element
		if (m == images[n].length-1) m = 0;
		else m++;
	}
	lastImage[n] = m;
	src = images[n][m];
	alt = alttag[n][m] ? alttag[n][m] : "Rotated image";
}

function delay() { return (Math.floor(Math.random()*(max-min))+min)*1000; }
