function voting(id, url, success_handler){
    $.ajax({
        'type':'POST',
        'url':url,
        'success':success_handler,
        'dataType':'json'
    });
}

function refresh_scores(id, score){
    if(!score){return}
    $('#score_result__'+id+' .voting_block')
        .removeClass('voting_positive')
        .removeClass('voting_negative')
        .removeClass('voting_zero');
    
    if(score.score < 0){
        $('#score_result__'+id+' .voting_block').addClass('voting_negative');
    } else if (score.score > 0){
        $('#score_result__'+id+' .voting_block').addClass('voting_positive');
    } else {
        $('#score_result__'+id+' .voting_block').addClass('voting_zero');
    }
    $('#score_result__'+id+' .voting_score').html(score.score+'');
    $('#score_result__'+id+' .voting_num_votes').html(score.num_votes+'');
    
    show_hide_comments();
}


function post_voting(id, app_label, model, direction){
    var url = "/voting/"+app_label+"/"+model+"/"+id+"/"+direction+"/";
    
    voting(id,
           url,
        function(data){
            refresh_scores(id, data.score)
        });
}

function show_hide_comments(){
    $('.comment-item').each(function(){
        var score = parseInt($('.voting_score',$(this)).html());
        var min_votes = VISIBLE_COMMENT_MIN_VOTES;
        if (isNaN(min_votes)){
            min=-5;
        }
        if (score<min_votes) {
            $('.comment-body, .comment-commentform',$(this)).hide();
            $('.ellipsis',$(this))
                .show()
                .click(function(e){
                    $(this).hide();
                    $(this).prev('div .comment-body').show();
                    $(this).next('div .comment-commentform').show();
                });
                
        } else {
            $('.comment-body, .comment-commentform',$(this)).show();
            $('.ellipsis',$(this)).hide();
        }
    });
}

$(function(){
    show_hide_comments();

    $("a.create-comment").click(function(e){
        $(this).hide();
        var form = $(this).next('form'); 
        $('textarea', form).markItUp(mySettings);
        var markItUp = $('.markItUp', form);
        $(".markItUpButton:gt(10)", markItUp).hide();
        
        var moreButton = $("<a>").html('>>')
                        .attr('href','javascript:void(0)')
                        //.attr('style','clear:both;text-align:left')
                        .click(function(){
            $(".markItUpButton:gt(10)", markItUp).show();
            $(this).hide();
        });
        moreButton.appendTo($('.markItUpHeader', markItUp));
        
        form.show();
        e.preventDefault();
    });    

    $(".delete-node-link").click(function(e){
        var yes = confirm("Удалить этот комментарий?");
        if (yes){
            var nodeId = $(this).attr('id').split('-')[1];
            $.post("/treebeardcomments/unapprove/"+nodeId+"/",
                   {'ok':'ok'},
                   function(data){
                       window.location = window.location;
                   });
        }
    });
    
});

