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

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) {
	  var title = images[i].getAttribute("title");
	  var longdesc = images[i].getAttribute("alt");
	  var divCaption_header = document.createTextNode(title);
	  var divCaption_summary = document.createTextNode(longdesc);
	  var header = document.createElement("h3");
	  header.appendChild(divCaption_header);
	  var summary = document.createElement("p");
	  summary.appendChild(divCaption_summary);
	  var divCaption = document.createElement("div");
	  divCaption.className="caption";
	  divCaption.appendChild(header);
	  divCaption.appendChild(summary);
	  var divContainer = document.createElement("div");
	  divContainer.className="imgcontainer";
	  images[i].parentNode.insertBefore(divContainer,images[i]);
	  divContainer.appendChild(images[i]);
	  insertAfter(divCaption,images[i]);
	}
  }
}
addLoadEvent(captionizeImages);