YAHOO.namespace('Flickr.Graph');

YAHOO.Flickr.Graph = function(elementId, attr) {	
	// Call the superclass' constructor.
	YAHOO.Flickr.Graph.superclass.constructor.call(this, elementId, attr); 
};
YAHOO.lang.extend(YAHOO.Flickr.Graph, YAHOO.Smb.Graph);


// --------------------------------------------------------------------------------
// Methods that override the original YAHOO.Smb.Graph methods.
// --------------------------------------------------------------------------------

YAHOO.Flickr.Graph.prototype.drawPie = function(dataSet, color) {

	// Draw the pie pieces and the white outline.

	this.ctx.strokeStyle = this.graphOutline;
	this.ctx.lineWidth = 1;

	for(var n = this.start, len = dataSet.length, last = Math.PI / -2; n < this.end && n < len; n++) {
		this.ctx.fillStyle = (n < color.length) ? color[n].fill : color[n % color.length].fill;
		this.ctx.beginPath();
		this.ctx.moveTo(this.scale.center.x, this.scale.center.y);
		this.ctx.arc(this.scale.center.x, this.scale.center.y, this.scale.radius, last, this.scale.radians * dataSet[n].y.value + last, false);
		this.ctx.lineTo(this.scale.center.x, this.scale.center.y);
		this.ctx.fill();

		this.ctx.beginPath();
		this.ctx.moveTo(this.scale.center.x, this.scale.center.y);
		if(n === 0) this.ctx.lineTo(this.scale.center.x + this.scale.radius, this.scale.center.y);
		this.ctx.arc(this.scale.center.x, this.scale.center.y, this.scale.radius, last, this.scale.radians * dataSet[n].y.value + last, false);
		this.ctx.lineTo(this.scale.center.x, this.scale.center.y);

		last += this.scale.radians * dataSet[n].y.value;
	}

	// Draw the colored outline.

	this.ctx.lineWidth = 5;		
	last = 0;

	for(var n = this.start, len = dataSet.length, last = Math.PI / -2; n < this.end && n < len; n++) {
		this.ctx.strokeStyle = (n < color.length) ? color[n].outline : color[n % color.length].outline;		
		this.ctx.moveTo(this.scale.center.x, this.scale.center.y);

		this.ctx.beginPath();
		this.ctx.arc(this.scale.center.x, this.scale.center.y, this.scale.radius, last, this.scale.radians * dataSet[n].y.value + last, false);
		this.ctx.stroke();
		
		last += this.scale.radians * dataSet[n].y.value;		
	}

	// Redraw the white outline.
	
	this.ctx.strokeStyle = this.graphOutline;
	this.ctx.lineWidth = 2;
	last = 0;
	var hyp = this.scale.radius + 4;

	for(var n = this.start, len = dataSet.length, last = Math.PI / -2; n < this.end && n < len; n++) {
		this.ctx.beginPath();
		this.ctx.moveTo(this.scale.center.x, this.scale.center.y);

		var theta = this.scale.radians * dataSet[n].y.value + last;
		var opp = hyp * Math.sin(theta);
		var adj = hyp * Math.cos(theta);

		this.ctx.lineTo(this.scale.center.x + adj, this.scale.center.y + opp);
		this.ctx.stroke();
		
		last += this.scale.radians * dataSet[n].y.value;		
	}	

	return this;
};
var x=1;
var objCache;
YAHOO.Flickr.Graph.prototype.drawArea = function(dataSet, color) {

	// Draw the line.
 {
	this.ctx.strokeStyle = color.line;
	this.ctx.lineWidth = 2;
	this.ctx.lineJoin = 'bevel';
  
	this.ctx.beginPath();
	this.ctx.moveTo(this.paddingLeft, this.height - ((dataSet[this.start].y.value - this.min) * this.scale[0].vert) - this.paddingBottom);
	for(var n = this.start, len = dataSet.length; n < this.end && n < len; n++) {
		this.ctx.lineTo(dataSet[n].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((dataSet[n].y.value - this.min) * this.scale[0].vert) - this.paddingBottom);
	}   
	this.ctx.stroke();

	// Draw the circles.
	this.ctx.strokeStyle = '#fff';
	this.ctx.fillStyle = color.line;
	this.ctx.lineWidth = 1;
	for(var n = this.start, len = dataSet.length; n < this.end && n < len; n++) {
		this.ctx.moveTo(dataSet[n].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((dataSet[n].y.value - this.min) * this.scale[0].vert) - this.paddingBottom);
		this.ctx.beginPath();
		this.ctx.arc(dataSet[n].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((dataSet[n].y.value - this.min) * this.scale[0].vert) - this.paddingBottom, 3, 0, 2 * Math.PI, false);
		this.ctx.fill();
		this.ctx.stroke();
	}

}

 	// Re-draw the circles outline for Safari 2.
	if(this.SAFARI_2) {
		this.ctx.strokeStyle = '#fff';
		this.ctx.lineWidth = 1;
		for(var n = this.start, len = dataSet.length; n < this.end && n < len; n++) {
			this.ctx.moveTo(dataSet[n].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((dataSet[n].y.value - this.min) * this.scale[0].vert) - this.paddingBottom);
			this.ctx.beginPath();
			this.ctx.arc(dataSet[n].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((dataSet[n].y.value - this.min) * this.scale[0].vert) - this.paddingBottom, 3, 0, 2 * Math.PI, false);
			this.ctx.stroke();
		}   		
	}


	return this;
};

YAHOO.Flickr.Graph.prototype.updateHoverInfo = function(e) {
	var x = Math.round((e.clientX - this.canvasPosition.x - this.parentPosition.x - this.paddingLeft) / (this.type === 'pie' ? this.scale.horiz : this.scale[0].horiz)) + this.start;
	if(x >= this.data[0].length) {
		this.hideHoverInfo();
		this.render();
		return this;
	}
	// Fire the custom event.

	var args = [];
	if(this.type === 'pie') {
		x = Math.round(e.clientX - YAHOO.util.Dom.getX(this.canvas));
		var y = Math.round(e.clientY - YAHOO.util.Dom.getY(this.canvas));

		var deltaX = Math.abs(x - this.scale.center.x);
		var deltaY = Math.abs(y - this.scale.center.y);
		var distanceFromCenter = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		var inGraph = distanceFromCenter <= this.scale.radius;
		if(inGraph) {
			// Calculate the angle in radians.

			var angle = Math.atan(deltaY/deltaX);
			if(x < this.scale.center.x && y < this.scale.center.y) {        // Quadrant IV
				angle += Math.PI;
			}
			else if(x < this.scale.center.x && y >= this.scale.center.y) {  // Quadrant III
				angle = Math.PI - angle;
			}
			else if(x >= this.scale.center.x && y >= this.scale.center.y) { // Quadrant II
				// We don't need to do anything here.
			}
			else {                                                          // Quadrant I
				angle = 2 * Math.PI - angle;
			}

			// Use the angle to determine which piece the cursor is over.

			for(var n = this.start, len = this.data[0].length, last = 0; n < this.end && n < len; n++) {
				if(angle >= last && angle < this.scale.radians * this.data[0][n].y.value + last) {
					args = [this.data[0][n].x.label.replace(/ /g, '&nbsp;'), (this.data[0][n].y.value / this.total * 100) + '%', this.data[0][n].y.value];
					break;
				}
				last += this.scale.radians * this.data[0][n].y.value;
			}        
		}
		else args = [];
	}
	else if(x >= this.start)  {
		args.push(this.data[0][x].x.label);
		for(var n = 0, len = this.data.length; n < len; n++) {
			if(!this.enableData[n]) continue;
			args.push(this.data[n][x].y.value);
		}
	}
	if(args.length > 0) this.mousemove.fire(args);
	// Update the hover info.
	
		if(this.enableHoverInfo) {
		if(this.type === 'pie') {
			if(inGraph) {
				YAHOO.util.Dom.setStyle(this.hoverInfo, 'display', 'block');                                        
				YAHOO.util.Dom.setStyle(this.hoverInfo, 'left', e.clientX + 'px');
				YAHOO.util.Dom.setStyle(this.hoverInfo, 'top', e.clientY + 'px');
				this.hoverInfoText.innerHTML = args[0] + '&nbsp;-&nbsp;' + args[1] + '&nbsp;(' + args[2] + ')';
			}
			else {
				YAHOO.util.Dom.setStyle(this.hoverInfo, 'display', 'none');                		        
			}
		}
		else {
			this.hideHoverInfo();
			//this.render();
			if(x >= this.start && x <= this.end)
			 {
	


	    	if(this.MSIE) {
	    	   var imgX =(this.data[0][x].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft-2);
  		     var imgH=document.getElementById("hover-image");
  	       imgH.style.display='inline';
  	       imgH.style.left=imgX+"px";
	       }
	       else
	       {
          this.render();
			    this.ctx.strokeStyle = '#fef5e1';
	  			this.ctx.lineWidth = 3;
		  		this.ctx.beginPath();
			  	this.ctx.moveTo(this.data[0][x].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.paddingTop + 1);
			  	this.ctx.lineTo(this.data[0][x].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - this.paddingBottom - 1);
				  this.ctx.stroke();
				
	

				  this.ctx.strokeStyle = '#fff';
				  this.ctx.lineWidth = 1;
				  this.ctx.fillStyle = '#fbbc36';

				  for(var n = 0, len = this.data.length; n < len; n++) {
					  this.ctx.moveTo(this.data[n][x].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((this.data[n][x].y.value - this.min) * this.scale[0].vert) - this.paddingBottom);
					  this.ctx.beginPath();
					  this.ctx.arc(this.data[n][x].x.value * this.scale[0].horiz - (this.start * this.scale[0].horiz) + this.paddingLeft, this.height - ((this.data[n][x].y.value - this.min) * this.scale[0].vert) - this.paddingBottom, 4, 0, 2 * Math.PI, false);
					  this.ctx.fill();
					  this.ctx.stroke();
			 	  }
			   }
			  
			}	
		}
	}
  
};