function toggleComments() {
    // 'this' is the anchor element clicked on
    // extract the numerical ID from the anchor name:
    var startIndex = "comment-toggle".length;
    var numId = this.getAttribute( "id" ).substr( startIndex );

    // get the div with the same numerical ID:
    div = document.getElementById( "comment-block" + numId );
    var divClass = div.getAttribute( "class" );
    if ( divClass == "comment-block" )
    {
        attr = div.style;
        if ( attr.display == "none" )
        {
            attr.display = "block";
        }
        else
        {
            attr.display = "none";
        }
    }
    return false;
}


function initCommentFunctions() {
    // don't hide the comments on archive pages:
    if (/\/[0-9]{4}\//.exec(document.location.href)) return;

    // flag to indicate if it's safe to hide comments:
    var hideComments = false;

    // Show the toggle button:
    spans = document.getElementsByTagName("li");
    for ( var i = 0 ; i < spans.length ; i++ )
    {
        var spanClass = spans[i].getAttribute( "class" );
        if ( spanClass == "comment-toggle" )
        {
            // show the toggle:
            spans[i].style.display = "inline";

            // give the toggle function to the toggle:
            a = spans[i].getElementsByTagName("a")[0];
            a.onclick = toggleComments;

            // confirm that toggles are available (safe to hide comments):
            hideComments = true;
        }
    }

    // Hide comments:
    if ( hideComments )
    {
        divs = document.getElementsByTagName( "div" );
        for ( var i = 0 ; i < divs.length ; i++ )
        {
            var divClass = divs[i].getAttribute( "class" );
            if ( divClass == "comment-block" )
            {
                divs[i].style.display = "none";
            }
        }
    }
}

/*
 * this function is swiped wholesale from Simon Willison:
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */

function addLoadEvent( callbackFunction ) {
    var oldOnloadFunction = window.onload;
    if ( typeof window.onload != 'function' ) {
        window.onload = callbackFunction;
    } else {
        window.onload = function() {
            oldOnloadFunction();
            callbackFunction();
        }
    }
}

addLoadEvent( initCommentFunctions );
