<?php $unique_id = $post_id; ?>
<div class="star rating_<?php echo $unique_id ?>" style="cursor: pointer; width: 100px;"></div>

<?php if ($user_can_rate): ?>
    <script type="text/javascript">
        var rating_object_<?php echo $unique_id ?>=null;
        var block_rate_<?php echo $unique_id ?>=false;
        jQuery(function(){
            rating_object_<?php echo $unique_id ?>=jQuery('.rating_<?php echo $unique_id ?>');

            jQuery(rating_object_<?php echo $unique_id ?>).raty({
                half:  true,
                number:<?php echo $max_rating ?>,
                score: parseFloat(<?php echo $rating ?>),
                click: function(score, evt) {
                    if(block_rate_<?php echo $unique_id ?>){
                        return;
                    }
                    block_rate_<?php echo $unique_id ?>=true;
                    var self=this;
                    var data = {
                        action: "set_rate",
                        score: score,
                        post_id: <?php echo $post_id ?>,
                    };
                    //send data to server
                    jQuery.post(ajaxurl, data, function(new_score) {
                        jQuery(self).raty({
                            readOnly: true,
                            score:new_score,
                            number:<?php echo $max_rating ?>,
                        });
                    });
                }
            });
        });
    </script>
<?php else: ?>
    <script type="text/javascript">
        jQuery(function(){
            jQuery('.rating_<?php echo $unique_id ?>').raty({
                half:  true,
                readOnly: true,
                number:<?php echo $max_rating ?>,
                score: parseFloat(<?php echo $rating ?>),
            });
        });
    </script>
<?php endif; ?>
