/*
   This file will load an array of photos from a list of members (with captions) and insert this
   into a document at the end of a menu.
   
   To update, add or edit the imagebank array as well as its corrisponding captionbank array. Be
   sure that the image exists in the directory "_images/portraits/". Also match a caption to the image.
   
   The photos must be a maximum of 150px wide.
   
   Use this script insert: <script type="text/javascript" src="_includes/random_member.js"></script> in
   the "<!-- Put JavaScripts Here -->" section of the template after the <script type="text/javascript"
   src="_includes/loader.js"></script>. Images will appear after the unordered list with the id="menu"
   attribute.
*/

/*
   randomImage assigns a random starting image to the place holder image. 
   This sets global variables startimgurl and startimgcapt
*/
function randomImage() {
	var imagebank = Array(
	"agrisettinail.jpg",
	"alemez.jpg",
	"aross.jpg",
	"AWeiss.jpg",
	"BSmelser.jpg",
	"BSmoker.jpg",
	"CConners.jpg",
	"DNorth.jpg",
	"ENebblett.jpg",
	"eriedel.jpg",
	"FCrespin.jpg",
	"IWard.jpg",
	"jbigney.jpg",
	"JDelgado.jpg",
	"JTrott.jpg",
	"KThomas.jpg",
	"lgottlieb.jpg",
	"lvallspinosa.jpg",
	"MBeatty.jpg",
	"MBunker.jpg",
	"PAnllo.jpg",
	"rfranko.jpg",
	"soshea.jpg",
	"ssunde.jpg",
	"thickey.jpg"
	);
	var captionbank = Array(
	"April Grisetti-Nail, Espanola",
	"Alma Lemez, Chaparral",
	"Anna Ross, Albuquerque",
	"Amy Weiss, Albuquerque",
	"Bindu Smelser, Espanola",
	"Bret Smoker, Santa Fe",
	"Connie Conners, Albuquerque",
	"Douglas North and Azul, Chama",
	"Edwin Nebblett, Reserve",
	"Elizabeth Riedel, Embudo",
	"Frank Crespin, Las Cruces",
	"Isa Ward, Bernalillo",
	"Jessica Bigney, Albuquerque",
	"James Delgado, Pojoaque",
	"Justina Trott, Santa Fe",
	"Kristy Thomas, Columbus",
	"Laura Gottlieb, Albuquerque",
	"Leigh Vall-Spinosa, Albuquerque",
	"Marcia Beatty, Taos",
	"Mary Bunker, Mora",
	"Pilar Anllo, Santa Fe",
	"Ruben Franco, Roswell",
	"Sheryl O’Shea, Albuquerque",
	"Scott Sunde, Albuquerque",
	"Thomas Hickey, Silver City"
	);
	
	var imageindex = Math.floor(Math.random() * imagebank.length);
	startimgurl = "/rios/_images/portraits/" + imagebank[imageindex];
	startimgcapt = captionbank[imageindex];
}

/*
   adds a space for the image and description text
*/
function preparePlaceholder() {
	var placeholder = document.createElement("img");
	placeholder.setAttribute("id","placeholder");
	var startimage = randomImage();
	placeholder.setAttribute("src",startimgurl);
	placeholder.setAttribute("alt","My Image Gallery");
	var description = document.createElement("p");
	description.setAttribute("id","description");
	var desctext = document.createTextNode(startimgcapt);
	description.appendChild(desctext);
	var gallery = document.getElementById("menu");
	insertAfter(placeholder,gallery);
	insertAfter(description,placeholder);
}

/*
   insertAfter Funciton which is not provided for in the DOM
*/
function insertAfter(newElement,targetElement){
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

addLoadEvent(randomImage);
addLoadEvent(preparePlaceholder);