$().ready(function(){
    function voteThis2() {
        var eid = $(this).parent().parent().attr("data-eid");
        var et = $(this).parent().parent().attr("data-et");
        var aid = $(this).parent().parent().attr("data-aid"); 
        var secs = $(this).parent().parent().attr("data-secs"); 

        $(this).parent().children(".vote-plus, .vote-minus").addClass("vote-disabled2");

        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) {}
                else {
                    pointsToAdd = parseInt(json.pts);
                    votesCount = parseInt(json.votes);
                }
            }
        });
        if (pointsToAdd) {
            var wherewehave = $(this).parent().children(".vote");        
            var wehave = parseInt(wherewehave.html());
            var result = pointsToAdd + wehave;
            var resultPrint = result;

            wherewehave.html(resultPrint);
            leftCommentsWidget(votesCount);
        }
        $(this).parent().children(".vote-plus, .vote-minus").unbind("click", voteThis2);
    }
    
    $("div[data-w='1'].tools2 .vote-plus, div[data-w='1'].tools2  .vote-minus").bind("click", voteThis2);

    function voteThis3() {
        var eid = $(this).parent().parent().attr("data-eid");
        var et = $(this).parent().parent().attr("data-et");
        var aid = $(this).parent().parent().attr("data-aid"); 
        var secs = $(this).parent().parent().attr("data-secs"); 

        $(this).parent().children(".vote-plus, .vote-minus").addClass("vote-disabled2");

        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) {}
                else {
                    pointsToAdd = parseInt(json.pts);
                    votesCount = parseInt(json.votes);
                }
            }
        });
        if (pointsToAdd) {
            var wherewehave = $(this).parent().children(".vote");        
            var wehave = parseInt(wherewehave.html());
            var result = pointsToAdd + wehave;
            var resultPrint = result;
            
            var res = Math.abs(result);
            if ((res % 100 > 10 && res % 100 < 20) || (res % 10 == 0) || (res % 10 > 4)) { resultPrint = resultPrint + ' голосов'; }
            else if (res % 10 == 1) { resultPrint = resultPrint + ' голос'; }
            else if (res % 10 == 2 || res % 10 == 3 || res % 10 == 4) { resultPrint = resultPrint + ' голоса'; }
            
            wherewehave.html(resultPrint);
            leftCommentsWidget(votesCount);
        }
        $(this).parent().children(".vote-plus, .vote-minus").unbind("click", voteThis3);
    }
     
    $("span[data-w='1'].tools3 .vote-plus, span[data-w='1'].tools3 .vote-minus").bind("click", voteThis3);

    function voteThis4() {
        var eid = $(this).parent().parent().attr("data-eid");
        var et = $(this).parent().parent().attr("data-et");
        var aid = $(this).parent().parent().attr("data-aid"); 
        var secs = $(this).parent().parent().attr("data-secs"); 

        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) {}
                else {
                    pointsToAdd = parseInt(json.pts);
                    votesCount = parseInt(json.votes);
                }
            }
        });
        if (pointsToAdd) {
            var wherewehave = $(this).parent().children(".vote");        
            var wehave = parseInt(wherewehave.html());
            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");
        }
        
    }
    
    $("div.voteWidget4 .votebtn").bind("click", voteThis4);

});
