// An array of colours that will be applied to H1 tags.
var h1Cols = ['#FFFFFF', '#e1e1f5', '#c2c2eb', '#a4a4e1', '#8585d6', '#6666cc'];

// Main script.
var h1Arr = [];
function h1Setup() {
    h1Arr = document.getElementsByTagName('h1');
    for (var h = 0; h < h1Arr.length; h++) {
        var h1 = h1Arr[h],
        text = h1.firstChild.nodeValue;
        h1.removeChild(h1.firstChild);
        h1.animNodes = [];
        for (var i = 0; i < text.length; i++) {
            var span = document.createElement('span');
            span.appendChild(document.createTextNode(text.substring(i, i + 1)));
            h1.appendChild(span);
            h1.animNodes[h1.animNodes.length] = span;
        }
        h1.animCount = 0;
        h1.animTimer = setInterval('h1Anim(' + h + ')', 50);
    }
};
function h1Anim(h) {
    var h1 = h1Arr[h],
    c = h1.animCount++,
    noAnim = 1;
    for (var i = 0; i < h1.animNodes.length; i++) {
        var s = h1.animNodes[i],
        frac = Math.max(0, Math.min(1, (c - i) / 10)),
        marg = document.all && !window.opera ? 'marginRight': 'marginLeft';
        if (s.animDone) continue;
        noAnim = 0;
        s.style.color = h1Cols[Math.floor(frac * 0.99999 * h1Cols.length)];
        if (frac == 1) {
            s.style[marg] = '0';
            s.animDone = 1;
        } else s.style[marg] = 0.6 * (1 - frac) + 'em';
    }
    if (noAnim) clearInterval(h1.animTimer);
    h1.style.visibility = 'inherit';
};

if (document.documentElement) {
    // Hide H1 elements for animation and trigger show on load.
    document.write('<style type="text/css"> h1 { visibility: hidden } </style>');
    var h1aOL = window.onload;
    window.onload = function() {
        if (h1aOL) h1aOL();
        h1Setup();
    }
}
