function getWindowHeight(w) {
    var width, height;
    w = w ? w : window;
    height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);

    return height;
}

new Event.observe(window, 'load', function() {
    var tds = document.getElementsByTagName('td');
    for (var i = 0; i < tds.length; i++) {
        if (tds[i].className.indexOf('nav') != -1) {
            if (tds[i].firstChild && tds[i].firstChild.tagName.toLowerCase() == 'a') {
                tds[i].url = tds[i].firstChild.href;
                tds[i].onclick = function() { window.location.href = this.url; }
                tds[i].oldClassName = tds[i].className;

                tds[i].onmouseover = function() { this.oldClassName = this.className; this.className += ' nav-hover'; }
                tds[i].onmouseout = function() { this.className = this.oldClassName; }
                tds[i].style.cursor = 'pointer';
            }
        }
    }
    replaceNavImages();
});

new Event.observe(window, 'load', function() {
    contentFit();
    if ($('subnavWrapper') && !$('subnavWrapper').visible()) {
        $('subnavWrapper').slideDown({ duration: 0.5, delay: 0.5, from: 0.1 });
    }
});
window.setTimeout(function() { if ($('subnavWrapper') && !$('subnavWrapper').visible()) { $('subnavWrapper').slideDown({ duration: 0.5, delay: 0.0, from: 0.1 }); } }, 4000);
window.setTimeout(function() { if ($('subnavWrapper')) $('subnavWrapper').show(); }, 6000);

new Event.observe(window, 'resize', function() {
    contentFit();
});

var navImagesReplaced = false;
function replaceNavImages() {
    var els = document.getElementsByTagName('a');
    var serial;
    var match;

    if (navImagesReplaced) {
        return;
    }
    navImagesReplaced = true;

    for (var i = 0; i < els.length; i++) {
        if (els[i].className.indexOf('nav-link') > -1 && els[i].className.indexOf('subnav') == -1) {
            el = els[i];
            match = el.className.match(new RegExp('nav-link-([0-9]+)'));
            serial = 0;
            if (match) {
                serial = match[1];
            }
            //el.onmouseover = new Function('this.innerHTML = \'<img src="/ecritcms/dynimages/txtgen.php?p=emc&t=' + escape(el.innerHTML) + '&i=1' + serial + '" border="0" alt="' + el.innerHTML + '" />\';');
            //el.onmouseout  = new Function('this.innerHTML = \'<img src="/ecritcms/dynimages/txtgen.php?p=emc&t=' + escape(el.innerHTML) + '&i=' + serial + '" border="0" alt="' + el.innerHTML + '" />\';');
            el.innerHTML = '<img src="/ecritcms/dynimages/txtgen.php?p=emc&t=' + escape(el.innerHTML) + '&i=' + serial + '" border="0" alt="' + el.linktext + '" />';
        }
    }
}


function contentFit() {
    var winHeight = getWindowHeight();
    var layout = $('fs-wrapper').className;
    var doResize = true;

    var paddingBottom = 20;

    if (layout == 'layout-profiel') {
        var max = 0;
        doResize = false;

        ['fs-subnav', 'fs-content', 'fs-side-right'].each(function(id) {
            $(id).setStyle({ height: 'auto' });
            max = Math.max(max, $(id).clientHeight);
        });

        var pos = $('fs-subnav').cumulativeOffset();
        if (max < (winHeight - pos[1] - paddingBottom)) {
            doResize = true;
        } else {
            ['fs-subnav', 'fs-content', 'fs-side-right'].each(function(id) {
                $(id).setStyle({ height: max + 'px' });
            });
        }
    }

    if (doResize) {
        if ($('fs-subnav')) {
            var pos = $('fs-subnav').cumulativeOffset();
            $('fs-subnav').setStyle({ height: (winHeight - pos[1] - paddingBottom) + 'px' });
        }
        if ($('fs-content')) {
            var pos = $('fs-content').cumulativeOffset();
            $('fs-content').setStyle({ height: (winHeight - pos[1] - paddingBottom) + 'px' });
            $('fs-content').setStyle({ overflow: 'auto' });
        }
        if ($('fs-side-right')) {
            var pos = $('fs-side-right').cumulativeOffset();
            $('fs-side-right').setStyle({ height: (winHeight - pos[1] - paddingBottom) + 'px' });
        }
    }
}