function swapImage(linkToImage, linkDescription) {
	if (linkToImage!=null) clearTimeout(rotation);
	if (!document.getElementById("imageContainer")) setPlaceholder();
	var imageContainer = document.getElementById("imageContainer");
	if (!document.getElementById("galleryList")) return false;
	var galleryList = document.getElementById("galleryList");
	var aGalleryList = galleryList.getElementsByTagName("a");
// IN CASE NO IMG URL IS SPECIFIED, GET THE FIRST ONE
	if (linkToImage==null && aGalleryList.length!=-1) {
		linkToImage = aGalleryList[0].getAttribute("href");
		if (aGalleryList[0].firstChild) {
			var linkDescription = aGalleryList[0].firstChild.getAttribute("alt");
		}
	}
// CREATE/REPLACE IMAGE
	if (imageContainer.firstChild) {
		imageContainer.firstChild.src=linkToImage;
	} else {
		var newImage = document.createElement("img");
		newImage.src=linkToImage;
		imageContainer.appendChild(newImage);
	}
// REMOVE PREVIOUS CAPTION
	if (imageContainer.lastChild.nodeName != "IMG") {
		imageContainer.removeChild(imageContainer.lastChild); // caption
		imageContainer.removeChild(imageContainer.lastChild); // br tag
	}
// ADD CAPTION
	if (linkDescription!=null && linkDescription!="" && linkDescription!="null") {
		var brTag = document.createElement("br");
		imageContainer.appendChild(brTag);
		var descriptionText = document.createTextNode(linkDescription);
		imageContainer.appendChild(descriptionText);
	}
//
	for (i=0; i<aGalleryList.length; i++) {
		var thisLink = aGalleryList[i];
		var linkHref = thisLink.getAttribute("href")
		if (linkHref==linkToImage) {
			if (i++<aGalleryList.length-1) {
				var nextImgNumber = i;
			} else {
				var nextImgNumber = 0;
			}
			linkToNextImage = aGalleryList[nextImgNumber].getAttribute("href");
			if (aGalleryList[nextImgNumber].firstChild) {
				var nextLinkDescription = aGalleryList[nextImgNumber].firstChild.getAttribute("alt");
			}
			rotation = setTimeout("swapImage('" + linkToNextImage + "',\"" + nextLinkDescription + "\")",3000);
		}			
	}
}

// ##### RUN ONLOAD #####
function setPlaceholder() {
	if (!document.getElementById("galleryList")) return false;
 	var galleryList = document.getElementById("galleryList");
	var imageContainer = document.createElement("p");
	imageContainer.setAttribute("id","imageContainer");
	galleryList.parentNode.insertBefore(imageContainer,galleryList);
}

function setThumbOnclick() {
	if (!document.getElementById("galleryList")) return false;
 	var galleryList = document.getElementById("galleryList");
	var aGalleryList = galleryList.getElementsByTagName("a");
	for (i=0; i<aGalleryList.length; i++) {
		var thisLink = aGalleryList[i];
		thisLink.onclick = function() {
			var linkToImage = this.getAttribute("href");
			var linkDescription = "";
			if (this.firstChild) {
				linkDescription = this.firstChild.getAttribute("alt");
			}
			if (linkToImage=="") return false;
			swapImage(linkToImage, linkDescription);
			return false;
		}
	}
}

addLoadEvent(setThumbOnclick);
addLoadEvent(swapImage);
// ##### RUN ONLOAD #####