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].title != '') {
            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");
            if (images[i].className == "right") {
                divContainer.className="imagecontainer_right";
            } else {
                divContainer.className="imagecontainer_left";
            }
            if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
                var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
                if (ffversion>=3) // Working correctly for version 3 (with small white line)
                    ; // keep class for image
                else if (ffversion>=2) // Partly working
                    images[i].className = "";
                else if (ffversion>=1) // Untested
                    images[i].className = "";
                else
                    images[i].className = "";
            } else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
                var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
                if (ieversion>=8) // Untested
                    images[i].className = "";
                else if (ieversion>=7) // Working correctly
                    images[i].className = "";
                else if (ieversion>=6) // Working correctly
                    images[i].className = "";
                else if (ieversion>=5) // Untested
                    images[i].className = "";
                else
                    images[i].className = "";
            } else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
                var oprversion=new Number(RegExp.$1) // capture x.x portion and store as a number
                if (oprversion>=10) // Untested
                    images[i].className = "";
                else if (oprversion>=9) // Working correctly (with small white line)
                    images[i].className = "";
                else if (oprversion>=8) // Working correctly (except no opacity)
                    images[i].className = "";
                else if (oprversion>=7) // Working correctly (except no opacity)
                    images[i].className = "";
                else // Opera 3, 5 and 6 have no JavaScript capabilities
                    images[i].className = "";
            } else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Chrome/x.x or Chrome x.x (ignoring remaining digits);
                var chversion=new Number(RegExp.$1) // capture x.x portion and store as a number
                if (chversion>=1) // Working correctly for version 1 (with small white line)
                    ; // keep class for image
                else
                    images[i].className = "";
            } else
                images[i].className = "";

            //if (navigator.appName == "Microsoft Internet Explorer")
                images[i].className = "";
            images[i].parentNode.insertBefore(divContainer,images[i]);
            divContainer.appendChild(images[i]);
            insertAfter(divCaption,images[i]);
        }
    }
}
