/* headerImage and rightPane image randomization code:

	Functions in file:
		getImageCache(target)
		printImageCache()
		getRandImgs(target)
*/

/*
	Valid parameter values: 'headerImageLeft', 'headerImageRight', 'headerImageMid', 'right' (default).
	
	This function is used to identify a particular 'image cache', based on what parameter
		it's given, and associate each image with a descriptive caption.  This caption can 
		then be used for <img> alt tags or for image descriptions in a picture gallary.
	
	Returns: a nested array consisting of a two element array with another array at 
		each index.  The elements of the array at the first index are paths to image files 
		defined explicitly within this function.  The elements of the second array are 
		a set of strings which map one-to-one onto the elements of the array at the first 
		index and represent captions for each of the images identified by the first array.
		So if the return value is called "images" then images[0] contains paths in a path 
		array and images[1] contains captions in a captions array, and in turn path[0] has
		a caption at captions[0], path[1] and captions[1], and so on.
*/
function getImageCache(target) {
	var cache = new Array();
	var caption = new Array();
	var images = new Array();
	
	/*
		These images (except for those when target == 'right') are by default displayed on 
		the gallary page in the order in which they are listed here.  For ordering of 
		headerImageLeft images vs. headerImageMid and headerImageRight see the printImageCache() function
		(below).
	*/
	switch (target) {
		case 'headerImageRight':
			cache[0] = "/images/headerImageRight/university_hall_clock_tower.jpg";
			caption[0] = "University Hall Clock Tower";
			
			cache[1] = "/images/headerImageRight/university_hall.jpg";
			caption[1] = "University Hall";
			
			cache[2] = "/images/headerImageRight/university_hall_front.jpg";
			caption[2] = "University Hall Auxillary Entrance";
			
			cache[3] = "/images/headerImageRight/university_hall_front_2.jpg";
			caption[3] = "University Hall Front Entrance";
			
			cache[4] = "/images/headerImageRight/university_hall_handicap_entrance.jpg";
			caption[4] = "University Hall Handicap Entrance";
			
			cache[5] = "/images/headerImageRight/university_hall_roof.jpg";
			caption[5] = "University Hall Roof";
			
			//ensure there are enough captions for each image
			//If there aren't, create what's needed initialized to null
			if(cache.length > caption.length) {
				for(var i = caption.length; i < cache.length; i++) {
					caption[i] = "";
				}
			}
			
			var images = new Array(2);
			images[0] = cache;
			images[1] = caption;
			
			break;
		
		default:
			cache[0] = "/images/rightColumnImage/studentsDiscussingNotes.jpg";
			caption[0] = "Students discussing class notes";
			
			cache[1] = "/images/rightColumnImage/studentNotebook.jpg";
			caption[1] = "Student studying";
			
			//ensure there are enough captions for each image
			//If there aren't, create what's needed initialized to null
			if(cache.length > caption.length) {
				for(var i = caption.length; i < cache.length; i++) {
					caption[i] = "";
				}
			}
			
			var images = new Array(2);
			images[0] = cache;
			images[1] = caption;
			
			break;
	}
	
	return images;
}

/*
	This function was designed for use with a picture gallary page.  printImageCache() takes all the images
	explicitly specified in the getImageCache function under headerImageLeft, headerImageMid or headerImageRight (i.e. all
	the rotating images used in the site headerImage itself) and produces a table with the id "gallary" 
	containing as many items on each row as specified by the itemsPerRow variable (default = 3).  Each image
	has an alt tag also explicitly defined by getImageCache().
	
	Returns: nothing.  This funtion should be called as inline javascript (i.e. from within the body of an html
	document) since it will render its table to wherever it was called from in the html.
*/
function printImageCache() {
	var images = getImageCache('headerImageLeft');
	var caption = images[1];
	var cache = images[0];
	
	var images = getImageCache('headerImageMid');
	var caption = caption.concat(images[1]);
	var cache = cache.concat(images[0]);
	
	var images= getImageCache('headerImageRight');
	var caption = caption.concat(images[1]);
	var cache = cache.concat(images[0]);
		
	var itemsPerRow = 3;
	
	document.write('<table id="gallery">');
	for(var i = 0; i < cache.length/itemsPerRow; i++) { //iterate through rows (assume 3 per row)
		document.write('<tr>')
		
		for(var j = 0; j < 3; j++) {
			var iter = i*3 + j;
			if(iter < cache.length)
				document.write('<td valign="top"><img src="'+cache[iter]+'" /><br /><span>'+ caption[iter]+'</span></td>');	
		}
		
		document.write('</tr>')
	}
	document.write('</table>');
}

/*
	Parameter: there is one valid parameter choice 'headerImageRight'.
	Returns: nothing.  Instead this function prints a div to wherever it's called from in an html document.  This div 
	contains an image randomly selected from the corresponding list defined in getImageCache().  See getImageCache() (above)
	for details.
*/
function getRandImgs(target) {
	
	var cache = getImageCache(target);
	
	switch (target) {
		case 'headerImageRight':
			var cacheRight = new Array();
			cacheRight = cache[0];
			captionRight = cache[1];
			
			var p = cacheRight.length;
			var whichImage = Math.floor(Math.random()*(p-0));
		
			document.write('<div id="headerImageRandImgs"><img src="'+cacheRight[whichImage]+'" alt="'+captionRight[whichImage]+'"></div>');
			
			break;
		default: //used for generation of the right panel image
			cacheColumn = cache[0];
			captionColumn = cache[1];
			
			var p = cacheColumn.length;
			var whichImage = Math.round(Math.random()*(p-1));
			document.write('<img src="'+cacheColumn[whichImage]+'" alt="'+captionColumn[whichImage]+'"><!--<p>'+captionColumn[whichImage]+'</p>-->');	
	}
}
