﻿/**********************************************************************************************************
 * Author: Joshua Carmody
 * Last Modified: 2009-04-08
 **********************************************************************************************************
 * Including this javascript file in the HL7 site code fixes some problems that can occasionally occur with
 * float sizing, and causes IFRAMES that have SCROLLING="NO" set to automatically expand to the 
 * height of their content.
 **********************************************************************************************************/



function resizeIframes() {
    var iframeElements = document.getElementsByTagName("iframe");
    for (var i = 0; i < iframeElements.length; i++) {
        if (iframeElements[i].scrolling == "no") {
            try {
                if (iframeElements[i].contentWindow) {
                    var newHeight = (iframeElements[i].contentWindow.document.body.scrollHeight + 50);
                    if (newHeight < 900) {
                        newHeight = 900;
                    }
                    iframeElements[i].style.height = newHeight + "px";
                } else if (iframeElements[i].contentDocument) {
                    var newHeight = (iframeElements[i].contentDocument.body.scrollHeight + 50);
                    if (newHeight < 900) {
                        newHeight = 900;
                    }
                    iframeElements[i].style.height = newHeight + "px";
                }
            }
            catch (e) {            
                // The above code will fail with "access denied", if the parent document and the document
                // in the iframe are from different domains.
                // If that happens, we will instead try to add scrollbars to the iFrame.
                try {
                    iframeElements[i].style.overflow = "auto";
                }
                catch (e2) {
                }
            }
        }
    }
}



function HL7LayoutTweaks() {
    var maincontentElement = document.getElementById("maincontent");
    if (maincontentElement) {
        maincontentElement.style.width = (maincontentElement.parentNode.clientWidth - 219) + "px";
    }
    resizeIframes();
    var iframeElements = document.getElementsByTagName("iframe");
    for (var i = 0; i < iframeElements.length; i++) {
        // The code below causes pages containing IFrames to scroll back to the top when links are clicked within the IFrame.
        try {
            iframeElements[i].addEventListener("load", function() { scroll(0, 0); resizeIframes();  }, false);
        }
        catch (err) {
            try {
                iframeElements[i].attachEvent("onload", function() { scroll(0, 0); resizeIframes(); }, false);
            }
            catch (err2) {
            }
        } 
    }
}


try {
    window.addEventListener("load", HL7LayoutTweaks, false);
    window.addEventListener("resize", HL7LayoutTweaks, false);
}
catch(err) {
    try {
        window.attachEvent("onload", HL7LayoutTweaks);
        window.attachEvent("onresize", HL7LayoutTweaks);
    }
    catch(err2) {
        // Layout tweaks won't work. Not the end of the world.
    }
}