$(document).ready(function(){

    $("#rate_form").stars({
        inputType   : "select",
        cancelShow  : false,
        oneVoteOnly : true,
        disabled    : false,
        showTitles  :true,
        callback: function(ui, type, value)
        {
            $.ajax({
                type        : "POST",
                url         : saveRateUrl,
                cache       : false,
                data        : 'rate='+value,
                success: function(data){
                    var json = $.parseJSON(data);
                    if(json.status == 'exist'){
                        $("#rate_form").stars("select", json.msg.value);
                        $('.ratingBoxMsg').html(msgRatingExist);
                        $('.ratingBoxMsg').fadeIn(2000, function(){
                            $(this).fadeOut(2000);
                        });
                        return false;
                    }
                    if(json.status == 'error'){
                        if(json.msg.errorcode == 'LOGIN'){
                            $('.ratingBoxMsg').html(msgRatingLogin);
                            $('.ratingBoxMsg').fadeIn(2000, function(){
                                $(this).fadeOut(2000);
                            });
                        }
                        return false;
                    }
                    if(json.status == 'ok'){
                        $("#fake-stars-on").width(Math.round( $("#fake-stars-off").width() / ui.options.items * parseFloat(json.msg.avg) ));
                        $("#fake-stars-cap").text(json.msg.avg + " (" + json.msg.votes + ")");
                        $('.ratingBoxMsg').html(msgRatingAdded);
                        $('.ratingBoxMsg').fadeIn(2000, function(){
                            $(this).fadeOut(2000);
                        });
                        return false;
                    }
                          

                //                    $("#fake-stars-on").width(Math.round( $("#fake-stars-off").width() / ui.options.items * parseFloat(json.avg) ));
                //                    $("#fake-stars-cap").text(json.avg + " (" + json.votes + ")");
                }
            })

        }
    });

    $("#average_rating_form").stars({ // already disabled via markup
        inputType: "select",
        split: 1,
        cancelShow: false,
        captionEl: $("#averageRating-cap")
    });

})


