/* PictureChange.js
This script changes the images in a table, where the image in each 'cell' is part of one larger picture
The script changes the images in the table at random, cycling through each cell before moving on to the next picture.
The script relies on the images on the HTML page having "id=Image0", "id=Image1" etc. 
To customise the script, modify the value of the constants below 
*/

//	Constants
// Each row defines the separate images that will fill each cell to make one complete picture.
//	To add more pictures, just add a new row and change the value of the maxPictures constant.
var Image1Names = new Array("Blue1.gif","Blue2.gif","Blue3.gif","Blue4.gif","Blue5.gif","Blue6.gif","Blue7.gif","Blue8.gif","Blue9.gif");
var Image2Names = new Array("GibRondo1.gif","GibRondo2.gif","GibRondo3.gif","GibRondo4.gif","GibRondo5.gif","GibRondo6.gif","GibRondo7.gif","GibRondo8.gif","GibRondo9.gif");
var Image3Names = new Array("Green1.gif","Green2.gif","Green3.gif","Green4.gif","Green5.gif","Green6.gif","Green7.gif","Green8.gif","Green9.gif");
var Image4Names = new Array("Red1.gif","Red2.gif","Red3.gif","Red4.gif","Red5.gif","Red6.gif","Red7.gif","Red8.gif","Red9.gif");
var Image5Names = new Array("Stop1.gif","Stop2.gif","Stop3.gif","Stop4.gif","Stop5.gif","Stop6.gif","Stop7.gif","Stop8.gif","Stop9.gif");
var Image6Names = new Array("Paint1.gif","Paint2.gif","Paint3.gif","Paint4.gif","Paint5.gif","Paint6.gif","Paint7.gif","Paint8.gif","Paint9.gif");
var ImageNames = new Array(Image1Names,Image2Names,Image3Names, Image4Names, Image5Names, Image6Names);


var maxCells = 9; 		//	The number of cells in the table, if you add more cells you will need to change this value
var maxPictures = 6;	//	The number of pictures to cycle through
var interval = 900;		//	The interval between each cell change (in milliseconds ?)

//	Variables
var currentPicture;
var nextPicture;
var currentCells;
var currentCellCount;
// var currentAction;
var t;
var started=false;
var picCell;

function initialArray(){
	var IX;
	var thisArray;
	
	thisArray = new Array(maxCells-1);
	for (IX=0;IX<maxCells;IX++){
		thisArray[IX]=IX;
	}
	return thisArray;
}
function start(){
	if (started==false){
		reset();
		t=setInterval('changePicture()', interval);
		started=true;
	}
}
function stop(){
	if (started==true){
		clearInterval(t);
		started=false;
	}
}
function changePicture(){
	var cellIndex;
	var currentCell;
	
	if (currentCellCount<1)
	{
		currentCells = initialArray();
		currentCellCount = maxCells;
		currentCell=0;
		IncrementPicture();
	}
	cellIndex=Math.floor(Math.random()*currentCellCount);
	currentCell=currentCells[cellIndex];
	currentCellCount--;
	picCell = document.getElementById('Image'+currentCell);
	if (picCell == null){
	window.alert('Image'+currentCell+' not found\ncellIndex='+cellIndex+'\ncellCount='+currentCellCount+'\ncurrentCells Array='+currentCells.toString());
	stop();
	}
	else
	{
		picCell.src='http://ultrainterior.co.nz/pics/'+ImageNames[nextPicture][currentCell];
		removeCell(cellIndex);
	}
}	
function removeCell(thisCell){
	var arrayString;
	delete currentCells[thisCell];
	arrayString = currentCells.join(':');
	while (arrayString.indexOf('::')>0){
		arrayString=arrayString.replace('::',':');
	}
	if (arrayString.substr(0,1)==':'){
		arrayString=arrayString.substr(1);
		// currentAction='Removed initial :';
	}
	if (arrayString.substr(arrayString.length-1,1)==':'){
		arrayString=arrayString.substr(0,arrayString.length-1);
		// currentAction='Removed trailing :';
	}
	currentCells=arrayString.split(':');
}
function IncrementPicture(){
	currentPicture++;
	nextPicture++;
	if (currentPicture==maxPictures){
		currentPicture = 0;
	}
	if (nextPicture==maxPictures){
		nextPicture = 0;
	}
}
function reset(){
	currentCells = initialArray();
	currentCell=0;
	currentCellCount = maxCells;
	currentPicture=0;
	nextPicture=1;
	for (i=0;i<maxCells;i++){
		picCell = document.getElementById('Image'+i);
		picCell.src='pics/'+ImageNames[0][i];
	}
}


