function buildGallery() {
	var ulList = document.getElementsByTagName("ul");
	for (var i = 0; i < ulList.length; i++) {
		if (ulList[i].className == "gallery") {
			var thisUl = ulList[i];
//CREATE IMAGE CONTAINER PK
			var imageContainer = document.createElement("p");
			imageContainer.className = "imageContainer";
			thisUl.parentNode.insertBefore(imageContainer, thisUl);
			var newImage = document.createElement("img");
			imageContainer.appendChild(newImage);
			var liList = thisUl.getElementsByTagName("li");
			newImage.src = liList[0].firstChild.firstChild.src;

//CREATE CAPTION CONTAINER P
			var captionContainer = document.createElement("p");
			captionContainer.className = "captionContainer";
			thisUl.parentNode.insertBefore(captionContainer, thisUl.nextSibling);
			var firstCaption = document.createTextNode(liList[0].firstChild.firstChild.getAttribute("alt"));
			captionContainer.appendChild(firstCaption);

// LOOP THRU LI'S
			for (var j = 0; j < liList.length; j++) {
				var thisLi = liList[j];
// REPLACE IMAGE WITH [#]
				if (thisLi.firstChild.firstChild.nodeName == "IMG") {
					var thisLinkImg = thisLi.firstChild.firstChild;
					var imgAltTag = thisLinkImg.getAttribute("alt");
					if (imgAltTag!=null && imgAltTag!="") {
						thisLi.firstChild.setAttribute("title",imgAltTag);
					}
					thisLi.firstChild.removeChild(thisLinkImg);
					var newLinkText = document.createTextNode("[" + (j+1) + "]");
					thisLi.firstChild.appendChild(newLinkText);
				}
// ADD ONCLICK BEHAVIOR
				thisLi.firstChild.onclick = function() {
// IF IMAGE CONTAINER P EXISTS, CHANGE THE SRC
					if (this.parentNode.parentNode.previousSibling.firstChild) {
						if (this.parentNode.parentNode.previousSibling.firstChild.nodeName == "IMG") {
							var previousP = this.parentNode.parentNode.previousSibling.firstChild;
							previousP.src = this.getAttribute("href");
						}
					}

// IF CAPTION CONTAINER P EXISTS, CHANGE THE CONTENT
					if (this.parentNode.parentNode.nextSibling) {
						if (this.parentNode.parentNode.nextSibling.nodeName == "P") {
							var nextP = this.parentNode.parentNode.nextSibling;
							if (nextP.firstChild) {
								nextP.removeChild(nextP.firstChild);
							}
							var newCaptionText = this.getAttribute("title");
							var newCaption = document.createTextNode(newCaptionText);
							nextP.appendChild(newCaption);
						}
					}

// LOOP THRU [#]S AND REMOVE CLASS
					if (this.parentNode.parentNode) {
						if (this.parentNode.parentNode.nodeName == "UL") {
							var liList = this.parentNode.parentNode.getElementsByTagName("li");
							for (var i = 0; i < liList.length; i++) {
								var thisLi = liList[i];
								if (thisLi.firstChild) {
									if (thisLi.firstChild.nodeName == "A") {
										var thisA = thisLi.firstChild;
										thisA.className = "";
									}
								}
							}
						}
					}

// ADD CLASS TO SELECTED NUMBER
					if (this.nodeName == "A") {
						this.className = "clickedLink";
					}

					return false;
				}
			} // FOR
		}
	}
}

addLoadEvent(buildGallery);