// This is my jquery rating system which has been abstracted to work to rate anything (Articles, Additional Information, Comments)

$(document).ready(function()
{
    $(".rateLink").click(function(e) 
    {
        e.preventDefault();
        var content = []; //makes content an array because I will store a tokenized value in it.
        var thisUrl = $(this).attr("href"); //url of button clicked on
        content = $(this).attr("name").split(/_/);  //type of content being rated
        var val = content[0];
        if (content[1] != undefined)
            var myType = content[1]; //This will tell the jquery which div to update
        else
            var myType = "";
            
        var thisImg = $(this);
//         alert(thisUrl);
            $.ajax
            ({
                type: "GET",
                url:  thisUrl + "&ajax=set",
                success: function(dat) {
                    
//                     var pieces = new Array();
//                     alert(dat);
                    var pieces = dat.split(/,/);
                    var articleRating = pieces[0];
                    var userRating = pieces[1];
                    
                    if (articleRating == "no")
                    {
                        
                    }
                    else if (articleRating == "login")
                    {
                        alert("You must be logged in to rate this article.");
                    }
                    else
                    {
                        $("#ratingText" + myType + "" + val).html(articleRating);
                    }
                    
                    if (userRating == 1)
                    {
                        $("#rateImgUp" + val).attr("src", docRoot + "images/upClicked.png");
                    }

                    if (userRating == -1)
                    {
                        $("#rateImgDown" + val).attr("src", docRoot + "images/downClicked.png");
                    }
                    
                    if (userRating == 0)
                    {
                        $("#rateImgUp" + val).attr("src", docRoot + "images/up.png");
                        $("#rateImgDown" + val).attr("src", docRoot + "images/down.png");
                    }
                }
//                 error: function() {
//                     window.location = $(".rateLink").attr("href");
//                 }
            })
    });
});
