$(function(){
	var $tables = $('.statistics-table');
	var $charts = $('.statistics-piechart');

	if($tables.length > 0){
		$tables.hide();

		$tables.each(function(){
			var $table = $(this);
			var tableData = $table.data('chart');
			var $chart = $charts.filter('[data-chart="' + tableData + '"]');

			var values = [],
				labels = [];

			$table.find('tr').each(function (){
				values.push(parseInt($("td", this).text(), 10));
				labels.push("%% - " + $("th", this).text());
			});


			var r = Raphael($chart.get(0), 460, 200);
			r.g.txtattr.font = "13px Verdana, Tahoma, Arial, sans-serif";

			var pie = r.g.piechart(
				370, 90, 80,
				values,
				{
					legend: labels,
					legendpos: "west",
					legendcolor: "#444",
					strokewidth: 2,
				});

			pie.hover(function () {
				this.sector.stop();
				this.sector.animate({ scale: [1.1, 1.1, this.cx, this.cy] }, 500, "elastic");
				if (this.label) {
					this.label[0].stop();
					this.label[0].scale(1.2);
					this.label[1].attr({"font-weight": 800});
				}
			}, function () {
				this.sector.animate({ scale: [1, 1, this.cx, this.cy] }, 500, "elastic");
				if (this.label) {
					this.label[0].animate({ scale: 1 }, 500, "bounce");
					this.label[1].attr({ "font-weight": 400 });
				}
			});
		});
	}
});
