$(document).ready(function(){
	
	$('#postCommentBtn').click(function(){
		
		$.ajax({
			type: 'post',
			url: 'blog/comments/add/ajax',
			data: $('#addCommentForm').serialize(),
			dataType: 'json',
			success: function(response) {
				if(response.code == 1) {
					
					if(response.url != 'http://') {
						link = $('<a></a>').html(response.name).attr('href',response.url).attr('rel','nofollow').attr('target','_blank');
					} else {
						link = response.name;
					}
					
					var blog_comment = $('<div>').addClass('blog_comment');
					blog_comment.append(
						$('<div>').addClass('blog_comment_name').html(link).append(' <span>'+response.created+'</span>')
					);
					blog_comment.append(
						$('<div>').addClass('blog_comment_body').html(response.body)
					);
					blog_comment.hide();
					
					$('.blog_comments').append(blog_comment);
					blog_comment.fadeIn("slow");
					$('#addCommentForm').hide();
					$('.blog_thanks').show();
					
					$('#blogname').val("");
					$('#blogurl').val("");
					$('#blogemail').val("");
					$('#blogbody').val("");
					$('#blogbody2').val("");
					
				} else {
					blogCommentError(response.error);
				}
			},
			error: function() {
				alert("An unknown error occurred, please try again");	
			}
		});
		
		return false;
		
	});
	
	$('.blog_thanks').click(function(){
		$('#addCommentForm').show();
		$(this).hide();
	});
	
});

function blogCommentError(errorMsg) {
	alert(errorMsg);
}