﻿var ie56 = false; // MUST ONLY BE EVALUATED ONLOAD
var ie567 = false; // MUST ONLY BE EVALUATED ONLOAD
var ie7 = false; // MUST ONLY BE EVALUATED ONLOAD
var chrome = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) ? true : false;

window.addEvent('load', function() {
    checkResized(false);
    setContentDivHeight();
    if (ie56) pngfix();

    var myChain = new Chain;

    // IE6 COPYRIGHT fades in a disgusting mess, so hide it until later
    if (ie56) myChain.chain(function() { $('copyright').style.visibility = 'hidden'; myChain.callChain(); });

    // remove contentdiv until after sizerdiv faded in (for Firefox)
    myChain.chain(function() { $('contentwrapper').style.display = 'none'; myChain.callChain(); });

    // SIZERDIV 2.4s then wait 1s
    var myFx = new Fx.Tween('sizerdiv', { duration: 2400, onComplete: function() { $('loadingmessage').style.display = 'none'; (function() { myChain.callChain(); }).delay(1000); } });
    myChain.chain(function() { myFx.start('opacity', 0, 1) });

    // restore contentdiv
    myChain.chain(function() { $('contentwrapper').style.display = 'block'; myChain.callChain(); });

    // COAT OF ARMS (0.6s for fade or wait afterwards)
    // can't fade it in IE or chrome, so just show it
    if (ie567 || chrome) myChain.chain(function() { (function() { $('coatofarms').style.visibility = 'visible'; myChain.callChain(); }).delay(600); });
    // all other browsers get a nice fadey coat of arms
    else {
        var myFx2 = new Fx.Tween('coatofarms', { duration: 800, onComplete: function() { myChain.callChain(); } });
        myChain.chain(function() { myFx2.start('opacity', 0, 1) });
    }

    // CONTENTWRAPPER 1.3s
    if (!ie56) {
        var myFx3 = new Fx.Tween('contentwrapper', { duration: 1300, onComplete: function() { myChain.callChain(); } });
        myChain.chain(function() { myFx3.start('opacity', 0, 1) });
    }
    // In IE6 the transparent PNGs inside the content region look awful fading in, even after pngfix, so don't fade
    else myChain.chain(function() { $('contentwrapper').style.visibility = 'visible'; myChain.callChain(); });

    // CONTENTBG FIX
    // Fade out contentbg after contentwrapper fade in, for IE and Chrome
    // IE can't fade in a translucent contentBG as the text above appears full of artefacts. Solution: fade in with a standard fully opaque contentbg and then fade it out after.
    // Chrome ignores the 0.8 opacity on contentbg and fades it in 100% opaque and then 'snaps' to the 0.8 opacity. Solution: as for IE.
    if (chrome) {
        $('contentbg').style.opacity = '1'; // during contentwrapper fade in, contentbg will be fully opaque and thus avoid the 'snap'
    }

    if (chrome || ie7) { // IE6 cannot do this for some reason, so instead, contentbg alpha opacity filter is preset in Styles-IE56.css
        var myFx4 = new Fx.Tween('contentbg', { duration: 400, onComplete: function() { myChain.callChain(); } }); // do the contentbg fade-out
        myChain.chain(function() { myFx4.start('opacity', 1, 0.8) });
    }

    // IE7 CLEARTYPE FIX: clone sizerdiv before fades (with ClearType on), and swap it back after all the fades have finished
    if (ie7) {
        var finalClone = $('sizerdiv').clone(true, true);
        myChain.chain(function() { // swap in the clone and re-set element styles.
            finalClone.replaces('sizerdiv');
            $('sizerdiv').style.visibility = 'visible';
            $('contentwrapper').style.visibility = 'visible';
            $('coatofarms').style.visibility = 'visible';
            $('contentbg').style.filter = 'alpha(opacity=80)';
            myChain.callChain();
        });
    }

    // IE6 COPYRIGHT show at end
    if (ie56) myChain.chain(function() { $('copyright').style.visibility = 'visible'; myChain.callChain(); });

    // wait for everything to finish before starting resize check loop
    myChain.chain(function() { checkResized(true); myChain.callChain(); });

    // GO
    myChain.callChain();
});


/*
window.addEvent('domready', function() {
var status = {
'true': 'open',
'false': 'close'
};
});
*/

function addLoadEvent(func) { // MPJ v0.1 09-Dec-08
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function returnObjById(id) // MPJ v0.1 09-Dec-08
{
    var returnVar;
    if (document.getElementById)
        returnVar = document.getElementById(id);
    else if (document.all)
        returnVar = document.all[id];
    else if (document.layers)
        returnVar = document.layers[id];
    return returnVar;
}

var origwinsize;
var newwinsize;
var resized = false;
function checkResized(loop) { // MPJ v0.1 09-Dec-08
    if (!origwinsize) origwinsize = document.documentElement.clientHeight;
    newwinsize = document.documentElement.clientHeight;
    if (newwinsize != origwinsize) // resizing
    {
        resized = true;
        origwinsize = newwinsize;
    }
    else {
        if (resized) {
            resized = false;
            setContentDivHeight();
        }
    }
    if (loop) setTimeout('checkResized(true)', 100);
}

function setContentDivHeight() { // MPJ TODO: 0) still getting main scrollbar in many browsers. 1) IE6 - use height not maxHeight. 2) Make main scrolly appear IF the bottom of the contentdiv is outside the window (rather than on an arbitrary min window height)
    var windowheight = document.documentElement.clientHeight;
    if (windowheight > 400) {
        if (windowheight < 867) // [867 = 26 sizerdiv margin + 26 shadow vis height + pagewrapper 800 vis height + 15 copyright] 
        {
            if (ie56) {
                $('content').style.height = (windowheight - 240) + "px"; // [240 = 26 sizerdiv margin + 26 shadow vis height + 15 copyright + 90 pagewrapper padding + 2 contentwrapper border + 51 headerstrip + 30 mainmenu]
            }
            else $('content').style.maxHeight = (windowheight - 240) + "px";  // [240 = 26 sizerdiv margin + 26 shadow vis height + 15 copyright + 90 pagewrapper padding + 2 contentwrapper border + 51 headerstrip + 30 mainmenu]
            $('pagewrapper').style.height = (windowheight - 157) + "px"; // [157 =  26 sizerdiv margin + 26 shadow vis height + 15 copyright + 90 pagewrapper padding]
            $('pagewrapper').style.backgroundPosition = "center center"; // default
            // document.documentElement.style.overflow = "hidden";
            resetScrollingPosition(); // hiding scroll bar, must scroll to top!
        }
        else { // max height reached
            if (ie56) {
                $('content').style.height = "627px"; /* content max height */
            }
            else $('content').style.maxHeight = "627px";   /* content max height */
            $('pagewrapper').style.height = "710px"; /* pagewrapper height */
            $('pagewrapper').style.backgroundPosition = "center center"; // default
            // document.documentElement.style.overflow = "hidden";
            resetScrollingPosition(); // unnecessary, but just in case
        }
    }
    else { // tiny screen, don't want two scroll bars (content and screen), so set content to overflow and screen to scroll.
        if (ie56) {
            $('content').style.height = "627px";
            $('pagewrapper').style.height = "710px";
        }
        else {
            $('content').style.maxHeight = "none";
            $('pagewrapper').style.height = "auto";
        }
        $('pagewrapper').style.backgroundPosition = "top center"; // if content is very long, prevents bg image from suddenly appearing in the middle of that content
        // document.documentElement.style.overflow = "auto"; // not "visible", doesn't work in IE7
    }
}

/*
function getScrollingPosition() {
var position = [0, 0];
if (typeof window.pageYOffset != 'undefined') {
position = [window.pageXOffset, window.pageYOffset];
}
else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
}
else if (typeof document.body.scrollTop != 'undefined') {
position = [document.body.scrollLeft, document.body.scrollTop];
}
return position;
}
*/

function resetScrollingPosition() { // MPJ v0.1 09-Dec-08
    if (typeof document.documentElement.scrollTop != 'undefined') document.documentElement.scrollTop = 0; // YES: ie7, ff3, opera962, NO: safari321, chrome0.4.154
    if (typeof document.body.scrollTop != 'undefined') document.body.scrollTop = 0; // YES: opera962, safari321, chrome0.4.154, NO: ie, ff3, 
}
