var maxStars = 5;

function starsOn(nid, rating)
{
        for(i = 1; i <= maxStars; i++){
                if(i <= rating){
                        document.getElementById("_" + i + "_" + nid).className = "starRated";
                } else {
                        document.getElementById("_" + i + "_" + nid).className = "";
                }
        }
}

function starsRated(nid)
{
        var rating = document.getElementById("ratingFlag_" + nid).className.substring(7);
        var starClass = document.getElementById("ratingStars_" + nid).className;

        for(i = 1; i <= maxStars; i++){
                if(i <= rating){
                        document.getElementById("_" + i + "_" + nid).className = starClass
                } else {
                        document.getElementById("_" + i + "_" + nid).className = "starOff";
                }
        }
}

function starRate(type, id, rating)
{
        //set rating flag on page
        document.getElementById("ratingStars_" + id).className = "starRated";

        //send rating
        document.getElementById("ratingFlag_" + id).className = "rating_" + rating;
        starsRated(id);

        //display saving message
        showLayerInline("savingRating_" + id);
        hideLayer("ratingStars_" + id);

        var xmlhttp =  new XMLHttpRequest();
        xmlhttp.open("POST", "/rate/" + type + "/" + id + "/" + rating, true);
        xmlhttp.send(null);

        setTimeout("hideLayer(\"savingRating_" + id + "\")", 1000);
        setTimeout("showLayerBlock(\"ratingStars_" + id + "\")", 1000);
}

function starsOff(nid)
{
        if(document.getElementById("ratingFlag_" + nid).className == "rating_0")
        {
                for(i = 1; i <= maxStars; i++)
                {
                        document.getElementById("_" + i + "_" + nid).className = "starOff";
                }
        } else {
                starsRated(nid);
        }
}

