var options;

var commentContainer;
var jData;
$(document).ready(function() {
    commentContainer = $('#comment-form-container');
    commentContainer.hide();
    $('a#addcomment').click(function() { 
        commentContainer.show(400); 
        return false;
    });
    $('#comment-form-errors').hide();
    $('#comment-form-preview').hide();
    commentContainer.children('form').ajaxForm(options);
}
);

function setLoading() {
    
}

function handleResponse(data, statusText) {
    jData = data;
    $('#comment-form-errors').hide();
    $('#comment-form-preview').hide();
    if(data.comment) {
        var commentList = $('#comment-list');
        if($('#no-comments').length == 0) {
            commentList.append(data.comment);
        } else {
            commentList.html(data.comment);
        }
        $('<p>Thank you for your comment.</p>').appendTo(document.body).dialog();
        commentContainer.hide(400);
    } else if(data.preview) {
        $('#comment-form-preview').html(data.preview).show();
    } else if(data.errors) {
        $('#comment-form-errors').html('Please enter a comment').show();
    }
}

options = {
    'beforeSubmit':setLoading,
    'success': handleResponse,
    'dataType': 'json'
}
