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>';
                }
            }
            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.

                if ( jQuery('#alternation').val() != '' ) {
                    var alternation = jQuery('#alternation').val();
                }
                else {
                    var alternation = 'comment-even';
                }
				
				var responseText = "";
                responseText += '<div class="comment ' + alternation + '">';
                responseText += '<div class="post">';
				
                responseText += '<div class="info">';

                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 += '<span class="author"><fb:name uid="' + viewerId + '" linked="true" useyou="true"><a href="http://www.facebook.com/profile.php?id=' + viewerId + '">' + u.name + '</a></fb:name></span>';
					responseText += '<span class="date"> / <a name="c" class="nolink">' + stamp + '</a></span>';
                }
                else {
                    responseText += '<span class="author">' + jQuery('#comment-author').val() + '</span>';
					responseText += '<span class="date"> / ' + stamp + '</span>';
                }

                responseText += '</div>'; // Closing author meta
				
				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
                }
				else{
					responseText += '<div class="user-pic">';
					responseText += '<a><img src="http://www.blogto.com/mt4/mt-static/images/default-userpic-90.jpg" width="42" height="42" alt="" /></a>';	
					responseText += '</div>'; // Close the user-pic div
					
				}
            
                var text = jQuery('#comment-text').val();
                text = text.replace(/\n/g,'<br />\n');
                responseText += '<div class="entry-body content">' + text + '</div>';
				
				responseText += '</div>'; // Close the post div
				
				responseText += '<div class="replytocomment">';
                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
            
                // Finally, display the comment.
                jQuery('#post_comments').append(responseText);

                // 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");
			
			// Call page via AJAX with the PAGECACHE.refresh command
			jQuery.get(location.pathname + "?refresh");
				
            mtEntryOnUnload();
        }
    }); 
}); 

