/**
 * Pre-req:
 *      jquery.js (source: jquery.com)
 *      jquery.validate.js (source: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
 *                          http://plugins.jquery.com/project/validate
 * Usage:
 *          Comments.init('#leave-a-comment-form-id', '#comments-content-id', '.comments-count-class', 'comments-template');
 *
 *          DON'T FORGET:
 *              - to include:
 *                  comments content container
 *                  comments count container
 *                into your HTML
 *              - attend to all TODO's
 */


var Comments = {
    /**
     * url to comments appengine application
     *
     * TODO change url when deploying
     */
    API_COMMENTS: "http://site-comments.appspot.com",
    //API_COMMENTS: "http://localhost:8080",

    init: function(leave_a_comment_form, comments, comments_count, comments_template) {
        /**
         * Descr:       initialize comments application
         * Usage:       Comments.init(leave_a_comment_form, comments, comments_count, comments_template);
         *
         *              leave_a_comment_form - css id selector - element id of leave a comment form
         *              comments - css id selector - the place to insert comments for this page
         *              comments_count - css class selector - the place to put the number of comments for this page
         *              comments_template - string - selects the template of comments to be returned by appengine
         * Return:      returns nothing
         */
        $(document).ready(function() {
            //$.validator.setDefaults({
                //debug: true /* TODO change to false when deploying */
            //});

            // load all comments for this page
            Comments._load_comments(location.href, comments, comments_count, comments_template);

            // add necessary details before webservice call to create a new comment
            $(leave_a_comment_form).
                attr('action', Comments.API_COMMENTS + '/create').
                append('<input type="hidden" name="url" value="' + location.href + '">').
                append('<input type="hidden" name="rm" value="webservice">');

            // validate form
            var options = {
                rules: {
                    author: "required",
                    title: "required",
                    content: "required"
                },
                messages: {
                    author: "Please leave your name",
                    title: "Please give your comment a title",
                    content: "Please leave your comment"
                },
                errorClass: "errorBox"
            };
            $(leave_a_comment_form).validate(options);
        });
    },
    _load_comments: function(this_page, comments, comments_count, comments_template) {
        /**
         * Descr:       insert all comments into the DOM of current page/article via API_COMMENTS
         * Usage:       Do not call directly
         *              Comments.load_comments(location.href)
         * Return:      returns nothing
         */
        //param = {rm: 'webservice', callback: '?', url: this_page};
        $.getJSON(this.API_COMMENTS + "/?rm=webservice&callback=?&tp=" + comments_template + "&url=" + this_page,
            function(comments_json) {
                $(comments).html(comments_json.html);
                $(comments_count).text("" + comments_json.count);
        });
    }
};
//vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
