var Comments = {
    init : function() {
        this.post_id = post_id;
        this.type = type;
	this.allowed = true;
        this.getAll();
        $('#addNewComment').click(function() {
           Comments.addComment();
        });

    },

    getAll : function(type) {
        var data = {};
        data['action'] = 'get_all';
        data['type'] = Comments.type;;
        data['post_id'] = Comments.post_id;
        $.post('/comments', data, function(html) {
            $('#commentsHolder').html(html);
        }, 'html');
    },

    addComment : function(type) {
	if (!this.allowed) {
    		alert("Comments disabled");
            return;
    	}
        if (!$('#addCommentName').val() || !$('#addCommentText').val()) {
            alert("Please enter your name & comment");
            return;
        }
        $('#loading').show();
        var data = {};
        data['action'] = 'add_comment';
        data['type'] = Comments.type;
        data['post_id'] = Comments.post_id;
        data['name'] = $('#addCommentName').val();
        data['text'] = $('#addCommentText').val();

        $.post('/comments', data, function(html) {
	         $('#loading').hide();
            alert("Thank you for adding your comment!\nAfter moderation it will be displayed.");
            $('#addCommentName').val('');
            $('#addCommentText').val('');
        }, 'html');
    }
}

$(document).ready(function() {
   Comments.init();
});

