function init_comment_form() {
    $('.comments input').keyup(function() {
        var strLength = $(this).val().length;
        $(this).parent().find('label span.chars').text( strLength );
    });
    $('.comments textarea').keyup(function() {
        var str = $(this).val();
        var strLength = str.length;
        var maxLength = $(this).parent().find('label .maxlength').text();
        $(this).parent().find('label span.chars').text( strLength );
        if(strLength > parseInt( maxLength ) ) { $(this).val(str.substr(0,parseInt( maxLength ))); }
    });
    $('a.editLink').click(function() {
        $(this).parents().find('dd .comment-edit').show(); /* show the comment editing form */
        return false;
    });
    $('input.editCancel').click(function() {
        $(this).parents().find('dd .comment-edit').hide();
        console.log($(this).parents().find('dd .comment-edit'));
        return false;
    });
}

function init_comment_abuse_reporting() {
    $('a.adminlink').click(function() {
        $(this).parent().parent().parent().find('.comment-flag').show();
        $(this).parent().parent().parent().find('.comment-flag').find('input.flagDescription').focus();
        return false;
    });
    $('input.flagCancel').click(function() {
        $(this).parent().parent().hide();
        return false;
    })
    $('input.flagSave').click(function() {
        if( $(this).prev().val() == 'Description required' || jQuery.trim($(this).prev().val()) == '' ){
            return false;
        }
    })
    $('input.flagDescription').focus(function() {
        if( this.value == 'Description required' ){
            this.value='';
        }
    })
    $('input.flagDescription').blur(function() {
        if( jQuery.trim($(this).val()) == '' ){
            this.value='Description required';
        }
    });
}

$(function(){
    init_comment_form();
    init_comment_abuse_reporting();
});