$().ready(function(){
    $(".comment dt.votebtn.anonymous").live("click", function() {
        $("#needRegisterWnd").show();
        setTimeout('$("#needRegisterWnd").hide()', 2500);
    });

    $(".comment dt.votebtn:not(.disabled):not(.anonymous)").live("click", function() {
        
        var eid = $(this).parent().parent().attr("id").slice(3);
        var aid = $(this).parent().parent().attr("data-aid");
        var secs = $("div[data-secs]").attr("data-secs");
        var et = "comments";

        var pointsToAdd = 0;
        var votesCount = 0;

        $.ajax({
            type: "POST",
            url: "/modules/votes/vote.php",
            data: "action=addvote&eid=" + eid + "&et=" + et + "&aid=" + aid + "&secs=" + secs,
            async: false,
            dataType: "json",
            cache: false,
            error: function(q,w,e) { },
            success: function(json){
                if (!json.error) { 
                    pointsToAdd = parseInt(json.pts); 
                    votesCount = parseInt(json.votes);
                }
                else if (json.error == 110) {
                    $("#errorWnd").show();
                    setTimeout('$("#errorWnd").hide()', 2500);
                }
            }
        });
        
        if (pointsToAdd) {
            var wherewehave = $(this).parent().children("dt.vote");
            var wehave = parseInt(wherewehave.html());
            if (isNaN(wehave)) {
                wehave = 0;
            }

            var result = pointsToAdd + wehave;
            var resultPrint = result;
            if (result > 0) {
                resultPrint = "+" + result;
            }

            wherewehave.toggleClass("green", (result>0));
            
            wherewehave.html(resultPrint);

            leftCommentsWidget(votesCount);

            $(this).parent().children("dt.votebtn").addClass("disabled").unbind("click");
        }
    });
});
