﻿// JScript File

function ShapeClass(TargetElement)
{
	var theGraphic = new jsGraphics(TargetElement);
	
	this.Left		= 0;
	this.Top			= 0;
	this.Right		= 0;
	this.Bottom		= 0;
	this.Width		= 0;
	this.Height		= 0;
	this.OriginalX	= 0;
	this.OriginalY	= 0;
	this.Radius		= 0;
	
	this.Types		= new Enumerator([	Rectangle	="RA", 
													Square		="SQ", 
													Ellipse		="EL", 
													Circle		="CI", 
													Line			="LI", 
													PolyLine		="PL",
													Point			="PO",
													Polygon		="PG"
												]);
	this.ShapeType	= Point;

	this.Actions	= new Enumerator([	Start			="B", 
													Stretch		="S", 
													End			="E", 
													Clear			="C"
												])

	var DrawingGraphic = false;

//---------------------------------------------------------------------------------------------	
	this.SetType = new Function('shapetype', 'this.ShapeType = shapetype;');
//---------------------------------------------------------------------------------------------	
	this.Clear = function()
	{
		theGraphic.clear();
	}
//---------------------------------------------------------------------------------------------	
	this.Draw =	function( Event ) 
	{
		MouseX = Event.x;
		MouseY = Event.y;
		
		switch (Event.type)
		{
			case "mousedown" :
				theGraphic.setColor("#ff00ff");
				theGraphic.setStroke(2);
				this.OriginalX = MouseX;
				this.OriginalY = MouseY;
				DrawingGraphic	= true;
				break;

			case "mousemove" :
				if (!DrawingGraphic) return false;
				break;

			case "mouseup" :
				DrawingGraphic	= false;
				break;

			default :
				return false;
		}

		this.Left		= Math.min( this.OriginalX , MouseX );
		this.Right		= Math.max( this.OriginalX , MouseX );
		this.Width		= this.Right-this.Left;
		this.Top			= Math.min( this.OriginalY , MouseY );
		this.Bottom		= Math.max( this.OriginalY , MouseY );
		this.Height		= this.Bottom-this.Top;

		switch (this.ShapeType)
		{
			case Rectangle :
					theGraphic.clear();
					theGraphic.drawRect(	this.Left, 
												this.Top, 
												this.Width, 
												this.Height
											);
					break
					
			case Square :
					theGraphic.clear();
					theGraphic.drawRect(	this.Left, 
												this.Top, 
												Math.max(this.Width, this.Height), 
												Math.max(this.Width, this.Height)
											 );
					break

			case Ellipse :
					theGraphic.clear();
					theGraphic.drawEllipse(	this.Left, 
													this.Top, 
													this.Width, 
													this.Height
												);
					break

			case Circle :
					this.Radius = Math.sqrt(Math.pow((MouseX - this.OriginalX),2) + Math.pow((MouseY-this.OriginalY),2))
					theGraphic.clear();
					theGraphic.drawEllipse(	this.OriginalX - this.Radius, 
													this.OriginalY - this.Radius, 
													this.Radius*2, 
													this.Radius*2
												);
					break

			case Line :
					theGraphic.drawPolyLine(MouseX,MouseY);
					break

			case PolyLine :
					theGraphic.drawPolyLine(MouseX,MouseY);
					break

			case Point :
					theGraphic.clear();
					theGraphic.drawImage(	"Images/Red_dot.PNG",
													MouseX,
													MouseY,
													2,
													2
												);
					break

			case Polygon :
					theGraphic.drawPolygon(MouseX,MouseY);
					break

			default :
					alert("ERROR: DrawShape: That Shape Type has not been implemented!");
					break
		}
		theGraphic.paint();
		return false;
	};
}
