var footnotes = new Array();

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var result = elm.attachEvent('on' + evType, fn);
        return result;
    } else {
        elm['on' + evType] = fn;
    }
}

function getEvent(e) {
    if (window.event) {
        return window.event;
    } else {
        return e;
    }
}

function cancelTimer(footnote) {
    if (footnote.fnTimerID != 0) {
        clearInterval(footnote.fnTimerID);
        footnote.fnTimerID = 0;
    }
}

function moveFootnote(footnote, direction) {
    footnote.fnCurrentHeight += direction;
    footnote.style.bottom = footnote.fnCurrentHeight + 'px';
    footnote.style.top = 'auto';
    if (footnote.fnCurrentHeight >= 0) {
        cancelTimer(footnote);
        footnote.fnState = 'open';
        footnote.fnCurrentHeight = 0;
        footnote.style.bottom = '0px';
    } else if (footnote.fnCurrentHeight <= -footnote.fnHeight) {
        cancelTimer(footnote);
        footnote.style.bottom = 'auto';
        footnote.style.top = '100%';
        footnote.fnState = 'closed';
        footnote.fnCurrentHeight = -footnote.fnHeight;
    }
}

function startMoving(footnote, direction) {
    footnote.fnTimerID = setInterval(function () { moveFootnote(footnote, direction) }, 20);
}

function closeFootnote(footnote) {
    if (isOpen(footnote)) {
        cancelTimer(footnote);
        footnote.fnState = 'closing';
        startMoving(footnote, -6);
    }
}

function openFootnote(footnote) {
    if (isClosed(footnote)) {
        cancelTimer(footnote);
        footnote.fnState = 'opening';
        startMoving(footnote, +6);
    }
}

function stop(e) {
    e = getEvent(e);
    e.preventDefault();
    e.stopPropagation();
}

function makeCloseFootnote(id) {
    return function (e) { stop(e); closeFootnote(footnotes[id]); };
}

function isClosed(footnote) {
    return (footnote.fnState == 'closing' || footnote.fnState == 'closed');
}

function isOpen(footnote) {
    return (footnote.fnState == 'opening' || footnote.fnState == 'open');
}

function makeToggleFootnote(id) {
    return function (e) {
        stop(e);
        var footnote = footnotes[id];
        if (footnote) {
            if (isClosed(footnote)) {
                openFootnote(footnote);
            } else {
                closeFootnote(footnote);
            }
        } else {
            alert('footnote "' + id + '" not found');
        }
    };
}

function setupFootnotes() {
    var body = document.getElementsByTagName('body')[0];
    var all_a = body.getElementsByTagName('a');
    for (var i = 0; i < all_a.length; ++i) {
        var link = all_a[i];
        if (link.className && link.className == 'footnoteref') {
            var id = 'footnote' + link.childNodes[0].nodeValue;
            addEvent(link, 'click', makeToggleFootnote(id), false);
        }
    }
    var all_div = body.getElementsByTagName('div');
    for (i = 0; i < all_div.length; ++i) {
        var div = all_div[i];
        if (div.className && div.className == 'footnote') {
            div.fnState = 'closed';
            div.fnTimerID = 0;
            div.style.position = 'fixed';
            div.style.left = '0';
            div.style.right = '0';
            div.style.width = 'auto';
            div.style.borderTop = '3px solid black';
            footnotes[div.id] = div;
            div.fnHeight = div.offsetHeight;
            div.fnCurrentHeight = -div.fnHeight;
            div.style.bottom = 'auto';
            div.style.top = '100%';
            var button = document.createElement('button');
            button.style.position = 'relative';
            button.style.bottom = '1ex';
            button.style.right = '1ex';
            var X = document.createTextNode("X");
            button.appendChild(X);
            addEvent(button, 'click', makeCloseFootnote(div.id), false);
            div.insertBefore(button, div.firstChild);
        }
    }
}

function setupJS() {
    if (!document.getElementsByTagName) return;
    if (!document.createElement) return;
    if (!document.createTextNode) return;
    setupFootnotes();
}

addEvent(window, 'load', setupJS, false);

