// javascript document

$(document).ready(function() {
    $('.nooga_comment_reply_prototype').find('.CommentCancel').hide();
});

function formSubmit(target) {
    var $form = $(target).addClass('processing').ajaxSubmit({
        success: function(html) {
            alert("Your comment has been submitted for moderation");
            window.location.reload();
        },
        error: function(xhr, status, error) {
            $form.addClass('error').removeClass('processing');
        }
    });
};

$('button.nooga_comment_comment_reply_show_form').live('click', function() {
    var $button = $(this);
    var $container = $button.parent().addClass('replying');
    $('.nooga_comment_reply_prototype').find('.CommentCancel').show();
    var $reply = $('div.nooga_comment_reply_prototype').clone()
        .removeClass('nooga_comment_reply_prototype')
        .find('.nooga_comment_reply_name_placeholder').text('@'+$button.attr('data-name')).end()
        .find('.nooga_comment_comment_form').attr('action', $button.attr('data-url')).end()
        .find('.nooga_comment_reply_cancel').click(function() {
            $reply.remove();
            $container.removeClass('replying');
        }).end()
        .appendTo($container)
        .find('textarea').focus().end();
    $('.nooga_comment_reply_prototype').find('.CommentCancel').hide();
});


$('button.nooga_comment_comment_loadmore_load').live('click', function() {
    var $button = $(this);
    var $container = $button.parent();

    $container.load($button.attr('data-url'));
});

$('button.nooga_comment_comment_vote').live('click', function(e) {
    var $button = $(this);
    var $container = $button.parent();
    var $score = $container.find('.nooga_comment_comment_score');
    if(!$(e.target).hasClass('clicked')) {
        $.getJSON($button.attr('data-url'), function(data) {
            if(data.error) {
                alert(data.error);
            } 
            else if(data.score == 1) {
                text = 'person likes this comment';
                $score.text(data.score+" "+text);
            }
            else if(data.score > 1) {
                text = 'people like this comment';
                $score.text(data.score+" "+text);
            } 
            
            if(typeof noogaMobile !== "undefined")
            {
                noogaMobile.updateArticleLikeCountText();
            }
        });
    }
    $(e.target).addClass('clicked');
    $(this).hide();
});

