window.setInterval('callLayout()',1000); // if text size is modified, no event is fired

window.onload = function() {
        callLayout();
}
window.onresize = function() {
        callLayout();
}

/**
 * Proxy function, dedicated to the call of the "layout" function, which should be
 * implemented in the page.
 */
function callLayout() {
        layout();
}

/**
 * Gets the height of the frame
 */
function getWindowHeight() {
        var windowHeight = 0;


                if (document.documentElement && document.documentElement.clientHeight) {
                        windowHeight = document.documentElement.clientHeight;
                } else {
                        if (document.body && document.body.clientHeight) {
                                windowHeight = document.body.clientHeight;
                        }
                }

        return windowHeight;
}

/**
 * Gets the width of the frame
 */
function getWindowWidth() {
        var windowWidth = 0;

                if (document.documentElement && document.documentElement.clientWidth) {
                        windowWidth = document.documentElement.clientWidth;
                } else {
                        if (document.body && document.body.clientWidth) {
                                windowWidth = document.body.clientWidth;
                        }
                }

        return windowWidth;
}

/**
 * Gets the top coordinate of the given object, relatively to the top of the frame
 */
function getTop(obj) {
        if (obj.offsetParent) {
                return (obj.offsetTop + getTop(obj.offsetParent));
        } else {
                return (obj.offsetTop);
        }
}

/**
 * Gets the left coordinate of the given object, relatively to the left of the frame
 */
function getLeft(obj) {
        if (obj.offsetParent) {
                return (obj.offsetLeft + getLeft(obj.offsetParent));
        } else {
                return (obj.offsetLeft);
        }
}

/**
 * Moves the given element to the specified coordinates
 */
function setPosition(obj, topOffset, leftOffset) {
        if (obj) {
                obj.style.position = 'absolute';
                obj.style.top = topOffset + 'px';
                obj.style.left = leftOffset + 'px';
        }
}

/**
 * Set the width of a block, taking into account the various interpretations
 * of the padding by some browsers
 */
function setWidth(obj, width, padding) {
        if (obj) {
                obj.style.width = width + 'px';

                // some browsers add the padding to the specified width, then the bar is too large and the padding must be substracted
                if (obj.clientWidth > width) {
                        obj.style.width = (width - padding*2) + 'px';
                }
        }
}

/**
 * Adds four corners to the specified block
 */
function addCorners(blockName) {

        var block = document.getElementById(blockName);
        var top = getTop(block);
        var left = getLeft(block);

        addCorner(blockName, top, left, 'tl');
        addCorner(blockName, top, (left + block.clientWidth - 5), 'tr');
        addCorner(blockName, (top + block.clientHeight - 5), left, 'bl');
        addCorner(blockName, (top + block.clientHeight - 5), (left + block.clientWidth - 5), 'br');
}

/**
 * Adds a single corner to the specified block
 */
function addCorner(blockName, topOffset, leftOffset, type) {

        var img = document.getElementById(blockName + type)
        if (!img) {
                // the corner has not yet been added
                img = document.createElement('img');
                img.id = blockName + type;
                document.body.appendChild(img);
                img.style.position = 'absolute';
                img.style.padding = '0px';
                img.style.width = '5px';
                img.style.height = '5px';
                img.src = '/rsrc/img/auto/FFFFFF/corn_5_' + type + '.gif';
        }
        img.style.top = topOffset + 'px';
        img.style.left = leftOffset + 'px';
}

/**
 * Gets the minimum width needed by an horizontal menu bar
 */
function getBarMinWidth(barName, padding) {

        var bar = document.getElementById(barName);
        if (!bar) {
                // the specified bar doesn't belong to this document, 0 is returned in order to avoid an exception
                return 0;
        }

        var minWidth = 0;
        var barItems = bar.childNodes;

        // loops on all children of the bar, and sums their widths
        for (var i = 0; i < barItems.length; i++) {
                if (barItems[i].nodeType==1) {
                        minWidth = minWidth + barItems[i].offsetWidth;
                } else {
                        bar.removeChild(barItems[i]);
                }
        }
        return minWidth + padding*2;
}