// Global variables
var loops, scene_list, sets, panes, this_loop, this_set, delay;

function initalizeHeader () {

	// DOM moat: If you can't hack it, go home.
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	
	// Check: Does this page have a rewritable header?
	if (!document.getElementById("logo")) return false;
	if (!document.getElementById("loops")) return false;
	if (!document.getElementById("scene_list")) return false;
	
	// Initialize global variables
	loops = document.getElementById("loops").value;				// how many loops?
	scene_list = document.getElementById("scene_list").value;	// big ol' list of all the sets and their images
	sets = scene_list.split("/");
	this_loop = 1;
	this_set = 1; 	// first set is already drawn; start redrawing with second set
	delay = 10000;	// refresh images every 20 seconds
	
	// Preload images
	for (var x = 0; x < sets.length; x++) {				// loop over sets
		
		var panelist = sets[x].split(";");
		for (var y = 0; y < panelist.length; y++) {		// loop over panes in a set
	
			var imglist = panelist[y].split(",");
			for (var z = 0; z < imglist.length; z++) {	// loop over possible images in a pane
				var imgpath = "images/" + imglist[z];
				var img = document.createElement("img");
				img.setAttribute("src", imgpath);
			}
		}
	}
	
	var logo_table = document.getElementById("logo");
	panes = logo_table.getElementsByTagName("img");		
	
	// Start redrawing!
	setTimeout('drawSet()', delay);
}

function drawSet() {

	// Redraw images
	var imagelist = sets[this_set].split(";");
	
	for (var i=0; i < panes.length; i++) {
	
		// Get this pane's image (just the one image, or a random one from its list if more than one img listed)
		var image_src = "";
		if (imagelist[i].match(/,/)) {
			image_src = "images/" + randValue(imagelist[i], ",");
		} else {
			image_src = "images/" + imagelist[i];
		}
		
		var counter = i+1;
		var pane_id = "pane_" + counter;
		
		// Fade out, replace with the new image, then fade back in		
		// Call function swapfade (image-object, 'new src', 'seconds'[, 'new alt text'])			
		swapfade(panes[i], image_src, '5', '', i);
	}
	
	// Continue looping if sets or loops remaining
	if (this_set < sets.length -1) {	
		this_set++;	
		setTimeout('drawSet()', delay);
	} else if (this_loop <= loops) {
		this_loop++;
		this_set = 0;
		setTimeout('drawSet()', delay);
	}
	
	return null;
}

//=====================================================

// Given an array represented as a list with a specified delimiter, return a random value from the array
function randValue(arraystring, separator) {
	var this_array = arraystring.split(separator);
	var rand = Math.floor(Math.random()*this_array.length);	
	return this_array[rand];
}
//=====================================================

// Run when the page loads
addLoadEvent(initalizeHeader);

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
