﻿function addCaption(oImgElem, bUseCaptionMarker) {
    // Check that image element not already have a div.imgblock as parent.
    if (oImgElem.parentNode && oImgElem.parentNode.className == "imgblock")
        return;

    // Create the div.imgblock element      
    var oImgBlockElem = document.createElement("div");
    oImgBlockElem.className = "imgblock";
    oImgBlockElem.style.styleFloat = oImgElem.style.styleFloat;
    oImgElem.style.styleFloat = "none";

    var oHandle = oImgElem;  // oHandle is element that should be moved into our div.imgblock element

    // If the current image has a parent A (anchor/hyperlink) element then
    // we would also like that to go into our div.imgblock, therefore the oHandle
    // is adjusted to point to the A element.
    if (oImgElem.parentNode.tagName == "A") {
        oHandle = oImgElem.parentNode;
    }

    //  alert("Before for '" + oImgElem.alt + "'");
    // Replace the oHandle node (the img or a) with our new div.imgblock element.
    var oOldHandle = oHandle.parentNode.replaceChild(oImgBlockElem, oHandle);  // This line sometimes crash in IE with error R6025!

    oImgBlockElem.appendChild(oOldHandle);

    oHandle = null;
    //  alert("After for '" + oImgElem.alt + "'");

    // Create div.caption element
    var oCaptionElem = document.createElement("div");
    oCaptionElem.className = "caption";
    oCaptionElem.style.marginLeft = oImgElem.style.marginLeft;

    // Create div.caption-text element with appropriate alt text
    var oCaptionTextElem = document.createElement("div");
    oCaptionTextElem.className = "caption-text";
    var oCaptionText = document.createTextNode(oImgElem.alt);
    oCaptionTextElem.appendChild(oCaptionText);
    oCaptionElem.appendChild(oCaptionTextElem);

    var oClearTextElem = document.createElement("div");
    oClearTextElem.className = "clear";
    oCaptionElem.appendChild(oClearTextElem);


    oImgBlockElem.appendChild(oCaptionElem);

    //return true;

    with (oImgElem.style) {
        oCaptionElem.style.width = (oImgElem.scrollWidth) + "px";
    }
    oImgBlockElem.style.width = (oImgElem.scrollWidth) + "px";

    if (oImgElem.getAttribute("align") == "right" ) {
        oImgBlockElem.style.styleFloat = oImgElem.getAttribute("align");
        oImgBlockElem.style.cssFloat = oImgElem.getAttribute("align");
        oImgBlockElem.style.margin = "0 0 0 10px";

    }
    if (oImgElem.getAttribute("align") == "left") {
        oImgBlockElem.style.styleFloat = oImgElem.getAttribute("align");
        oImgBlockElem.style.cssFloat = oImgElem.getAttribute("align");
        oImgBlockElem.style.margin = "0 10px 0 0";
    }


    return true;
}

function addCaps() {
    var i = 0;
    var oImages = document.images; //document.getElementsByTagName("img"); // document.images
    var oImg;
    //alert("inside addCaps() - " + oImages.length );
    for (i = 0; i < oImages.length; i++) {
        oImg = document.images[i];
        //alert("oImg.className = " + oImg.className);
        if (oImg.className.search("addcap") >= 0) {
            addCaption(oImg, true);
        }
    }

    return true;
}

