/* Copyright Claude Needham gxxaxx@gmail.com All rights reserved
	Please refrain from using this script without permission.
	This is how I make my living. 
	If you would like to use the script contact me. We can work something out. 
	You can get the javascript without stealing it (I'll keep the price low).
	And I can live in a house instead of a station wagon.
*/

/*
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: 5 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;

// 2 by 4 version
//var imgobjs = new Array(8);
//var imgids = ["r1c1i", "r1c2i", "r1c3i", "r1c4i", "r2c1i", "r2c2i", "r2c3i", "r2c4i"];
//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

// 2 by 2 version
var imgobjs = new Array(4);
var imgids = ["r1c1i", "r1c2i", "r2c1i", "r2c2i"];
var imgndx = [ 1, 1, 1, 1 ];
var imgtodo = new Array(4);	// 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 = 3000;	// 3 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);
	var badids = 0;
	var whichbad = "";
	for (var i = 0; i < imgids.length ; i++) {
		if (!imgobjs[i]) {
			badids++;
			whichbad += imgids[i]+"img, "
		}
	}
	if (badids == 0) {
		startSwitch();
	} else {
//		alert(badids + " malformed ids. "+whichbad);
	}
}

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);
}


