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 ctn = document.getElementById('content');
  var images = ctn.getElementsByTagName("img");
  if (images.length < 1) return false; 
  for (var i=0; i<images.length; i++) {
      title = images[i].getAttribute("title");
      img_alt = images[i].getAttribute("alt");
      img_align = images[i].getAttribute("align");
      images[i].setAttribute("align","");
      img_width = images[i].width;

    //if ((title != -1) || (title != '')) 
    if (title) 
    {
      var divCaption = document.createElement("div");
      divCaption.className="caption";
      //divCaption.style.width = img_width+10;
      var divCaption_text = document.createTextNode(title);
      //divCaption.appendChild(divCaption_text);
      var divContainer = document.createElement("div");
      divContainer.className="imgcontainer"+img_align;
      images[i].parentNode.insertBefore(divContainer,images[i]);
      divContainer.appendChild(images[i]);
      	var PicCaption = document.createElement("p");
      	PicCaption.className="caption";
      	PicCaption.appendChild(divCaption_text);
      	divContainer.appendChild(PicCaption);
		divContainer.style.width = img_width;
		//alert("W: " + divContainer.style.width);
      //divCaption.appendChild(PicCaption);
      insertAfter(divCaption,images[i]);
    }
    else{
      var divContainer = document.createElement("div");
      divContainer.className="imgcontainer"+img_align;
      images[i].parentNode.insertBefore(divContainer,images[i]);
      divContainer.appendChild(images[i]);
    	
    }
  }
}
addLoadEvent(captionizeImages);
