/*

For an splash page put the following line inside the BRC area
<script type="text/javascript" src="./ssi/heisplash.js"></script>

LIMITS:
Number of images: 4 per image

The images must be named properly
print1.jpg, print2.jpg, print3.jpg etc

*/

// this is same as putting <body onload="init()"> in doc
window.onload = init;

var imgobjs = new Array(8);
var imgids = ["ceramic", "charcoal", "drawing", "painting", "pastel", 
			"print", "sculpture", "watercolor"];
var imgndx = [ 1, 1, 1, 1, 1, 1, 1, 1];

var imgtodo = new Array(8);	// this is used as part of mechanism to guarantee all get switched

var picprefix = "splash-";

var picdir = null;

var timeID = 0;

var MAXIMAGES = 5;		// 5 images print1, print2, print3
var initialDelay = 5000;	// 5 second initial delay
var firstTime = true;
var minInterval = 1000;		// 1 second min delay between image switch
var maxInterval = 3000;		// 3 second max delay between image switch
var doContinous = false;	// Just run through the images once
function init() {
	for (var i = 0; i < imgids.length ; i++) {
		imgobjs[i] = document.getElementById(imgids[i]+"img");
	}

	resetImgTodo();

	picdir = grabDir(imgobjs[0].src);
	startSwitch();
}

function resetImgTodo() {
	for (var i = 0; i < imgids.length ; i++) {
		imgtodo[i] = i;
	}
}

function startSwitch() {
	var thisInterval = randInt(minInterval, maxInterval);
	if (firstTime) {
		thisInterval += initialDelay;
		firstTime = false;
	}
	timeID = setInterval("switchImage()", thisInterval);
}

function switchImage() {
	// clear interval
	clearInterval(timeID);
	timeID = 0;

	// if imgtodo array is depleted reset
	if (imgtodo.length == 0) {

		// see if reached end of graphic chains (assumed equal)
		if (imgndx[0] >= MAXIMAGES) {
			if (doContinous) {
		
				for (var i = 0; i < imgids.length ; i++) {
					imgndx[i] = 0;	// set to 0 so we repaint the first image set too.
				}
				resetImgTodo();
			} else {
				return;
			}
		} else {
			resetImgTodo();
		}
	}

	var randndx;
	if (imgtodo.length == 1) {
		randndx = 0;
	} else {
		randndx = randInt(0, imgtodo.length-1);
	}
	
	// alert("randndx "+randndx);

	var thistodo = imgtodo.splice(randndx, 1);

	var nextimagendx = imgndx[thistodo] + 1;

	if (nextimagendx > MAXIMAGES) {
		if (doContinous) {
			nextimagendx = 1;
		}
	}
//	imgobjs[1].src = picdir + "charcoal2.jpg";
//	alert(picdir + imgids[thistodo] + nextimagendx + ".jpg");
	imgndx[thistodo] = nextimagendx;
	imgobjs[thistodo].src = picdir + picprefix + imgids[thistodo] + nextimagendx + ".jpg";
	startSwitch();
}

function randInt(lower, upper) {
        var range = upper - lower;
        var size = range + 1;
        return Math.floor( (Math.random() * size) + lower );
}

function grabDir(instring) {
	var tmp;
	var ndx = instring.lastIndexOf("/");
	if (ndx == -1) {
		return "";
	}
	return instring.substring(0,ndx+1);
}

