﻿$(document).ready(function () {

    /* Get the latest Post (with Comments) and rendered using jQuery Template */
    $.get('/Blog/GetPost', function (data) {
        var post = data.post;
        // This is to be used for adding new comment to the main-page blog post
        $('#PostId').val(post.Id);

        if (data.userType > 1) {
            $.get('/Content/Templates/_postWithCommentsAdmin.tmpl.htm', function (templates) {
                $('body').append(templates);
                $('#blogTemplate').tmpl(post).appendTo('#blogPost');
            });
        }
        else {
            $.get('/Content/Templates/_postWithComments.tmpl.htm', function (templates) {
                $('body').append(templates);
                $('#blogTemplate').tmpl(post).appendTo('#blogPost');
            });
        }
    });

    /* Add a new comment */
    $('#commentForm').live('submit', function () {
        tinyMCE.triggerSave(true, true);
        $(this).ajaxSubmit({
            success: function (result) {
                if (result.success) {
                    $('#commentForm')[0].reset();
                    var newComment = { comment: result.comment };
                    // Append the new comment to the div
                    $('#commentTemplate').tmpl(result.comment).appendTo('#commentsTemplate');
                }
                $('#commentFormStatus').text(result.message);
            }
        });
        return false;
    });
});
