function listCount( list ) {
    return list.getElementsByTagName( "li" ).length;
}

function createCitations() {
    var body = document.getElementsByTagName( "body" ).item( 0 ); //reference to document body
    var footnote = document.createElement( "div" ); //div to hold footnotes
    var links = document.getElementById( "content" ).getElementsByTagName( "a" ); //references to all links in article (note: also contains bookmarks)
    var ol = document.createElement( "ol" );

    //assign ID to footnote div for CSS
    footnote.id = "footnotes";

    //create links and append to ol
    for( var i = 0; i < links.length; i++ ) {
        if( links[ i ].href && links[ i ].href.indexOf( "javascript:" ) < 0 ) {
            var text, node, t;
            var li = document.createElement( "li" );

            //get reference to link's text node
            t = links[ i ];
            while( t.nodeType != 3 ) {
                t = t.firstChild;
            }

            //assemble text for list item
            text = t.nodeValue + ": ";
            if( links[ i ].href.indexOf( "mailto:" ) > -1 ) {
                text += links[ i ].href.substr( 7 );
            } else {
                text += links[ i ].href;
            }

            //append reference to list
            node = document.createTextNode( text );
            li.appendChild( node );
            ol.appendChild( li );

            //add footnote reference to link
            var index = listCount( ol );
            var cite = document.createElement( "cite" );

            cite.appendChild( document.createTextNode( " [" + listCount( ol ) + "]" ) );
            links[ i ].appendChild( cite );
        }
    }

    //attach div to body
    footnote.appendChild( ol );
    if( listCount( ol ) > 0 ) {
        document.getElementById( "content" ).appendChild( footnote );
    }
}

addEvent( window, "load", createCitations );