// Script settings
image_tag = 'center-body-photo'	//HTML tag of image to swap
photo_count = 43;				//Count of photos beginning with 1
photo_basepath = "images/body/";			//Folder containing photos
photo_prefix = "_";	//First part of photo name
photo_suffix = ".jpg";			//Suffix of photo signifying file type
photo_digits = 2;				//Number of digits in photo number scheme
swap_time = 1800;				//Milliseconds before changing photo
wait_time = 250;				//Milliseconds to wait for image to finish preloading
max_swaps = 20;					//Stop swapping images after this many times

// Program variables
last_fullpath = null;
count_swaps = 0;

// Initialize image preloader
myImage = new Image();
myImage.onload = function() { this.mycomplete = true; }
myImage.mycomplete = false;

// Choose a new image and preload it
function pickimage() {
	
	// Pick a new image (not the currently displayed one)
	do {
		
		// Pick a random image index
		rindex = (Math.round(Math.random()*(photo_count-1))+1);
		prefixlen = photo_digits - String(rindex).length;
		prefix = "";
		for (a=0; a<prefixlen; a++) {
			prefix += "0";
		}
		
		// Construct the image filename
		filename = photo_prefix + prefix + String(rindex) + photo_suffix;
		fullpath = photo_basepath + filename;
		
		// Check if image is already displayed
		displayed = false;
		if (fullpath == last_fullpath) displayed = true;
		
	} while (displayed == true);
	
	// Preload the chosen image
	myImage.src = fullpath;
	last_fullpath = fullpath;
	
	// Now display the new image
	timeout = swap_time;
	if (count_swaps < 1) timeout = 1;
	setTimeout("displayimage()", timeout);
}

// Display an image once it has been preloaded
function displayimage() {
	
	// Show a preloaded image
	if (myImage.mycomplete) {
		
		// Find the image to display
		img = document.getElementById(image_tag);
		
		// Display the image
		img.src = myImage.src;
		img.style.visibility = 'visible';
		
		// Preload next image
		count_swaps++;
		myImage.mycomplete = false;
		if (count_swaps < max_swaps) setTimeout("pickimage()", 1);
	}
	
	// Wait for the image to be preloaded
	else {
		setTimeout("displayimage()", wait_time);
	}
}


