<!-- varjovaali ajax starts, mostly jquery (www.jquery.com) based -->

    // manipulates 'ehdokaslista' votebuttons accordingly
    // eid == ehdokas id
    // votes == ehdokas votes or null
    function setAanestysButtons(eid, votes) {
        // set voted ehdokas div

        if(votes == null) {
            // grab current votes from html if not known
            votes = $("#ehdokas" + eid + "_currentvotes").html();
        }
        // toggle voted div
        var votetd = $("#ehdokas" + eid + "_vote");
        votetd.html("<span class=\"candidate_votes\">" + votes + "</span>\nääntä");
        votetd.removeClass("candidate_vote");
        votetd.addClass("candidate_vote_locked_voted");

        // set other divs
        //
        // all shadowelection_element classed elements in div with id ehdokaslista
        //  whose id element is not the voted one

        $("#ehdokaslista div.shadowelection_element[id!=ehdokas" + eid + "]").each(function() {
            votes = $("#" + this.id + "_currentvotes").text();
            votetd = $("#" + this.id + "_vote");
            votetd.removeClass("candidate_vote");
            votetd.addClass("candidate_vote_locked");
            votetd.html("<span class=\"candidate_votes\">" + votes + "</span>\nääntä");
        });
    }

    // makes the ajax call when voted
    function aanesta(servletPath, sivusto, vaali, kunta, ehdokasId, arvo) {
        $.ajax({
            url: servletPath,
            type: "POST",
            dataType: "xml",
            data: "sivusto=" + encodeURIComponent(sivusto)
                  + "&vaali=" + encodeURIComponent(vaali)
                  + "&kunta=" + encodeURIComponent(kunta)
                  + "&eid=" + encodeURIComponent(ehdokasId)
                  + "&arvo=" + encodeURIComponent(arvo),
            timeout: 10000,
            error: function() {
                alert('Äänestys epäonnistui. Yritä myöhemmin uudelleen.');
            },
            success: function(xml) {
                var eid = $(xml).find('eid').text();
                var votes = $(xml).find('votes').text();
                var totalVotes = $(xml).find('totalvotes').text();

                $("span.totalvotecount").text(totalVotes);

                $("#shadowelection_vote_casted_placeholder").html(
                    "<div id='shadowelection_vote_casted'>Olet jo äänestänyt tänään tässä kunnassa. Voit äänestää uudelleen huomenna.</div>"
                );

                setAanestysButtons(eid, votes);
            }
        });
    }

    // initializes varjovaalipages' dynamic elements when necessary
    function setVarjovaaliPageContent(totalVotes) {
        // set additional static vars
        if(totalVotes != null) {
            // sets total vote count value for all elements with class 'totalvotecount'
            $("span.totalvotecount").text(totalVotes);
        }
    }

    // opens tool window for generating the html code for external elements
    function openToolwindow(url) {
        window.open(url, null, 'width=600, height=300');
        return false;
    }

<!-- varjovaali ajax ends -->

<!-- generic voting ajax starts -->

// makes the ajax call when voted

//    sivusto=publication
//    vaali=listausartikkelin home-sektio
//    sektio=aanestetyn artikkelin sektio
//    ehdokasId=aanestetty ehdokas
//    arvo=äänen arvo, +1 tai -1
function v_aanesta(servletPath, sivusto, vaali, sektio, ehdokasId, arvo, plusOnly) {
    var plusOnlyV = plusOnly;
    var vaaliV = vaali;
    $.ajax({
        url: servletPath,
        type: "POST",
        dataType: "xml",
        data: "sivusto=" + encodeURIComponent(sivusto)
              + "&vaali=" + encodeURIComponent(vaali)
              + "&sektio=" + encodeURIComponent(sektio)
              + "&eid=" + encodeURIComponent(ehdokasId)
              + "&arvo=" + encodeURIComponent(arvo),
        timeout: 10000,
        error: function() {
            alert('Äänestys epäonnistui. Yritä myöhemmin uudelleen.');
        },
        success: function(xml) {
            var eid = $(xml).find('eid').text();
            var arvo = $(xml).find('arvo').text();
            var plusVotes = $(xml).find('plusvotes').text();
            var minusVotes = $(xml).find('minusvotes').text();
            var sumVotes = $(xml).find('sumvotes').text();
            var votesLeft = $(xml).find('votesleft').text();
            var totalVotes = $(xml).find('totalvotes').text();
            var votesLeftSection = null;
            if($(xml).find('leftsection') != null)
                votesLeftSection = $(xml).find('leftsection').text();
            var votesLeftPlus = $(xml).find('votesleftplus').text();
            var votesLeftMinus = $(xml).find('votesleftminus').text();

            setVotingPageContent(vaaliV, totalVotes);
            v_setAanestysButtons(vaaliV, eid, plusVotes, minusVotes,
                                 sumVotes, arvo, votesLeft, plusOnly,
                                 sektio, votesLeftSection, votesLeftPlus, votesLeftMinus);
        }
    });
}

//// manipulates 'ehdokaslista' votebuttons accordingly

function v_setAanestysButtons(vaali, eid, plusVotes, minusVotes, sumVotes, arvo, votesLeft, plusOnly, sektio, votesLeftSection, votesLeftPlus, votesLeftMinus) {

//    alert('set ' + eid + ' ' + plusVotes + ' ' + minusVotes + ' ' + sumVotes + ' ' + arvo + ' ' + votesLeft + ' ' + plusOnly);

    //// voted candidate
    //
    // set cand div class according to the vote given
    var votetd = $("#v_cand" + eid);
    votetd.removeClass('v_cand');
    votetd.removeClass('v_candvoted');
    votetd.removeClass('v_candvotedplus');
    votetd.removeClass('v_candvotedminus');
    if(arvo > 0)
        votetd.addClass('v_candvotedplus');     // v_candvotedplus class on + voted elements
    else if(arvo < 0)
        votetd.addClass('v_candvotedminus');    // v_candvotedminus class on - voted elements
    else
        votetd.addClass('v_cand');              // v_cand on elements not voted

    var vleft;
    // update vote counts
    if(plusOnly) {
        $("#v_cand" + eid + " div.v_candvotebutton_votes").html(sumVotes + (sumVotes == 1 ? " ääni" : " ääntä"));
        vleft = "Sinulla on vielä " + votesLeft + (votesLeft == 1 ? " ääni" : " ääntä") + " annettavana.";
    } else {
        $("#v_cand" + eid + " div.v_candvotebutton_votes").html(sumVotes);
        vleft = "Sinulla on vielä " + votesLeft + (votesLeft == 1 ? " ääni" : " ääntä") + " annettavana " +
                "(" + votesLeftPlus + " plus-" + (votesLeftPlus == 1 ? "ääni" : "ääntä") + " ja "
                + votesLeftMinus + " miinus-" + (votesLeftMinus == 1 ? "ääni" : "ääntä") + ").";
        $("#v_cand" + eid + " div.v_candvotebutton_votesdetail").html(
                "(" + plusVotes + " +" + (plusVotes == 1 ? "ääni" :"ääntä") + " ja " + minusVotes + " -" + (minusVotes == 1 ? "ääni" : "ääntä") + ")"
        );
        if(votesLeftPlus == '0') {
            $(".v_list div.v_candvotebutton_voteplus").fadeOut(500);
        }
        if(votesLeftMinus == '0') {
            $(".v_list div.v_candvotebutton_voteminus").fadeOut(500);
        }
    }

    if(votesLeftSection == null || votesLeftSection == '') {
        $("#" + vaali + "_v_votesleft").html(vleft);
    }else{
        $("#" + sektio + "_v_votesleft").html(vleft);
    }

    //// rest of the candidates on the list

    // select all divs with class v_cand, v_candvoted, v_candvotedplus or v_candvotedminus below
    //  v_list classed div which id does not match the voted one
    $(".v_list > div.v_cand[id!=v_cand" + eid + "]," +
      ".v_list > div.v_candvoted[id!=v_cand" + eid + "]," +
      ".v_list > div.v_candvotedplus[id!=v_cand" + eid + "]," +
      ".v_list > div.v_candvotedminus[id!=v_cand" + eid + "]"
     ).each(function() {
        if(votesLeft > 0) {
            // if there are still votes left to cast update the tooltips to match
            $("#" + this.id + " div.v_candvotebutton_voteplus a").attr("title", vleft);
            if(!plusOnly)
                $("#" + this.id + " div.v_candvotebutton_voteminus a").attr("title", vleft);
        } else {
            // no votes left, change v_cand to v_candvoted
            var jqelem = $(this);
            if(jqelem.hasClass('v_cand') && jqelem.hasClass('v_cand'+sektio)) {
                jqelem.removeClass('v_cand');
                jqelem.removeClass('v_cand'+sektio);
                jqelem.addClass('v_candvoted');
            }
        }
    });
}

function v_aanesta_login() {
    $('#votelogin').dialog(
        {
            autoOpen: true,
            modal: true,
            resizable: true,
            height: 250
        }
    );
}

// initializes  dynamic elements when necessary
function setVotingPageContent(vaali, totalVotes) {
    // set additional static vars
    if(totalVotes != null) {
        // sets total vote count value for all elements with class 'totalvotecount'
        $("span." + vaali + "_totalvotecount").text(totalVotes);
    }
}

<!-- generic voting ajax ends -->



