
/**** FadeHeader **************/
var starting, stoping, waiting, header;
var fadeCycles = 25;
var fadeMultiplyer = Math.pow(100, 1 / fadeCycles);
function Fade() {
    if (starting.style.opacity < 1) {//fade

        var opacityIn = parseFloat(starting.style.opacity) * 100;

        if (opacityIn == 0) {//starting fade
            starting.style.visibility = 'visible';
            opacityIn = fadeMultiplyer;
        }
        else {
            opacityIn = opacityIn * fadeMultiplyer;
            if (opacityIn > 99) opacityIn = 100; //fix floating point errors
        }
        var opacityOut = 100 - opacityIn;

        starting.style.opacity = opacityIn / 100;
        starting.style.filter = 'alpha(opacity=' + Math.round(opacityIn) + ')';
        stoping.style.opacity = opacityOut / 100;
        stoping.style.filter = 'alpha(opacity=' + Math.round(opacityOut) + ')';

        if (opacityOut == 0) stoping.style.visibility = 'hidden'; //hide when finished fading
    }
    else if (waiting < 150) {//wait
        waiting++;
    }
    else { //reset
        stoping = starting;
        starting = (starting.nextSibling) ? starting.nextSibling : header.childNodes[0]; //if has next sibling then start it           
        waiting = 0;
    }
}
function SetupFade() {
    header = document.getElementById('header');
    for (var i = 0; i < header.childNodes.length; i++) if (header.childNodes[i].nodeType == 3) header.removeChild(header.childNodes[i]); //remove all textnodes
    starting = header.childNodes[0]; //a
    stoping = header.childNodes[2]; //c
    waiting = 0; //should wait first
    setInterval(Fade, 70);
}

/****** Persist Scroll Position ******/
function SaveScrollPosition() {
    createCookie('scrollposition', document.documentElement.scrollTop);
}
function SetScrollPosition() {
    window.scroll(0, readCookie('scrollposition') * 1);
}

/****** Onload functions ******/
window.onload = function() {
    SetScrollPosition();
    setTimeout(SetupFade, 100);
};

/****** OnBeforeUnload functions ******/
window.onbeforeunload = function() {
    SaveScrollPosition();
};


