jQuery(document).ready(function() { 
    jQuery('#comments-form').ajaxForm({
        beforeSubmit: function(data, obj, opt) {
            // Start by disabling the post button
            jQuery('#submit-button').attr("disabled", "disabled");
            var responseText;

            // Verify that the comment content is valid
            var u = mtGetUser();
            if ( u && u.is_authenticated ) { // an authenticated user
                // If the cookie is valid, there's nothing to check!
            }
            else {
                if ( jQuery('#comment-author').val() == '' ) {
                    responseText = '<p>Enter an author name before submitting a comment.</p>';
                }
                if ( jQuery('#comment-email').val() == '' ) {
                    responseText = '<p>Enter a valid email address before submitting a comment.</p>';
                }
                // Remember the anonymous commenter's settings, if they want them saved.
                if ( jQuery('#comment-bake-cookie').is(':checked') ) {
                    mtSaveUser( $('#comments-form') );
                }
            }
            if ( jQuery('#comment-text').val() == '' ) {
                responseText = '<p>Enter some comment text before submitting.</p>';
            }

            if (responseText) {
                jQuery('#comment-status').css('display', 'block');
                jQuery('#comment-status').html('<h3>Comment Error</h3>' + responseText);
                jQuery('#comment-status>p').css('color', 'red');
                // Enable the button on the form again.
                jQuery('#submit-button').removeAttr("disabled");
                return false;
            }
            else { // The comment was validated, and can be submitted. Display the result.

                responseText = '<div style="margin-top: -28px; margin-bottom: 28px;">';
                if ( jQuery('#alternation').val() != '' ) {
                    var alternation = jQuery('#alternation').val();
                }
                else {
                    var alternation = 'comment-even';
                }
                responseText += '<div class="comment ' + alternation + '">';
                responseText += '<div class="avatar">';

                if ( u && u.is_authenticated ) {
                    // Get the current logged in Facebook user
                    var session = FB.Facebook.apiClient.get_session();
                    if (!session) return;
                    var viewerId = session.uid;
                    if (!viewerId) return;

                    responseText += '<div class="user-pic comment-fb-' + viewerId + '">';
                    responseText += '<a href="http://www.facebook.com/profile.php?id=' + viewerId + '" class="auth-icon"><img src="/mt4/mt-static/plugins/FacebookCommenters/facebook_logo.png" alt="Authenticated with Facebook" \/><\/a>';
                    responseText += '<fb:profile-pic uid="' + viewerId + '" size="square" linked="true"><img src="http://static.ak.connect.facebook.com/pics/q_default.gif" \/><\/fb:profile-pic>';
                    responseText += '</div>'; // Close the user-pic div
                }
            
                responseText += '</div>'; //Close the avatar div
                responseText += '<div class="post">';
                responseText += '<p class="author">';

                var currentTime = new Date();
                var year  = currentTime.getFullYear();
                var month = currentTime.getMonth() + 1;
                var date  = currentTime.getDate();
                var hour  = currentTime.getHours();
                var min   = currentTime.getMinutes();

                if (month ==  1) { month = 'January'; }
                if (month ==  2) { month = 'February'; }
                if (month ==  3) { month = 'March'; }
                if (month ==  4) { month = 'April'; }
                if (month ==  5) { month = 'May'; }
                if (month ==  6) { month = 'June'; }
                if (month ==  7) { month = 'July'; }
                if (month ==  8) { month = 'August'; }
                if (month ==  9) { month = 'September'; }
                if (month == 10) { month = 'October'; }
                if (month == 11) { month = 'November'; }
                if (month == 12) { month = 'December'; }
            
                if (hour == 12 ) {
                    meridiem = 'PM';
                }
                else if (hour >= 13) {
                    meridiem = 'PM';
                    hour = hour - 12;
                }
                else {
                    meridiem = 'AM';
                }
            
                if (min < 10) { min = '0' + min; }
            
                var stamp = month + ' ' + date + ', ' + year + ' at ' + hour + ':' + min + ' ' + meridiem;
            
                if (u && u.is_authenticated ) {
                    responseText += '<fb:name uid="' + viewerId + '" linked="true" useyou="true"><a href="http://www.facebook.com/profile.php?id=' + viewerId + '">' + u.name + '</a></fb:name> on <a name="c" class="nolink">' + stamp + '</a>';
                }
                else {
                    responseText += jQuery('#comment-author').val() + ' on ' + stamp;
                }

                responseText += '</p>'; // Closing author meta
            
                var text = jQuery('#comment-text').val();
                text.replace('/\n/','<br />');
                responseText += '<p>' + text + '</p>';

                responseText += '</div>'; // Close the post div

                responseText += '<div class="links">';
                responseText += '<a title="Reply" href="javascript:void(0);" onclick="alert(' + "'This page needs to be refreshed to create a comment reply.'" + ')">Reply</a>';
                responseText += '</div>';

                responseText += '<div class="clear"></div>';
                responseText += '</div>'; // Close the comment div
                responseText += '</div>'; // Close the extra div.
            
                // Finally, display the comment.
                jQuery('#comment-status').css('display', 'block');
                jQuery('#comment-status').html(responseText);
                jQuery('#comment-status>p').css('color', 'black');

                // This will let a Facebook-authenticated user post the story to their Wall.
                if (u && u.is_authenticated ) {
                    mtFireEvent('commentposted');
                }
                // Clear the existing comment.
                jQuery('#comment-text').val('');
                // Enable the button on the form again.
                jQuery('#submit-button').removeAttr("disabled");
            } // End the pre-submit comment display.
        },
        success: function(responseText, statusText) {
            //jQuery('#comment-status').css('display', 'block');
            //jQuery('#comment-status').html(responseText);
            //if ( responseText.match(/class="comment-confirmation"/) ) {
                // This will let a Facebook-authenticated user post the story to their Wall.
            //    mtFireEvent('commentposted');
                // Clear the existing comment.
            //    jQuery('#comment-text').val('');
            //}
            // Enable the button on the form again.
            //jQuery('#submit-button').removeAttr("disabled");
            mtEntryOnUnload();
        }
    }); 
}); 
