//***********************************************************
//*************** EDIT FUNCTIONS ****************************
//***********************************************************

var clickCount = 0;
var clickPixelX = new Array();
var clickPixelY = new Array();
var clickPointX = new Array();
var clickPointY = new Array();
var drawContent = "";
var mapX = 0; 
var mapY = 0; 

//reset Measure tool
function resetEditTool(){
	clickCount = 0;
	clickPixelX = new Array();
	clickPixelY = new Array();
	clickPointX = new Array();
	clickPointY = new Array(); 
	replaceLayerContent("Drawing", "");
//	document.frmMain.coords.value = "";
}

//add Measure pixel
function addMeasurePixel(theX, theY){
	clickPixelX[clickCount] = theX;
	clickPixelY[clickCount] = theY;
}

//add Measure point
function addMeasurePoint(theX, theY){
	clickPointX[clickCount] = theX;
	clickPointY[clickCount] = theY;
	clickCount = clickCount + 1;
	if(clickCount > 0){
		showMeasureGraphics();
	}
}

// show Measure graphics
function showMeasureGraphics(){
	drawContent = "";
			if(clickCount == 1){
				ptX1 = clickPixelX[0];
				ptY1 = clickPixelY[0];
				drawPoint(ptX1,ptY1,0);
				
			}
			if(clickCount == 2){
				ptX2 = clickPixelX[1];
				ptY2 = clickPixelY[1];
				drawPoint(ptX2,ptY2,0);
				
				drawLine(ptX1,ptY1,ptX2,ptY2,0);
				replaceLayerContent("Drawing", drawContent);
				sendMeasure(ptX1,ptY1,ptX2,ptY2);
								}
			if(clickCount == 3){  //this should never fire because after the second click the measure function fires within map.aspx.vb; I put it in here for testing
			resetEditTool();
								}
								}

//***********************************************************
//*************** GEOMETRY FUNCTIONS ************************
//***********************************************************


wi=2
he=2
r=255
g=0
b=0

//draw point
function drawPoint(x,y, rubberBand){
  if(rubberBand == 1){
	r = 60;
  } else {
    r = 255;
  }
 // if(document.frmMain.tool.value == "EDIT_POINT"){
//	drawContent = drawContent +"<img src='images/transparent.gif' style='z-index:-10; position:absolute; left:"+x+";top:"+y+";width:5;height:5;background:rgb("+r+","+g+","+b+")'>"; 
 // }else{
	drawContent = drawContent +"<img src='images/transparent.gif' style='z-index:-10; position:absolute; left:"+x+";top:"+y+";width:"+wi+";height:"+he+";background:rgb("+r+","+g+","+b+")'>"; 
//  }
}

//draw line
function drawLine(x0,y0,x1,y1,rubberBand){
	wi1=wi
	he1=he
	x0=Math.round(x0)
	x1=Math.round(x1)
	y0=Math.round(y0)
	y1=Math.round(y1)
	xxx=x0
	yyy=y0
	ax=Math.abs(x1-x0)+1
	ay=Math.abs(y1-y0)+1
	jx=jy=1
	if(x1<x0){jx=-1}
	if(y1<y0){jy=-1}
	
	if (ax==ay){
		while (xxx!=x1+jx){
			drawPoint(xxx,yyy,rubberBand)
		  	xxx=xxx+jx
		  	yyy=yyy+jy
		}
		return
	}
	if (ax<ay){
		while (xxx!=x1+jx){
			x=xxx+jx
			y=Math.floor((x-x0)/ax*ay*jx*jy+y0)
			he=(y-yyy)*jy
			if(jy==-1){
				drawPoint(xxx,y+1,rubberBand)
			}else{
				drawPoint(xxx,yyy,rubberBand)
			}
			yyy=y
			xxx=xxx+jx
		 }
	}else{
		while (yyy!=y1+jy){
			y=yyy+jy
			x=Math.floor((y-y0)/ay*ax*jx*jy+x0)
			wi=(x-xxx)*jx
			if(jx==-1){
				drawPoint(x+1,yyy,rubberBand)
			}else{
				drawPoint(xxx,yyy,rubberBand)
			}
			xxx=x
			yyy=yyy+jy
		}
	}
	wi=wi1
	he=he1
}
