

if (!String.prototype.lpad) {
	String.prototype.lpad = function(pad_string, pad_length) {
		var input = this;
		while (input.length < pad_length) {
			input = pad_string + input;
		}
		return input;
	};
}


if (!Array.prototype.shuffle) {
	Array.prototype.shuffle = function() {
		var array_len = this.length, array_ptr, temp;
		if (array_len == 0) {
			return
		}
		while (--array_len) {
			array_ptr = Math.floor(Math.random() * (array_len + 1));
			temp = this[array_len];
      this[array_len] = this[array_ptr];
      this[array_ptr] = temp;
		}
	};
}


// Variables
var photo1, photo2, photo_data1 = "", photo_data2 = "";
var rotation_seconds = 4;
var total_photos = 158;

var photo;
var photos_array = new Array();

// Populate array
for (photo = 0; photo < total_photos; photo++)
{
	photos_array[photo] = '/images/headers/' + (photo + 1).toString().lpad('0', 3) + '.jpg';
}

// Shuffle array
photos_array.shuffle();

photo1 = 0; 

if((photo2 + 1) < total_photos)
	photo2 = photo2 + 1;
else
	photo2 = 0;

// Store photo1 in memory
photo_data1 = new Image;
photo_data1.src = photos_array[photo1];

// Store photo2 in memory
photo_data2 = new Image;
photo_data2.src = photos_array[photo2];

// Initial call on function show images
window.onload = show_images;



function show_images()
{
	// Display photo stored in photo_data1
	document.getElementById("Photo").src = photo_data1.src;
	
	// Replace photo_data1 with photo_data2
	photo_data1.src = photo_data2.src;

	// Calculate next index for photo1
	if((photo1 + 1) < total_photos)
		photo1 = photo1 + 1;
	else
		photo1 = 0;
	
	// Calculate next index for photo2
	if((photo2 + 1) < total_photos)
		photo2 = photo2 + 1;
	else
		photo2 = 0;
		
	// Store new photo_data2 in memory
	photo_data2.src = photos_array[photo2];
	
	// Recursive call on cycle_header_photos
	setTimeout("show_images()", rotation_seconds * 1000);
}
