Raphael.fn.pieChart = function (cx, cy, r, values, labels, stroke) {
    var paper = this,
        rad = Math.PI / 180,
        chart = this.set();
    function sector(cx, cy, r, startAngle, endAngle, params) {
        var x1 = cx + r * Math.cos(-startAngle * rad),
            x2 = cx + r * Math.cos(-endAngle * rad),
            y1 = cy + r * Math.sin(-startAngle * rad),
            y2 = cy + r * Math.sin(-endAngle * rad);
        return paper.path(["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
    }
    var angle = 0,
        total = 0,
        start = 0,
        process = function (j) {
            var value = Math.abs(values[j]),
                angleplus = values.length==1?(359 * value / total):(360 * value / total),
                popangle = angle + (angleplus / 2),
                color = "hsb(" + start + ", 1, .5)",
                ms = 500,
                delta = 30,
                bcolor = "hsb(" + start + ", 1, 1)",
                p = sector(cx, cy, r, angle, angle + angleplus, {gradient: "90-" + bcolor + "-" + color, stroke: stroke, "stroke-width": 3}),
                txt = paper.text(cx + (r + delta + 55) * Math.cos(-popangle * rad), cy + (r + delta + 25) * Math.sin(-popangle * rad), labels[j]+"\n"+values[i]).
                	attr({fill: "#0AABEE", stroke: "none", opacity: 0, "font-family": 'Fontin-Sans, Arial', "font-size": "16px", "font-weight":"bold"});
                p.mouseover(function () {
                    p.animate({scale: [1.1, 1.1, cx, cy]}, ms, "elastic");
                    txt.animate({opacity: 1}, ms, "elastic");
                }).mouseout(function () {
                    p.animate({scale: [1, 1, cx, cy]}, ms, "elastic");
                    txt.animate({opacity: 0}, ms);
                });
            angle += angleplus;
            chart.push(p);
            chart.push(txt);
            start += .1;
        };
    for (var i = 0, ii = values.length; i < ii; i++) {
        total += Math.abs(values[i]);
    }
    for (var i = 0; i < ii; i++) {
        process(i);
    }
    return chart;
};

function buildPie(idSource) {
	
	var raphael = Raphael.ninja();
    var values = [],
        labels = [];
    var table = document.getElementById(idSource);
    var arr = table.getElementsByTagName('TR');
    for(i = 0; i < arr.length; ++i){
    	var row = arr[i].getElementsByTagName('TD');
    	labels.push(row[0].innerHTML);
    	values.push(parseInt(row[1].innerHTML, 10));
    }
	
    raphael(idSource+"holder", 600, 600).pieChart(300, 300, 140, values, labels, "#fff");
}