$(document).ready(function() {

    $('.comment-form').submit(function() {  
        var id = $(this).attr('id');
        var entry_id = id.replace(/^comment-form-/, '');
        var sUrl = "/cgi-bin/mt/" + "reply.fcgi";
        $.ajax({
            type: 'post',
            url: sUrl, 
            data: $('#comment-form-' + entry_id).serialize(),
            context: $('#ajax-comment-' + entry_id),
            success: function(responseText) {
                $(this).html(responseText);
                var entry_id = $(this).attr('id').replace(/^ajax-comment-/, '');
                var old_count = $("#commentcount-"+entry_id).html();
                var new_count = parseInt(old_count)+1;
                $("#commentcount-"+entry_id).html(new_count);
            }
        });
        $('#ajax-comment-' + entry_id).show();
        $('#ajax-comment-' + entry_id).html('<img src="http://rumrz.com/mt434-static/images/indicator.white.gif" />');
        $('#comment-form-' + entry_id).hide();
        return false;
    });
    
    

    $('.tweet-form').submit(function() {  
        var id = $(this).attr('id');
        var entry_id = id.replace(/^tweet-form-/, '');
        var sUrl = "/cgi-bin/mt/" + "reply.fcgi";
        $.ajax({
            type: 'post',
            url: sUrl, 
            data: $('#tweet-form-' + entry_id).serialize(),
            context: $('#tweet-entry-' + entry_id),
            success: function(responseText) {
                $(this).html("Thank you for Tweeting!");
                var entry_id = $(this).attr('id').replace(/^tweet-entry-/, '');
                var old_count = $("#tweets-"+entry_id).html();
                var new_count = parseInt(old_count)+1;
                $("#tweets-"+entry_id).html(new_count);
            }
        });
        $('#tweet-entry-' + entry_id).show();
        $('#tweet-entry-' + entry_id).html('<img src="http://rumrz.com/mt434-static/images/indicator.white.gif" />');
        $('#tweet-form-' + entry_id).hide();
        return false;
    });
    
    $('.tweet-entry').each(function(){
        var id = $(this).attr('id');
        var entry_id = id.replace(/^tweet-entry-/,'');
        updateLength(entry_id);
    });
    
    $('.tweet-field').keyup(function(){
        var id = $(this).attr('id');
        var entry_id = id.replace(/^tweet-field-/,'');
        updateLength(entry_id);
    });
    
    function updateLength(entry_id) {
        var c = $('#tweet-field-' + entry_id).val().length;
        var l = $('#tweet-chars-' + entry_id);
        l.html(140 - c);
        l.css('color', (140 - c < 0) ? '#D40D12' : '#666');  
    }

});
