var beController = beController || {};
beController.blogPost = function(postId) {

    var com = new Element('div', {
        html: '<h4>Add Commentary</h4><form id="comForm"><label for="name">Your Name:</label><input type="text" id="name" name="name" value="Anonymous" /><br /><label for="website">Website (optional):</label><input type="text" id="website" name="website" /><div id="comTextCon"><a href="#" id="writeComment" class="writeComSelect">Comment</a><a href="#" id="previewComment">Preview</a><textarea id="comWrite"></textarea><div id="comPreview"></div></div><button id="addCommentB">Add Comment</button></form>',
        id: 'addComment'
    });
    com.inject($('commentary'), 'bottom');
    
    var preview = $('previewComment');
    var write = $('writeComment');

    preview.addEvent('click', function(event) {
        event.stop();
        write.removeClass('writeComSelect');
        preview.addClass('writeComSelect');
        var req = new Request.HTML({url:'/comment/preview', update: $('comPreview'),
        onFailure: function() {
            alert("I'm sorry, there was an issue previewing your comment. Please try again");
        }
        }).post('comment=' + $('comWrite').get('value'));
        req.send();
        $('comPreview').setStyle('display', 'block');
        $('comWrite').setStyle('display', 'none');
    });

    write.addEvent('click', function(event) {
        event.stop();
        preview.removeClass('writeComSelect');
        write.addClass('writeComSelect');
        $('comPreview').setStyle('display', 'none');
        $('comWrite').setStyle('display', 'block');
    });

    $('addCommentB').addEvent('click', function(event) {
        event.stop();
        
        var req = new Request.JSON({url:'/comment/submit',
        onFailure: function() {
            alert("I'm sorry, there was an issue submitting your comment. Please try again");
        },
        onSuccess: function(response) {
            if (response.message == 'ok') {
                alert('Your comment was submitted, and if it is not spam, will be approved in the next couple of days');
            } else {
                alert('Your comment could not be submitted. Are all the required fields filled out?');
            }
        }
        }).post('comment=' + $('comWrite').get('value') + '&name=' + $('name').get('value') + '&website=' + $('website').get('value') + '&blogPost=' + postId);
    });
}

