/*
 * Canvas pies - JQuery plugin
 *
 * @author Francesco Vivoli <f.vivoli@gmail.com> - http://atalayasec.org
 *	
 *
 * Based on code initially wrote by Stoyan Stevanof - http://www.phpied.com/
 * 
 * !!!!!! BEFORE YOU UPDATE THIS FILE, KEEP IN MIND IT IS MODDED !!!!!!
 */
(function($) {
/**
 * parse a table into a canvas pie chart.
 * @example $('#containerdiv').canvaspie({table: '#mydata',canvas: '#canvas'})
 * @desc hides the table and put the resulting pie chart into the specified 
 *  canvas.   
 */	
$.fn.canvaspie = function(options) {
	var defaults = {
		table: '#mydata',
		canvas: '#canvas'
	}
	
	var opts = $.extend(defaults, options);
	
	$(this).each(function(){
	
		//if(!$(opts.canvas,this).length || !$(opts.table,this).length) return
		//$(opts.table,this).hide();
		var table = $(opts.table,this);
		var tindex = 1;

		var data = [], color, colors = [], value = 0, total = 0;

		$('tr',table).each(function(){
			if($('td',this).length==0) return;
			value = parseFloat($('td',this)[tindex].innerHTML);
			data[data.length]=value;
			total+=value;

		     // random color
		     color = getPieColor();
		     colors[colors.length] = color; // save for later
		     $(this).css("background-color",color) // color this TR

		});


		
	    ///// STEP 2 - Draw pie on canvas
		//todo: find a way of getting this jquery-style and still be able
		//to access the context
		//var canvas = document.getElementById('canvas');
		var canvas = document.getElementById(my_canvas_id);
		//var canvas = $(opts.canvas,this);
	    // exit if canvas is not supported
	    //if (typeof canvas.getContext === 'undefined') {
	      //return;
	    //}

	    // get canvas context, determine radius and center
	    var ctx = canvas.getContext('2d');
	    var canvas_size = [canvas.width, canvas.height];
	    var radius = Math.min(canvas_size[0], canvas_size[1]) / 2;
	    var center = [canvas_size[0]/2, canvas_size[1]/2];

	    var sofar = 0; // keep track of progress
	    // loop the data[]
	    for (var piece in data) {

	        var thisvalue = data[piece] / total;

	        ctx.beginPath();
	        ctx.moveTo(center[0], center[1]); // center of the pie
	        ctx.arc(  // draw next arc
	            center[0],
	            center[1],
	            radius,
	            Math.PI * (- 0.5 + 2 * sofar), // -0.5 sets set the start to be top
	            Math.PI * (- 0.5 + 2 * (sofar + thisvalue)),
	            false
	        );

	        ctx.lineTo(center[0], center[1]); // line back to the center
	        ctx.closePath();
	        ctx.fillStyle = colors[piece];    // color
	        ctx.fill();

	        sofar += thisvalue; // increment progress tracker
	    }
	
	
	});	
	
};




})(jQuery);


// utility - generates random color

//jblossom: moved this outside scope so it can be redefined within OEM
function getPieColor() {
    var rgb = [];
    for (var i = 0; i < 3; i++) {
        rgb[i] = Math.round(100 * Math.random() + 155) ; // [155-255] = lighter colors
    }
    return 'rgb(' + rgb.join(',') + ')';
}
