var CQ_collab_ratings_defaultMessage = ""; // overlay in page
var CQ_collab_ratings_allowComments = true; // overlay in page

var counterToAdd; // overlay in page

function CQ_collab_ratings_setRating(formAction, id, rating) {

    
    document.getElementById(id + "-rating").value = (rating * 20);
    
    //rating formAction once session
    if ($.cookie(formAction) == null) {
        $.cookie(formAction, formAction);
    }
    else {
        return;
    }

    $.ajax({
       type: "POST",
       url: formAction,
       data: $("#"+id+"-form").serialize(),
       success: function(msg){
            var i = 1;
            while (i <= 5) {
                document.getElementById(id + "-star-" + i).className =
                    "ratings-star " + (i <= rating ? "full" : "empty");
                i++;
            }
       }
     });

}


function updateFingeredCounter(ratingId) {
    var counter;
    
    if (ratingId == fingerUpId) {
        counter = ++fingerUp;
    } else if (ratingId == fingerDownId) {
        counter = ++fingerDown;
    } else {
        return false;
    }

    document.getElementById(ratingId).innerHTML = counter; 
}

function CQ_collab_ratings_setRatingFingered(formAction, id, rating, ratingId) {
    
    document.getElementById(id + "-rating").value = rating;
    
    //rating formAction once session
    if ($.cookie(formAction) == null) {
        $.cookie(formAction, formAction);
    }
    else {
        return;
    }

    $.ajax({
       type: "POST",
       url: formAction,
       data: $("#"+id+"-form").serialize(),
       success: function(msg){
                    updateFingeredCounter(ratingId);
                }
    });
}


// comments overrides

function CQ_collab_comments_validateFields(id) {
    // Validate text
    var tField = document.getElementById(id + "-text");
    var rField = document.getElementById(id + "-rating");
    if (CQ_collab_ratings_allowComments) {
        if ((tField.value == "" || tField.value == CQ_collab_comments_getDefaultMessage()) && !rField.value) {
            CQ_collab_comments_showError('Please provide either a rating or a comment', id + "-text");
            return false;
        }
    } else if (!rField.value) {
        CQ_collab_comments_showError('Please provide a rating', id + "-text");
        return false;
    }
    if (CQ_collab_ratings_allowComments && rField.value &&
            tField.value == CQ_collab_comments_getDefaultMessage()) {
        tField.value = "";
    }
    return true;
}

function CQ_collab_comments_getDefaultMessage() {
    return CQ_collab_ratings_defaultMessage;
}

