$(function() {
    
    var url = "http://prout.webatu.com/free_ajax.php",
        obj = new Object,
        html = '<div class="box" align="left"><a href="#" class="love" id="l_"><span class="on_img" align="left"></span></a></div>';
    
    obj.blog = blog;
    
    obj.action = "lire";
    obj.id = new Array;
    
    $("#container > .article").each(function(i) {
        $this = $(this);
        
        $id = $this.attr("id");
        
        if ($id.substr(0, 2) == "p_") {
            // On récupère l'id de l'article
            obj.id[i] = $this.attr("id").substr(2);
            
            $(".article-bottom", $this).after('<div class="box" align="center"><a href="#" class="love" id="l_' + obj.id[i] + '" title="J\'aime cet article"><span class="on_img" align="left"></span></a></div>');
        }
    });
    
    var encode = $.toJSON(obj);
    
    $.ajax({
        type: "POST",
        url: url,
        data: {
            data: encode
        },
        dataType : "json",
        success: function(data) {
            $.each(data.votes, function(i, article) {                
                $("span.on_img").eq(i).html(article.vote);
            });
            
        }
    });
    
    $(".love").live("click", function() {
        $this = $(this);
        obj.id = $(this).attr("id").substr(2);
        obj.action = "voter";
        
        var encode = $.toJSON(obj);
        
        $(this).fadeOut(300);
        
        $.ajax({
            type: "POST",
            url: url,
            data: {
                data: encode
            },
            dataType : "json",
            success: function(data) {
                if (data.message == "ok") {
                    $("span.on_img", $this).html(data.vote);
                } else {
                    $("span.on_img", $this)
                        .append(" " + data.message)
                        .effect("pulsate", { times: 3, duration: 50 });
                }
                $this.fadeIn(300);
            }
        });
        
        return false;
    });

});