/*	MapDotNet Common Drawing Library
	Copyright 2003, Specialized Technology Products, Inc. All Rights Reserved
	
	REVISION HISTORY:
	2/10/2004 Created	BFH
	7/29/2004 Cross Browser FDW
*/

/* adds a rectangle object to the supplied canvass */
/* the parentCanvas is the MapNavigator image DIV */
/* returns the new rectangle object */
/* opacity is supported by VML only */
function MDNAddRect(parentCanvas, left, top, width, height, newID, borderWidth, 
					borderColor, fillColor, opacity, useVML)
{
	var aRect;
	if (useVML)
	{
		aRect=document.createElement('v:Rect');
		parentCanvas.appendChild(aRect);
		aRect.id=newID;
		aRect.fill.opacity=opacity;
		aRect.stroked=true;
		aRect.style.position='absolute';
		aRect.style.width=width;
		aRect.style.height=height;
		aRect.style.left=left;
		aRect.style.top=top;
		
		if (fillColor!=null)
			aRect.fillColor=fillColor;
		if (borderWidth>0)
			aRect.strokeweight=borderWidth;
		if (borderColor!=null)
			aRect.strokecolor=borderColor;
	}
	else
	{
		aRect=document.createElement('div');
		parentCanvas.appendChild(aRect);
		aRect.id=newID;
		aRect.style.borderStyle='solid';
		aRect.style.visibility='visible';
		aRect.style.overflow='hidden';
		aRect.style.position='absolute';
		aRect.style.width=width;
		aRect.style.height=height;
		aRect.style.left=left;
		aRect.style.top=top;
		
		if (fillColor!=null)
			aRect.style.backgroundColor=fillColor;
		if (borderWidth>0)
			aRect.style.borderWidth=borderWidth;
		if (borderColor!=null)
			aRect.style.borderColor=borderColor;
	}
	return aRect;
}

/* draws a polyline or polygon on the supplied DIV canvas */
/* id is only needed to locate VML shapes, not needed for non-VML mode */
function MDNDrawPolyShape(parentCanvas, pointsStr, xArray, yArray, color, weight, closePoly, id, useVML)
{
	var shape;
	if (useVML)
	{
		/* if poly is set to closed, and there are at least three points, add endpoint */
		var endPoint='';
		if (xArray.length>=3 && closePoly)
		{
			var endPoint = '' + xArray[0] + ',' + yArray[0];
		}

		/* draw the polyline
		   Note: the odd fact that points must be set before appending the child shape
		   Note VML quirk that you cannot set "filled" to true and set the opacity */
		shape=document.createElement('v:polyline');
		shape.id=id;
		shape.stroked=true;
		shape.strokeColor=color;
		shape.points=pointsStr.value + endPoint;
		shape.strokeWeight=weight;		
		parentCanvas.appendChild(shape);
		shape.style.left=0;
		shape.style.right=0;
		shape.style.position='absolute';
		shape.style.zIndex=1001;
		shape.filled=false;
	}
	else
	{
		var xA, yA;	
		if (xArray.length>=3 && closePoly)
		{
			ex=new Array();
			ex[0]=xArray[0];
			ey=new Array();
			ey[0]=yArray[0];
			xA=xArray.concat(ex);
			yA=yArray.concat(ey);
		}
		else
		{
			xA=xArray;
			yA=yArray;
		}
		if (xA.length>1)
		{
			var g=new jsGraphics(parentCanvas.id);
			g.setColor(color);
			g.setStroke(weight);
			g.drawPolyline(xA, yA);
			g.paint();
		}
	}
}

/* draws a circle, the first point is the center and the second determines radius (p2-p1) */
function MDNAddCircle(parentCanvas, x, y, radius, color, weight, id, useVML)
{
	var shape;
	if (useVML)
	{
		shape=document.createElement('v:oval');
		shape.id=id;
		shape.stroked=true;
		shape.strokeColor=color;
		shape.strokeWeight=weight;
		shape.style.width=radius*2;
		shape.style.height=radius*2;
		parentCanvas.appendChild(shape);
		shape.style.position='absolute';
		shape.style.left=x-radius;
		shape.style.top=y-radius;
		shape.style.zIndex=1001;
		shape.filled=false;
	}
	else
	{
		var g=new jsGraphics(parentCanvas.id);
		g.setColor(color);
		g.setStroke(weight);
		g.drawOval(x-radius, y-radius, radius*2, radius*2);
		g.paint();
	}
}

/* clear canvas of any polylines or polygons */
/* id of shape is only required if using VML */
function MDNClearShapesFromCanvas(parentCanvas, id, useVML)
{
	if (useVML)
	{
		var theShape=parentCanvas.children[id];
		if (theShape!=null)
		{
			theShape.removeNode(true);
		}
	}
	else
	{
		/* non VML draws pixels with unlabeled DIVs
		so assume we are deleting any child DIVs with no id
		all other elements on the map navigator DIV canvas are left intact */
		var idx;
		var col=parentCanvas.childNodes;
		if (col!=null)
		{
			for (idx=0; idx<col.length; idx++)
			{
				if (col[idx].tagName == 'DIV' && (col[idx].id==null || col[idx].id==''))
				{
					col[idx].parentNode.removeChild(col[idx]);
					idx--;
				}
			}
		}
	}
}

/* returns truncated and padded numeric based on precision */
function MDNTruncateAndPad(val, precision)
{
	var p=Math.pow(10, precision);
	val=(parseInt(val * p))/p;

	var strArray=('' + val).split('.');
	if (strArray.length>1)
	{
		while(strArray[1].length<precision)
			strArray[1]+='0';
		strArray[1]='.'+strArray[1];
	}
	else
	{
		strArray[1]='';
	}
	return strArray[0]+strArray[1];
}


