function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}


function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

function captionizeImages() {
	if (!document.getElementsByTagName) return false;
	if (!document.createElement) return false;
	var images = document.getElementsByTagName("img");
	if (images.length < 1) return false; 
		for (var i=0; i<images.length; i++) {
			if (images[i].className.indexOf("captioned") != -1 || images[i].className.indexOf("captioned_right") != -1 || images[i].className.indexOf("captioned_left") != -1) {
			
			imgFloat = ""
			if (images[i].className.indexOf("captioned_right") != -1) {
				imgFloat = " right"
			}
			if (images[i].className.indexOf("captioned_left") != -1) {
				imgFloat = " left"
			}
			var title = images[i].getAttribute("title");
			var divCaption = document.createElement("div");
			divCaption.className="caption";
			var divCaption_text = document.createTextNode(title);
			divCaption.appendChild(divCaption_text);
			var divContainer = document.createElement("div");
			divContainer.style.width = images[i].width+'px'
			divContainer.className="imgcontainer"+imgFloat;
			images[i].parentNode.insertBefore(divContainer,images[i]);
			divContainer.appendChild(images[i]);
			insertAfter(divCaption,images[i]);
		}
	}
}
addLoadEvent(captionizeImages);
