/*

name: galleryImages.js
author: Andrew Cullen
notes: Used to replace oodomimages.js, a rollover script that was incompatible with selecting an image for the main area
*/

//lastclicked holds the id attribute of the currently highlighted thumbnail
var lastclicked = '';

function highlightImage(img) {
	insertOver(img);
}

function lowlightImage(img) {
	if (img.id!=lastclicked)
		stripOver(img);
}

//select an image and highlight the selected thumbnail
function selectImage(img) {
	//determine if selected thumbnail was already selected
	if (img.id==lastclicked) {
	
		return false;
	}	
	
	//the image tag for the main image on the page
	var mainTarget = document.getElementById("ShellTarget4_image");	
		
		
	mainTarget.src = determineMainImage(img);
	
	
	var oldlastclicked = "";
	//the previously selected thumbnail
	if (lastclicked.length>0) {
		oldlastclicked=lastclicked;
		
	}
	
	//add highlighting to current thumbnail
	highlightImage(img);
	
	//set this image as the last one clicked
	lastclicked=img.id;
	
	//remove the highlighting from the previously selected thumbnail
	if (oldlastclicked.length>0) {
		var lastThumb = document.getElementById(oldlastclicked);
		stripOver(lastThumb);
		
	}
					
}								

//add the relevant event handlers
function addThumbEvents() {

	var gallery = document.getElementById("ShellTarget1");
	
	var thumbs = gallery.childNodes;
	
	//have we highlighted the first image in the gallery yet?
	var firstImageHighlighted = false;
	for (var i=0;i<thumbs.length;i++) {
		
		//if the current child node is an image, and its course doesn't contain "spacer.gif", add the events
		if (thumbs[i].tagName=="IMG" && thumbs[i].src.indexOf("spacer.gif")==-1) {
				
				thumbs[i].onmouseover=function() {highlightImage(this);}
				thumbs[i].onmouseout=function() {lowlightImage(this);}
				thumbs[i].onclick=function() {selectImage(this);}
				
				if (!firstImageHighlighted) {
					selectImage(thumbs[i]);
					firstImageHighlighted=true;		
				}
			
		}
	}
}

//add an onload event to run addThumbEvents
if(window.addEventListener){window.addEventListener("load", addThumbEvents, false);} 
else{
	if(window.attachEvent){window.attachEvent("onload", addThumbEvents);}
	else{if(document.getElementById){window.onload=addThumbEvents;}}
}						

//remove the string "_over" from an image's src attribute
function stripOver(img) {
	var newsrc = img.src.replace("_over.",".");
	img.src = newsrc;
	
}
//insert the string "_over" into an image's src attribute, just before the file extension
function insertOver(img) {
	//make sure the over string isn't already in the src
	if (img.src.indexOf("_over.")==-1) {
		var newsrc = img.src.replace(".jpg","_over.jpg").replace(".gif","_over.gif");
		img.src = newsrc;
	}
	
}

//return the main image src attribute based on the thumbnail's src attribute
function determineMainImage(img) {
	var src = img.src.replace("_over.",".");
	
	src = src.replace("thumbs/","");
	return src;
}