function tooltip(title)
{
	$("#tooltip").html('<p style="margin:0px; padding:0px;">'+title+'</p>');
}

/*
 * 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/
 * 
 *
 */
(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 title	=	"";
		
		var data = [], color, colors = [], titles = [], 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;
			titles[titles.length]=$('td',this)[0].innerHTML;
		     // random color
		     color = getColor();
		     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');
	    // 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 pieVertices = 12;
		var arcIncrementMultiplier = 1 / pieVertices;
		
	    var sofar = 0; // keep track of progress
	    // loop the data[]
	    for (var piece in data) {

	        var thisvalue = data[piece] / total;
			var arcStartAngle = Math.PI * (- 0.5 + 2 * sofar); // -0.5 sets set the start to be top
			var arcEndAngle = Math.PI * (- 0.5 + 2 * (sofar + thisvalue));	
	        
			ctx.beginPath();
	        ctx.moveTo(center[0], center[1]); // center of the pie
	        ctx.arc(  // draw next arc
	            center[0],
	            center[1],
	            radius,
	            arcStartAngle,
	            arcEndAngle,
	            false
	        );
						
			var arcIncrement = (arcEndAngle - arcStartAngle) * arcIncrementMultiplier;
			
			var coord = [];
			var coordIndex = 1;
						
			for (i = 0; i < ((pieVertices * 2) - 2); i = i+2) {				
				var arcAngle = arcStartAngle + arcIncrement * coordIndex;
				coord[i] = radius + Math.round(Math.cos(arcAngle) * radius);				
				coord[i+1] = radius + Math.round(Math.sin(arcAngle) * radius);
				coordIndex++;			
			}
			
			var xx = radius + Math.round(Math.cos(arcStartAngle) * radius);
			var yy = radius + Math.round(Math.sin(arcStartAngle) * radius);
			
			var xxEnd = radius + Math.round(Math.cos(arcEndAngle) * radius);
			var yyEnd = radius + Math.round(Math.sin(arcEndAngle) * radius);	
			
			var coords	=	radius + ',' + radius + ',' + + xx + ',' + yy + ',' + coord.join(',') +  ',' + xxEnd + ',' + yyEnd;
			
			areatag = '<area shape="poly" coords="'+coords+'" title="'+titles[piece]+'" alt="'+titles[piece]+'" onmouseover="tooltip(this.title)" onmouseout="tooltip(\'\')" ></area>';
						
			document.getElementById("slices").innerHTML  +=  areatag;
			
			$('#pie_chart').attr('usemap','#slices');
						
	        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
	    }
	
	});	
	
};

// utility - generates random color
function getColor() {
    var rgb = [];
    for (var i = 0; i < 3; i++) {
        rgb[i] = Math.round(255 * Math.random() + 80) ; // [155-255] = lighter colors
    }
    return 'rgb(' + rgb.join(',') + ')';
}

})(jQuery);

function submitPoll(pollID, question)
{
	$.post("/bundles/polls/ajax/polls.php",   
	  { pollID : pollID, question : question } , function(data) {    
	  	if(data=="done!")
		{
			window.location.reload();	
		}
	  	
	  }, "html" 
	);
}