function Stat() {
    this.content_height = 570;
    
    this.phase_out_length = 250;
    this.phase_in_length = 500;
    this.phase_in_wait = 500;
    
    this.last_sx = 0;
    this.last_mx = 0;
    this.menu_phase = 1;
    this.menu_base_phase = 1;
    this.menu_hide_start = false;
    this.menu_hide_end = false;
    this.menu_show_start = false;
    this.target_mx = 0;
    this.target_sx = 0;
    this.last_y = 0;
    
    this.resetViewport = function() {
        if ( typeof window.innerWidth != 'undefined' ) {
            this.vpwidth = window.innerWidth;
            this.vpheight = window.innerHeight;
        }
        else if ( typeof document.documentElement != 'undefined' &&
            typeof document.documentElement.clientWidth != 'undefined' &&
            document.documentElement.clientWidth != 0 ) {
            this.vpwidth = document.documentElement.clientWidth;
            this.vpheight = document.documentElement.clientHeight;
        }
        else {
            this.vpwidth = document.getElementsByTagName('body')[0].clientWidth;
            this.vpheight = document.getElementsByTagName('body')[0].clientHeight;
        };
    }
    
    this.resetViewport();
}
function center(element) {
    var w = element.offsetWidth;
    
    var x = 0;
    var el = element;
    while ( el ) {
        x += el.offsetLeft;
        el = el.offsetParent;
    }
    
    var sw = stat.vpwidth;
    var sx = Math.round( x + w / 2 - sw / 2 );
    
    var pw = document.body.scrollWidth;
    sx = ( sx + sw > pw) ? pw - sw : sx;
    sx = ( sx < 1 ) ? 0 : sx;
    
    stat.target_sx = sx;
    
    return false;
}
function moveResult( cx, tx) {
    if ( cx < tx ) return cx + Math.round( (tx - cx) / 5 + 0.5 );
    if ( cx > tx ) return cx - Math.round( (cx - tx) / 5 + 0.5 );
    return tx;
}
function moveLoop() {
    stat.resetViewport();
    
    var now = new Date();
    var sx = document.body.scrollLeft;
    // move target_sx if sx has changed (user moved scrollbar)
    if ( sx != stat.last_sx ) {
        stat.target_sx += sx - stat.last_sx;
        stat.last_sx = sx;
    }
    // move real scroll closer to target scroll
    if ( sx != stat.target_sx ) {
        var tsx = moveResult( sx, stat.target_sx );
        window.scroll( tsx, document.body.scrollTop );
        stat.last_sx = tsx;
    }
    // if menu is not in (or already moving to) the right place, start hiding the menu
    if ( ! stat.menu_hide_start && stat.target_sx != stat.target_mx ) {
        stat.menu_hide_start = now;
        stat.menu_base_phase = stat.menu_phase;
        stat.target_mx = stat.target_sx;
        stat.menu_show_start = false;
    }
    // if scroller in the right place, start countdown to show menu in the right place
    if ( ! stat.menu_show_start && sx == stat.target_sx && stat.menu_phase != 1 ) {
        stat.menu_show_start = now;
    }
    var new_phase = stat.menu_phase;
    // correct phase: first hide to the end, then show
    if ( stat.menu_hide_start ) {
        var time_passed = now.getTime() - stat.menu_hide_start.getTime();
        new_phase = 1 - ( ( time_passed ) / stat.phase_out_length );
        new_phase = new_phase - ( 1 - stat.menu_base_phase );
        if ( new_phase <= 0 ) {
            new_phase = 0;
            stat.menu_hide_start = false;
            stat.menu_hide_end = now;
        }
    }
    else if ( stat.menu_show_start ) {
        var time_passed = now.getTime() - stat.menu_show_start.getTime();
        // make sure we do not virtually reveal the menu before hide ends
        if ( stat.phase_in_wait < stat.phase_out_time && stat.menu_hide_end ) {
            var min_wait = stat.menu_hide_end.getTime() - stat.menu_show_start.getTime();
            var extra_wait = min_wait - stat.phase_in_wait;
            if ( extra_wait > 0 ) time_passed -= extra_wait;
        }
        if ( time_passed > stat.phase_in_wait ) {
            if ( stat.last_mx != stat.target_mx ) {
                var divs = new Array();
                divs.push( document.getElementById("rightheader") );
                divs.push( document.getElementById("header") );
                divs.push( document.getElementById("contact") );
                divs.push( document.getElementById("copyright") );
                
                for (var div in divs) {
                    divs[div].style.left = stat.target_mx + 'px';
                }
                stat.last_mx = stat.target_mx;
            }
            
            new_phase = ( time_passed - stat.phase_in_wait ) / stat.phase_in_length;
            if ( time_passed > stat.phase_in_wait + stat.phase_in_length ) {
                new_phase = 1;
                stat.menu_show_start = false;
            }
        }
    }
    // adjust menu phase
    if ( stat.menu_phase != new_phase ) {
        var divs = new Array();
        divs.push( document.getElementById("rightheader") );
        divs.push( document.getElementById("header") );
        divs.push( document.getElementById("contact") );
        divs.push( document.getElementById("copyright") );
        for (var div in divs) {
            divs[div].style.opacity = new_phase;
            divs[div].style.filter = 'alpha(opacity=' + Math.round( new_phase * 100 ) + ')';
        }
        stat.menu_phase = new_phase;
    }
    // move content vertically to center of the page
    var dest = Math.round( ( stat.vpheight - stat.content_height ) / 2 );
    dest = ( dest < 0 ) ? 0 : dest;
    
    if ( stat.last_y != dest ) {
        document.getElementById("centering").style.height = dest + 'px';
        stat.last_y = dest;
    }
    setTimeout("moveLoop()", 20);
}