
//======================================================================
jQuery(document).ready(function() {
	drawGraph();
});


//======================================================================
Raphael.fn.pieChart = function(a_cx, a_cy, a_radius, a_valueArr, a_labelArr, a_colourArr, a_stroke) {
   var l_paper = this;
   var l_chart = this.set();
	var l_shadow = null;

   var l_angle = 90;
   var l_total = 0;
   for (var i = 0, ii = a_valueArr.length; i < ii; i++) {
       l_total = l_total + a_valueArr[i];
   }

   var c_rad = Math.PI / 180;

   function pieChartSegment(a_cx, a_cy, a_radius, startAngle, endAngle, params) {
       var x1 = a_cx + a_radius * Math.cos( - startAngle * c_rad);
       var x2 = a_cx + a_radius * Math.cos( - endAngle * c_rad);
       var y1 = a_cy + a_radius * Math.sin( - startAngle * c_rad);
       var y2 = a_cy + a_radius * Math.sin( - endAngle * c_rad);
       return l_paper.path(["M", a_cx, a_cy, "L", x1, y1, "A", a_radius, a_radius, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
   }

   var process = function(j) {
       var l_value = a_valueArr[j];
       var l_colour = "rgb(" + a_colourArr[j][3] + ", " + a_colourArr[j][4] + ", " + a_colourArr[j][5] + ")";
       var l_colourBase = "rgb(" + a_colourArr[j][0] + ", " + a_colourArr[j][1] + ", " + a_colourArr[j][2] + ")";
       var l_anglePlus = 360 * l_value / l_total;
       var l_popAngle = l_angle + (l_anglePlus / 2);
       var l_timing = 500;
       var l_delta = 30;
       	var l_segment = pieChartSegment(a_cx, a_cy, a_radius, l_angle, l_angle + l_anglePlus, {
	           gradient: (l_angle + l_anglePlus / 2) + "-" + l_colourBase + "-" + l_colour,
	           stroke: a_stroke,
	           "stroke-width": 3
	       });

       var l_text = l_paper.text(a_cx*1.1,a_cy*2.1, 
           a_labelArr[j]).attr({fill: "#000", stroke: "none", opacity: 0, "font-family": '"Gill Sans", Helvetica, Arial', "font-size": "16px"});

       l_segment.mouseover(function() {
           l_segment.animate({scale: [1.1, 1.1, a_cx, a_cy]}, l_timing, "elastic");
           l_text.animate({opacity: 1});
       }).mouseout(function() {
           l_segment.animate({scale: [1, 1, a_cx, a_cy]},l_timing, "elastic");
           l_text.animate({opacity: 0});
       });
		

       l_angle = l_angle + l_anglePlus;
       l_chart.push(l_segment);
       l_chart.push(l_text);
   };

   for (var i = 0; i < ii; i++) {
       process(i);
   }		
	l_shadow = l_paper.circle(a_cx + 18, a_cy + 18, a_radius).attr({"stroke-width": 0, "gradient": "r(0.5,0.5)#000-#fff"}).toBack();
	l_chart.push(l_shadow);
   return l_chart;
};

//======================================================================
Raphael.fn.pieCircle = function(a_cx, a_cy, a_radius, a_label, a_colour, a_stroke) {
   var l_paper = this;
   var l_chart = this.set();
	var l_shadow = null;
       var l_timing = 500;
	
	var l_circle = l_paper.circle(a_cx, a_cy, a_radius).attr({"stroke-width": 3, stroke: "#FFF",
	"gradient": "r(0.5,0.5)rgb(" + a_colour[0] + ", " + a_colour[1] + ", " + a_colour[2] + ")-rgb("
	 + a_colour[3] + ", " + a_colour[4] + ", " + a_colour[5] + ")"});
	
     l_chart.push(l_circle);

     var l_text = l_paper.text(a_cx*1.1,a_cy*2.1, 
         a_label).attr({fill: "#000", stroke: "none", opacity: 0, "font-family": '"Gill Sans", Helvetica, Arial', "font-size": "16px"});

     l_circle.mouseover(function() {
         l_circle.animate({scale: [1.1, 1.1, a_cx, a_cy]}, l_timing, "elastic");
         l_text.animate({opacity: 1});
     }).mouseout(function() {
         l_circle.animate({scale: [1, 1, a_cx, a_cy]},l_timing, "elastic");
         l_text.animate({opacity: 0});
     });

     l_chart.push(l_text);
		
	l_shadow = l_paper.circle(a_cx + 18, a_cy + 18, a_radius).attr({"stroke-width": 0, "gradient": "r(0.5,0.5)#000-#fff"}).toBack();
	l_chart.push(l_shadow);
   return l_chart;
};

//======================================================================
function drawGraph() {
    var l_values, l_labels, l_colours, l_tmpColour;
    jQuery("div.xGraphCanvas").each(function() {
        l_values = [];
        l_labels = [];
        l_colours = [];
        jQuery("table.xData tr", this).each(function() {
			var l_stat = parseInt(jQuery("td.xStat", this).text(),10);
			if (l_stat != "0") {
          	  l_values.push(l_stat);
				l_tmpColour = jQuery("td.xColour", this).text().split(" ");
	            l_colours.push(l_tmpColour);
	            l_labels.push(jQuery("th", this).text() + " " + "(" + jQuery("td.xStat", this).text() + ")");
			}
        });
		if (jQuery(this).hasClass("xGrandTotal")) {
			if (l_values.length == 1) {
	        	Raphael(jQuery("div.xChart", this).get(0), 300, 300).pieCircle(145, 140, 120, l_labels[0], l_colours[0], "#fff" );
			} else{
        		Raphael(jQuery("div.xChart", this).get(0), 300, 300).pieChart(145, 140, 120, l_values, l_labels, l_colours, "#fff" );
			}
		} else {
			if (l_values.length == 1) {
        		Raphael(jQuery("div.xChart", this).get(0), 200, 200).pieCircle(88, 88, 73, l_labels[0], l_colours[0], "#fff" );
			} else{
	        	Raphael(jQuery("div.xChart", this).get(0), 200, 200).pieChart(88, 88, 73, l_values, l_labels, l_colours, "#fff" );
			}
		}
    });
}
