$(document).ready(function() {
    previewed = false;
    
    commentBusy = false;
});

function ajaxComment(args) {
    var media = args.media;
	$('ul.errorlist').remove();
    
    $('div.comment-error').remove();
    
    if (commentBusy) {
        $('div.comment-form form').before('\
            <div class="comment-error">\
                Your comment is currently in the process of posting.\
            </div>\
        ');
        $('div.comment-error').fadeOut(2000);
        
        return false;
    }
    
    comment = $('div.comment-form form').serialize();
   
    // Add a wait animation
    $('.form_submit').after('\
        <img src="/media/img/loader.gif" alt=""\
            class="ajax-loader" />\
			<span class="ajax-loader-span">Please wait...</span>\
    ');
    
    // Indicate that the comment is being posted
    $('p.submit').after('\
        <div class="comment-waiting" style="display: none;">\
            One moment while the comment is posted. . .\
        </div>\
    ');
    $('div.comment-waiting').fadeIn(1000);
    
    commentBusy = true;
    
    url = $('div.comment-form form').attr('action');
    
    // Use AJAX to post the comment.
    $.ajax({
        type: 'POST',
        url: url,
        data: comment,
        success: function(data) {
            commentBusy = false;
        
            removeWaitAnimation()
        
            if (data.success == true) {
                commentSuccess(data);
            } else {
                commentFailure(data);
            }
        },
        error: function(data) {
            commentBusy = false;
            
            removeWaitAnimation()
            
            $('div.comment-form form').unbind('submit');
            $('div.comment-form form').submit();
        },
        dataType: 'json'
    });
    
    return false;
}

function commentSuccess(data) {
    prepare_comment_form();
    email = $('#id_email').val();
    name = $('#id_name').val();
    url = $('#id_url').val();
    
	if (!name) {
		name = $('#auth_user').val();
	}

	if (!email) {
		email = $('#auth_user_email').val();
	}
    
    if ($('div#comments').children().length == 0) {
        $('div#comments').prepend(
            '<h2 class="comment-hd">1 comment so far:</h2>'
        )
    }
        var user_str = name;
	if(url != "") {
	 user_str = '<a href="'+url+'" target="_blank">'+name+'</a>';
	}

	$.get('/blogs/load_last_comment/' + name + '/',
		{}, function(data) {
				if (data.ok) {
					comment = data.comment
				} else {
					comment = $('#id_comment').val();
				}

				comment_html = '<ol id="thread">\
					<li class="comment odd depth-1">\
					<div class="round comment-body new">\
						<div class="author">\
							<!--<img src="img/gravatar.png" alt="" />-->\
							<em><cite class="large"><span class="commenting_user">' + user_str + '</span></cite> says:</em>\
						</div>\
						<div class="meta small">\
							<em>0 minutes ago</em>\
						</div>\
						<p>' + comment + '</p>\
						<br />\
					</div>\
					</li>\
					  </ol>';
				
				$('#id_comment').val('');
				$('#comments').append(comment_html);
				$.scrollTo($('div .new'));
				$('div.comment:last').show('slow');
				$('div #comments').after('\
					<br /><div class="comment-thanks">\
						Thank you for your comment!\
					</div>\
				');
				$('div.comment-thanks').fadeOut(4000);
				$('#id_email').val('');
				$('#id_comment').val('');
				$('#id_name').val('');
				$('#id_url').val('');
				$('#id_check_if_true').removeAttr('checked');
				$('ul.errorlist').remove();
				//if ($('#auth_user').val() == 'none') {
					//Recaptcha.reload();
				//}
		},
		'json' )
}

function commentFailure(data) {
    prepare_comment_form();
	recaptcha_error = '';
    for (var error in data.errors) {
		$('#id_' + error).parent().before(data.errors[error]);
		//$('#break_' + error).remove();
		if (error = 'recaptcha') {
			recaptcha_error = error;
		}
    }
	if (recaptcha_error) {
		$('.form_label_recaptcha').before(data.errors[recaptcha_error]);
	}
}

function removeWaitAnimation() {
    $('.ajax-loader').remove();
    $('.ajax-loader-span').remove();
    $('div.comment-waiting').stop().remove();
}

