////////////////////////////////////////////////////////////////////////
//
//                    CHEditor Image Caption Util
//
////////////////////////////////////////////////////////////////////////
function addCaption (oImgElem, bUseCaptionMarker) {
  if( oImgElem.parentNode && oImgElem.parentNode.className=="imgblock")
    return;

  var align = 'left';
  if (oImgElem.align == 'right') align = 'right';

  var oImgBlockElem = document.createElement("div");
  oImgBlockElem.className = "imgblock";
  oImgBlockElem.style.styleFloat = align;
  oImgElem.style.styleFloat = "none";

  if(align = 'left') oImgBlockElem.className = oImgBlockElem.className + " leftjust";
  if(align == 'right') oImgBlockElem.className = oImgBlockElem.className + " rightjust";

  var oHandle = oImgElem;

  if(oImgElem.parentNode.tagName == "A") oHandle = oImgElem.parentNode;

  var oOldHandle = oHandle.parentNode.replaceChild(oImgBlockElem,oHandle);
  oImgBlockElem.appendChild(oOldHandle);
  oHandle = null;

  var oCaptionElem = document.createElement("div");
  oCaptionElem.className = "caption";
  oCaptionElem.style.marginLeft = oImgElem.style.marginLeft;



  var oCaptionTextElem = document.createElement("div");
  oCaptionTextElem.style.width = oImgElem.style.width;
  oCaptionTextElem.className = "caption-text";
  var oCaptionText = document.createTextNode( oImgElem.alt );
  oCaptionTextElem.appendChild(oCaptionText );
  oCaptionElem.appendChild(oCaptionTextElem);

  oImgBlockElem.appendChild(oCaptionElem);

  with (oImgElem.style) {
    oCaptionElem.style.width = (oImgElem.scrollWidth)+"px";
  }

  oImgBlockElem.style.width = (oImgElem.scrollWidth)+"px";
  return true;
}