$(document).ready(function() {
    initComments();
    $('title').bind("keyup", function() {
        initComments();
    })
});



function initComments() {
    //$('.comment_form').hide();  // hide all forms on load
    $('.replies').hide();
    $('.showreplies').show();

    var form = document.getElementById('thecommentform');

    var h = $('.moveableform');
    var c = $('.commentform');
    var w = document.getElementById('commentformwrapper');
    var cancel = document.getElementById('cancelcomment');

    var cur_show;

    //h.hide();

    $("a.show").click(function( event ) {
        event.preventDefault();

        // if there's a hidden "reply" link, unhide it
        if (cur_show != null)
            cur_show.style.display = 'inline';

        var id = this.id.substr(5);
        var toplace = document.getElementById('comment_form_' + id)
        toplace.appendChild(w);

        cancel.style.display = 'inline';

        var thread = form.elements['thread'];
        thread.value = id;

        // save this link so we can reshow it later, then hide it
        cur_show = this;
        this.style.display = 'none';

    })
    $("#cancelcomment").click(function( event ) {
        event.preventDefault();

        var toplace = document.getElementById('firstcomment')
        toplace.appendChild(w);

        cancel.style.display = 'none';

        // if there's a hidden "reply" link, unhide it
        if (cur_show != null)
            cur_show.style.display = 'inline';

        var thread = form.elements['thread'];
        thread.value = null;
    })

    $("a.showreplies").click(function( event ) {
        event.preventDefault();
        $(this).next(".replies").slideDown("normal");
        $(this).hide();
    })
}