/**
 * Set the width of the body, depending on the contained elements.
 * Returns the calculated width.
 */
function packBody() {

        var barsMaxWidth = Math.max(getBarMinWidth('header', defaultSpace), getBarMinWidth('menubar', defaultSpace) + defaultSpace*2);
        var minPageWidth = Math.max(barsMaxWidth, document.getElementById('content').offsetWidth);

        var pageWidth = 0;

        if (minPageWidth > getWindowWidth()) {
                pageWidth = minPageWidth;
        } else {
                pageWidth = getWindowWidth();
        }

        // a debug security
        if (pageWidth > 2000) {
                pageWidth = 2000;
        }

        document.body.style.width = pageWidth + 'px';
        return pageWidth;
}

var defaultSpace = 10;

/**
 * Packs the header : adjusts the width of the bar to the body and moves the space label to the right
 */
function packHeader(windowWidth) {

        var header = document.getElementById('header');
        if (header) {
                var title = document.getElementById('title');
                var logo = document.getElementById('logo');

                header.style.height = (Math.max(title.clientHeight, logo.clientHeight)) + 'px';

                // set the width of the header bar, corresponding to the width of the body
                setWidth(header, windowWidth, defaultSpace);

                // moves the space label to the right
                title.style.position = 'absolute'; // needed by firefox in order the width of the element to be measured
                setPosition(title, defaultSpace, (windowWidth - defaultSpace - title.clientWidth));
        }
}

/**
 * Packs the menu bar : adjusts the width of the bar to the body and moves the "private" link to the left
 */
function packMenuBar(windowWidth) {

        var menubar = document.getElementById('menubar');
        if (menubar) {
                var btonPrivate = document.getElementById('btonPrivate');
                var btonAdvSearch = document.getElementById('btonAdvSearch');

                // set the width of the menu bar, corresponding to the width of the body minus the margins
                setWidth(menubar, windowWidth - defaultSpace*2, defaultSpace);

                var topAdjustment = (navigator.appName == 'Microsoft Internet Explorer') ? 5 : 3;
                
                // moves the "private" link to the left
                setPosition(btonPrivate, getTop(btonAdvSearch) - topAdjustment, defaultSpace * 2);
        }
}

/**
 * Packs the content block : adjusts the height of the left blocks to the gallery and moves the content block to the center of the page
 */
function packContent(windowWidth) {

        // adjusts the height of the item list
        var gallery = document.getElementById('gallery');
        var messageList = document.getElementById('messageList');
        var newsList = document.getElementById('newsList');
        var detailsList = document.getElementById('detailsList');

        if (gallery ) {
                var galleryTop = getTop(gallery);
                      if (newsList) {
                        var newsListTop = getTop(newsList);
                        if (galleryTop + gallery.clientHeight > (newsListTop + newsList.clientHeight)) {
                                newsList.style.height = (gallery.clientHeight + galleryTop - newsListTop - 2*defaultSpace) + 'px';
                        }
                      } else if (messageList) {
                              var messageListTop = getTop(messageList);
                        if (galleryTop + gallery.clientHeight > (messageListTop + messageList.clientHeight)) {
                                messageList.style.height = (gallery.clientHeight + galleryTop - messageListTop - 2*defaultSpace) + 'px';
                        }
                      } else if (detailsList) {
                              var detailsListTop = getTop(detailsList);
                        if (galleryTop + gallery.clientHeight > (detailsListTop + detailsList.clientHeight)) {
                                detailsList.style.height = (gallery.clientHeight + galleryTop - detailsListTop - 2*defaultSpace) + 'px';
                        }
                      }
        }

        // centers the content block (more robust than margin:auto)
        var content = document.getElementById('content');
        if (content) {
                content.style.marginLeft = (windowWidth-content.clientWidth)/2 + 'px';
        }
}

/**
 * Entry point of the layout processing.
 * Packs the body and then packs each block of the page.
 * Finally, adds corners to the menu bar.
 */
function layout() {
        if (document.getElementById) {

                var windowWidth = getWindowWidth();
                var windowHeight = getWindowHeight();

                var pageWidth = packBody();

                packHeader(pageWidth);
                packMenuBar(pageWidth);
                packContent(pageWidth);

                addCorners('menubar');
        }
}