var chart = {
	initialize: function(chart_) {
		this.canvas = chart_;
		if (typeof(G_vmlCanvasManager) != 'undefined') {
			this.canvas = G_vmlCanvasManager.initElement(chart_);
		}
		this.context = this.canvas.getContext('2d');
		this.center = {x: 80, y: 80};
		this.radius = 40;
		this.total = exchange_statistics.inject(0, function(a, n) {return a + n.amount;});
		this.colors = ['#dc1e2a', '#f27624', '#4d4893', '#4d7693'];
		this.draw();
	},
	draw: function() {
		var so_far = 0;
		
		for (var i=0, len=exchange_statistics.length; i<len; i++) {
			var item = exchange_statistics[i],
				percent = item.amount / this.total,
				a = Math.PI * (-0.5 + 2 * so_far),
				b = Math.PI * (-0.5 + 2 * (so_far + percent));
			
			this.context.save();
			this.context.beginPath();
			this.context.moveTo(this.center.x, this.center.y);
			this.context.arc(this.center.x, this.center.y, this.radius, a, b, false);
			this.context.lineTo(this.center.x, this.center.y);
			this.context.closePath();
			this.context.fillStyle = this.colors[i];
			this.context.fill();
			
			var width = 15,
				angle = ((a+b)/2) * 180 / Math.PI,
				x = (this.center.x - width/2 -4) + Math.cos(this.radians(angle)) * (this.radius + 25),
				y = (this.center.y + 4) + Math.sin(this.radians(angle)) * (this.radius + 25);
			
			if (!window.opera) {
				this.context.fillStyle = '#333333';
				this.context.font = 'normal 11px helvetica, arial, sans-serif';
				this.context.fillText(Math.round(percent*1000)/10 + '%', x, y-7);
				this.context.font = 'bold 11px helvetica, arial, sans-serif';
				this.context.fillText(item.currency, x, y+7);
			} else {
				var label = new Element('div', {'class': 'chart_label'});
				label.innerHTML = Math.round(percent*1000)/10 + '%' + '<br /><strong>' + item.currency + '</strong>';
				label.style.left = x - 3 + 'px';
				label.style.top = y - 18 + 'px';
				this.canvas.up().insert(label);
			}
			
			this.context.restore();
			
			if (!Prototype.Browser.IE) {
				var g = this.context.createRadialGradient(this.center.x, this.center.y, 0, this.center.x, this.center.y, this.radius);
				g.addColorStop(0, 'rgba(255, 255, 255, 0)');
				g.addColorStop(1, "rgba(255, 255, 255, 0.25)");
				this.context.beginPath();
				this.context.fillStyle = g;
				this.context.arc(80, 80, this.radius, 0, Math.PI*2, true);
				this.context.fill();
			}

			so_far += percent;
		}
	},
	get_color: function() {
        var rgb = [];
        for (var i=0; i<3; i++) {
            rgb[i] = Math.round(100 * Math.random() + 155);
        }
        return 'rgb(' + rgb.join(',') + ')';
	},
	radians: function(a) {
		return a * Math.PI / 180;
	}
};
