/* Javascript originally written by: Brett Chang of evercloud.com */
/* Javascript altered to create one Div with a class of "caption" containing the image and the images alt-text displayed within a span for flexible styling abilities (plus it's a bit cleaner output and since an image is not tabular data, why would it be in a table?)*/

var IMAGE_TRACKER = new Array();

function addImgCaption(myImgElem)
{

	// alert('myImgElem');
	// alert(myImgElem);
	// dump(myImgElem, true);

	var myParent = myImgElem.parentNode;

	var myParentParent = myImgElem.parentNode.parentNode;

	var myWrapperTable = null;

	var myNodeForAppend = myImgElem;

	function createDiv(myImgElem, myNodeForAppend) {

		// alert('createDiv - myNodeForAppend');
		// alert(myNodeForAppend);
		// dump(myNodeForAppend, true);

		var eDiv = document.createElement("div");

		var eWrap = document.createElement("span");

		var eBr = document.createElement("br");
		
		var eBr2 = document.createElement("br");

		var eSpan = document.createElement("p");
		
		var eCaption =  document.createTextNode (myImgElem.getAttribute("title"));
		
		var capText = myImgElem.getAttribute("title");
		  
		//stylin'!

		eDiv.className = "caption";
		eDiv.style.width=myImgElem.getAttribute("width")+"px";
		eSpan.style.width=myImgElem.getAttribute("width")+"px";
		eDiv.style.cssFloat=myImgElem.getAttribute("align");
		eDiv.style.styleFloat=myImgElem.getAttribute("align");
		eSpan.className = "caption-text";
		eWrap.className = "image-wrapper";
		if(capText == null || capText == '') {
			eWrap.style.border = "none";
		}
		if(myImgElem.getAttribute("align") == "left") {
			eDiv.style.marginRight = "10px";
		}
		if(myImgElem.getAttribute("align") == "right") {
			eDiv.style.marginLeft = "10px";
		}
		eBr2.clear = "all";

		//create the table structure
		eDiv.appendChild(eWrap);
		eWrap.appendChild(myNodeForAppend);
		eDiv.appendChild(eBr);
  
		// var imgAlt = myNodeForAppend.getAttribute("alt");
		// var imgSrc = myNodeForAppend.getAttribute("src");  //http://temp.artofthepeace.ca/wp-content/uploads/2007/04/euphemia.jpg

		var imgAlt = myImgElem.getAttribute("alt");
		var imgSrc = myImgElem.getAttribute("src");  //http://temp.artofthepeace.ca/wp-content/uploads/2007/04/euphemia.jpg

		var imgSplit = imgSrc.split("/");
		var imgCount = (imgSplit.length-1);
		var imgName = imgSplit[imgCount];

		if (capText != null && capText != '') {
			if(imgName != imgAlt){
				eDiv.appendChild(eSpan);
				eSpan.appendChild(eCaption);
			}
		}
		  
  		/*myNodeForAppend.removeAttribute("alt");*/
		// myNodeForAppend.removeAttribute("title");
		myImgElem.removeAttribute("align");
  
		return eDiv;
				
	}

	if(null == IMAGE_TRACKER[myImgElem.getAttribute('src')])
	{

		if(myParent.nodeName == 'A'){
			// alert('A node - myParent');
			// alert(myParent);
			// dump(myParent, true);
			
			myNodeForAppend = myParent;

			myWrapperDiv = createDiv(myImgElem, myNodeForAppend);

			myParentParent.appendChild(myWrapperDiv);
		}
		else
		{

			// alert('NOT A node - myNodeForAppend');
			// alert(myNodeForAppend);
			// dump(myNodeForAppend, true);
	
			myWrapperDiv = createDiv(myImgElem, myNodeForAppend);
	
			myParent.appendChild(myWrapperDiv);
		}

		IMAGE_TRACKER[myImgElem.getAttribute('src')] = '1';

	}

}
