
//envelope for rubberBand.
var rubberBandEnvelope = new tempEnvelope();

//tells when the zoom box is activated
var zoombox = false;

//***********************************************************************
// Function(s) to create a new RubberBand on myMap.
//***********************************************************************


var RubberBandStartX;
var RubberBandStartY;
var RubberBandEndX;
var RubberBandEndY;
var sX; 
var sY;
var startXCoord, startYCoord;
var endXCoord, endYCoord;
var currentX, currentY;


function startRubberBand() {
	document.onselectstart = function(){ return false; }
	
	rubberBandLayer = document.getElementById("rubberBandLayer");
	rubberBandLayer.style.visibility = "visible";
	RubberBandStartY= IMouse.y + "px";
	RubberBandStartX= IMouse.x + "px";
	
	startXCoord =IMouse.coordX;
	startYCoord = IMouse.coordY;

	rubberBandLayer.style.top = RubberBandStartY;
	rubberBandLayer.style.left = RubberBandStartX;
	zoombox = true;
	
	//stop the rubber Band.
	document.onmouseup = stopRubberBand;

}


function moveRubberBands() {



	sX = parseInt(RubberBandStartX);
	sY = parseInt(RubberBandStartY);
	as =  parseInt(sY) - parseInt(IMouse.y);
	
	//Top Left Drag
	if (IMouse.x < sX) {	
		rubberBandLayer.style.top = IMouse.x + "px";
		rubberBandLayer.style.height = sX - IMouse.x + "px"
		rubberBandLayer.style.left = IMouse.x + "px"
		rubberBandLayer.style.width = sX - IMouse.x + "px";
	}
	
	//Lower Left Drag
	if (IMouse.y > sY) {
		rubberBandLayer.style.top = sY + "px";
		rubberBandLayer.style.height = IMouse.y - parseInt(rubberBandLayer.style.top) + "px";
	}	
	
	//Top Right Drag
	if (IMouse.y < sY)	{
		rubberBandLayer.style.top = IMouse.y + "px";
		rubberBandLayer.style.height = sY - IMouse.y + "px"
	}
	
	//Top Right Drag
	if (IMouse.x > sX) {
		rubberBandLayer.style.left = sX + "px";	
		rubberBandLayer.style.width = IMouse.x - sX + "px";	
	}
	
	
	//Normal (Lower Right Drag)
	if (IMouse.x  >  parseInt(rubberBandLayer.style.left) &&  parseInt(IMouse.y) > parseInt(rubberBandLayer.style.top)) {
		rubberBandLayer.style.width = parseInt(IMouse.x) -  parseInt(rubberBandLayer.style.left) + "px";
		rubberBandLayer.style.height = parseInt(IMouse.y) - parseInt(rubberBandLayer.style.top) + "px";
		
	}
 	if(IMouse.y == sY || IMouse.x == sX )	{
		rubberBandLayer.style.width = 0+"px"; 
		rubberBandLayer.style.height = 0+"px";
	}
	
	currentX = IMouse.coordX;
	currentY = IMouse.coordY;
	
}



function stopRubberBand() {
	
	
	document.onselectstart = function(){ return true; }
	
	endXCoord =currentX;
	endYCoord = currentY
	
	RubberBandEndX = IMouse.x;
	RubberBandEndY = IMouse.y;
	
	 if (endXCoord < startXCoord)
	 {
		rubberBandEnvelope.minx =startXCoord;
		rubberBandEnvelope.maxx = endXCoord;
	 }
	 else
	 {
		rubberBandEnvelope.minx =endXCoord;
		rubberBandEnvelope.maxx = startXCoord;
	 }
	 
	 if (endYCoord > startYCoord)
	 {
		rubberBandEnvelope.miny =startYCoord;
		rubberBandEnvelope.maxy = endYCoord;
	 }
	 else
	 {
		rubberBandEnvelope.miny =endYCoord;
		rubberBandEnvelope.maxy =startYCoord;  
	 }
	

	zoombox = false;
	rubberBandLayer.style.visibility = "hidden";
	rubberBandLayer.style.top 		=0 +"px";
	rubberBandLayer.style.left 		=0 +"px";
	rubberBandLayer.style.width 	=0 +"px";
	rubberBandLayer.style.height 	=0 +"px";
	
	
	document.onmouseup = null;

	
	var RubberXDiff = Math.abs(parseInt(RubberBandStartX) -parseInt(RubberBandEndX));
	var RubberYDiff = Math.abs(parseInt(RubberBandStartY) -parseInt(RubberBandEndY));
	
	//stop the rubberBand zoom function if the pixel difference is small
	var minimumPixAllowed = 4;
	if (RubberXDiff <= minimumPixAllowed && RubberYDiff <= minimumPixAllowed) 
	{
		alert("Please hold your left mouse button down and drag your defined area.");
		return;
	}
	
	
	
	//log as event (TODO)
	rubberBandUpFunctions(rubberBandEnvelope);
	
}



function rubberBandUpFunctions(env) {


switch (currentToolName)
{
	case "zoominbox":
	{
		zoom2Envelope(env);	
		break;
	}
	
	case "zoomoutbox":
	{
		convertZoomOut(env);	
		break;
	}
	
	case "select":
	{
		mapSelect(env);	
		break;
	}
}
	
}







