﻿// (c) Copyright 2012, The Software Ranch LLC and Morpho-Graphics LLC. All rights reserved

function DrawingContext()
{
	// Which drawing library are we using, Raphael or Walter Zorn?
	this.useRaphael = false;
	
	// Need to track all the raphael objects.
	this.objects = null;
	
	this.div = null;
	this.dc = null;
	this.color = "#ffffff";
	this.stroke = 1;

	this.font = "";
	this.size = 12;
	
	this.initialize = function(div, x, y)
	{
		// Set up the drawing contexts.
		this.div = div;
		if (this.useRaphael)
		{
			this.dc = Raphael(document.getElementById(div), x, y);
			var type = this.dc.toString();
			this.objects = new Array();
		}
		else
		{
			this.dc = new jsGraphics(div);
		}
	}

	this.resize = function(x, y)
	{
		if (this.useRaphael)
		{
			this.dc.setSize(x, y);
		}
		else
		{
		}
	}

	this.setColor = function(color)
	{
		this.color = color;
		if (this.useRaphael)
		{
		}
		else
		{
			this.dc.setColor(color)
		}
	}

	this.setStroke = function(stroke)
	{
		this.stroke = stroke;
		if (this.useRaphael)
		{
		}
		else
		{
			this.dc.setStroke(stroke)
		}
	}

	this.clear = function()
	{
		if (this.useRaphael)
		{
			for (var i=0; i<this.objects.length; i++)
			{
				this.objects[i].remove();
			}
			this.objects = new Array();
		}
		else
		{
			this.dc.clear();
		}
	}

	this.paint = function()
	{
		if (this.useRaphael)
		{
		}
		else
		{
			this.dc.paint();
		}
	}

	this.drawLine = function(x1, y1, x2, y2)
	{
		if (this.useRaphael)
		{
			var path = this.dc.path({ "stroke": this.color, "stroke-width": this.stroke });
			path.moveTo(x1, y1);
			path.lineTo(x2,y2);
			this.objects[this.objects.length] = path;
		}
		else
		{
			this.dc.drawLine(x1, y1, x2, y2)
		}
	}
	
	this.drawPolyline = function(pointsX, pointsY)
	{
		if (this.useRaphael)
		{
			var path = this.dc.path({ "stroke": this.color, "stroke-width": this.stroke });
			path.moveTo(pointsX[0], pointsY[0]);
			for (var i=1; i<pointsX.length; i++)
			{
				path.lineTo(pointsX[i], pointsY[i]);
			}
			this.objects[this.objects.length] = path;
		}
		else
		{
			this.dc.drawPolyline(pointsX, pointsY);
		}
	}

	this.drawPolygon = function(pointsX, pointsY)
	{
		if (this.useRaphael)
		{
			var path = this.dc.path({ "stroke": this.color, "stroke-width": this.stroke });
			path.moveTo(pointsX[0], pointsY[0]);
			for (var i=1; i<pointsX.length; i++)
			{
				path.lineTo(pointsX[i], pointsY[i]);
			}
			path.moveTo(pointsX[0], pointsY[0]);
			this.objects[this.objects.length] = path;
		}
		else
		{
			this.dc.drawPolygon(pointsX, pointsY);
		}
	}

	this.fillEllipse = function(x, y, dx, dy)
	{
		if (this.useRaphael)
		{
			var ellipse = this.dc.ellipse(x + dx/2, y + dy/2, dx/2, dy/2);
			ellipse.attr("fill", this.color);
			this.objects[this.objects.length] = ellipse;
		}
		else
		{
			this.dc.fillEllipse(x, y, dx, dy);
		}
	}

	this.fillPolygon = function(xPoints, yPoints)
	{
		if (this.useRaphael)
		{
			// TODO: Do a filled polygon.  Probably using path().
		}
		else
		{
			this.dc.fillPolygon(xPoints, yPoints);
		}
	}

	this.drawEllipse = function(x, y, dx, dy)
	{
		if (this.useRaphael)
		{
			var ellipse = this.dc.ellipse(x + dx/2, y + dy/2, dx/2, dy/2);
			ellipse.attr("stroke", this.stroke);
			ellipse.attr("color", this.color);
			this.objects[this.objects.length] = ellipse;
		}
		else
		{
			this.dc.drawEllipse(x, y, dx, dy);
		}
	}

	this.setFont = function(font, size, style)
	{
		if (this.useRaphael)
		{
			this.font = font;
			this.size = size;
			this.style = style;
		}
		else
		{
			this.dc.setFont(font, size, style);
		}
	}

	this.drawString = function(text, x, y)
	{
		if (this.useRaphael)
		{
			this.dc.print(x, y, text, this.dc.getFont(this.font), this.size);
		}
		else
		{
			this.dc.drawString(text, x, y);
		}
	}

	this.setPrintable = function(printable)
	{
		if (this.useRaphael)
		{
		}
		else
		{
			this.dc.setPrintable(printable);
		}
	}
	
	this.translate = function(x, y)
	{
		if (this.useRaphael)
		{
			for (var i=0; i<this.objects.length; i++)
			{
				this.objects[i].translate(x, y);
			}
		}
		else
		{
		}
	}

	this.rotate = function(deg, cx, cy)
	{
		if (this.useRaphael)
		{
			for (var i=0; i<this.objects.length; i++)
			{
				this.objects[i].rotate(deg, cx, cy);
			}
		}
		else
		{
		}
	}

	this.drawGrabber = function(x, y, color)
	{
		this.setColor(color);
		this.fillEllipse(Math.round(x-gs2), Math.round(y-gs2), gs, gs);
		this.setStroke(1);
		this.setColor("000000");
		this.drawEllipse(Math.round(x-gs2), Math.round(y-gs2), gs, gs);
	}
}

