// JavaScript Document



//*************************************************************
// Performs the nesessary opertaions for the tools selected.
//*************************************************************

var previousTool = currentToolName;

function setTool(toolName) {
	previousTool = currentToolName;
	currentToolName = toolName;
	
	
	
	var thisTool = findToolObject(toolName)
	var isButton = thisTool.isButton
	
 	switch (toolName)
  	{
		//=================================
		//Custom tool for OnCor
		//Delete if not in use.
		//==================================
		
				
		case "back":	//HyperLink for CofOf PDF's
		{
			myMap.manageHistory(true)
			break;
		}
		
		case "elections":	//HyperLink for CofOf PDF's
		{
			populateDistrictNumbers()
			showLayer("electionsPrintingLayer");
			//position the div
			epl = document.getElementById("electionsPrintingLayer");
			epl.style.top = parseInt(myMap.layer.offsetTop + 10) + "px";
			epl.style.left = parseInt(myMap.layer.offsetLeft + 10) + "px";
			break;
		}
		
		
		
		
		case "roadDedications":	//HyperLink for CofOf PDF's
		{
			clearAllObjectsOnMap()
			myMap.layer.style.cursor = "crosshair";
			myMap.layer.onmousedown = function() { myHyperLinks.getHyperLink("roadDedications") }
			startHyperSelection("roadDedications")
			break;
		}
		
		
		case "CofO":	//HyperLink for CofOf PDF's
		{
			clearAllObjectsOnMap()
			myMap.layer.style.cursor = "crosshair";
			myMap.layer.onmousedown = function() { myHyperLinks.getHyperLink("CofO") }
			startHyperSelection("CofO")
			break;
		}
		
		
		
		
		case "latlong":	
		{
			clearAllObjectsOnMap()
			clearCoordinateConvertLayer()
			positionCoordinateConvertLayer();

			myMap.layer.onmousedown = getLatLong;
			myMap.layer.style.cursor = "crosshair";
			break;
		}
			
			
			
		case "parcelId":
		{
			clearAllObjectsOnMap()
			myMap.layer.onmousedown = doParcelId;
			myMap.layer.style.cursor = "crosshair";
			break;
		}
		
		
		
		case "newParcelSearch": 
		{
			clearAllObjectsOnMap()
			myMap.container.style.cursor = "crosshair";
			myMap.layer.onmousedown = getPrintKey;
			break;
		}
		
		
		/*
		case "test":
		{
			clearAllObjectsOnMap()
			myMap.layer.style.cursor = "crosshair";
			myMap.layer.onmousedown = startPoly;
			break;
		}
		*/
		
		
		//*************************************************************
		// Create tool and perform functions when tool is clicked.
		//*************************************************************
		case "zoomin":
		{
			clearAllObjectsOnMap()
			myMap.zoomInStep();
			myMap.layer.style.cursor = "default";
			break;
		}
		
		//=============================================================
		case "zoomout":
		{
			clearAllObjectsOnMap()
			myMap.zoomOutStep();
			break;
		}
		//=============================================================
		case "pan":
		{
			clearAllObjectsOnMap()
			//myMap.layer.style.cursor = "move";
			myMap.layer.style.cursor="-moz-grab" 
			myMap.layer.style.cursor = "url('"+iconPath+"/pan.cur')";
			myMap.layer.onmousedown= startPan;
			break;
		}
		
		
		//=============================================================		
		case "fullextent":
		{
			clearAllObjectsOnMap()
			myMap.fullextent = true;
			myMap.getMap();
			break;
		}
		
		
		case "measure":
		{
			startMeasure();
			clearMeasure();
			myMap.layer.onmousedown= measureClick;
			myMap.layer.onclick= null;
			myMap.container.style.cursor = "crosshair";
			break;	
		}
		
		case "zoominbox":
		{
			rubberBandLayer = document.getElementById("rubberBandLayer");
			clearAllObjectsOnMap()
			
			myMap.layer.style.cursor = "-moz-zoom-in";
			myMap.layer.style.cursor = "url('"+iconPath+"/ZoomIn.cur')";
			rubberBandLayer.className = "rubberBandLayerZoomIn"
			myMap.layer.onmousedown= startRubberBand;
			break;	
		}
		
		
		case "zoomoutbox":
		{
			rubberBandLayer = document.getElementById("rubberBandLayer");
			clearAllObjectsOnMap()
			rubberBandLayer.className = "rubberBandLayerZoomOut"
			myMap.layer.style.cursor = "-moz-zoom-out";
			myMap.layer.style.cursor = "url('"+iconPath+"/ZoomOut.cur')";
			myMap.layer.onmousedown= startRubberBand;
			break;	
		}
		
		
		
		
		//=============================================================
		case "attributeQuery":
		{
			clearAllObjectsOnMap()
			myMap.layer.style.cursor = "crosshair";
			myMap.layer.onmousedown= doAttributeQuery;
			break;
		}
		
		case "identify":
		{
			clearAllObjectsOnMap()
			showActiveLayerList(false)
			myMap.layer.onmousedown = doPointQuery;
			myMap.layer.style.cursor = "crosshair";
			break;	
		}
		

		case "select":
		{
			clearAllObjectsOnMap()
			rubberBandLayer = document.getElementById("rubberBandLayer");
			rubberBandLayer.className = "rubberBandLayerSelect"
			myMap.layer.style.cursor = "pointer";
			myMap.layer.style.cursor = 'url('+iconPath+'/goldPointer.cur)';
			myMap.layer.onmousedown= startRubberBand;
			showActiveLayerList(true)
			break;	
		}

		
		case "clear":
		{
			clearAllObjectsOnMap()			
			myMap.spatialSelection = null;	 //ISpatialSelection
			myMap.querySelection= null; 	//IQuerySelection
			myMap.querySelection2= null; 	//IQuerySelection
			myMap.pointSelection= null; 	//IPointSelection
			myMap.appendXML= null; // string of XML
			myMap.imageBuffer = null;
			myMap.getMap();
			break;	
		}
		
		
		case "pictometry":
		{
			//clearAllObjectsOnMap()
			//myMap.layer.style.cursor = "crosshair";
			//myMap.layer.onmousedown = doOnCorPicto
			alert("This Feature is currently disabled until a license is purchased");
			break;	
		}
		
		case "print":
		{
			loadPrint("printingLayer");
			break;		
		}
		case "refresh":
		{
			document.location.href = document.location.href;
			break;		
		}
		
		case "help":
		{
			window.open(appPath+"/help.htm","helpPage", "height=500,width=800,status=yes,toolbar=no,menubar=yes,resizable=yes ,location=no,scrollbars =yes")
			break;		
		}
		
		case "link":
		{
			var myURL = getLinkURL()
			document.getElementById("linkTextbox").value = myURL;
			putInLowerLeft(linkLayer)
			showLayer('linkLayer');
			break;		
		}
		
		
		case "emailLink":
		{
			mailLink();
			break;
		}
		
		/*
		case "markerClick":
		{
			myMap.layer.onmousedown= null;
			break;	
		}
		*/
		/*
		case "layerList":
		{
			createLegend()
			break;	
		}
		*/
		

		//=============================================================
		default: { alert("Unknown tool, make sure you added the functions to setTools.js! check your code"); break; }
	
	}
	
	if(isButton) { currentTool = setTool(previousTool); }
	//alert(previousTool);
	
	
}



function clearAllObjectsOnMap() {
	if (measuring) { clearMeasure() }
	settingsLayer.innerHTML = '&nbsp;';
	myMap.spatialSelection = null;
	myColorPicker.hide();
}






function startPan() {
	
	document.onselectstart = function(){ return false }
	panning = true
	panX = IMouse.x;
	panY = IMouse.y;
	myMap.layer.style.cursor = "-moz-grabbing"
	myMap.layer.style.cursor = "url('"+iconPath+"/panMove.cur')";
	
	document.getElementById("mapLayer").onmouseup = stopPan;
	
}

function stopPan() {
	
	if (panning)
	{	
		document.onselectstart = function(){ return true }
		convertPanCoords()
		document.getElementById(myMap.layerName).onmouseout = null
		//reset the panDivs
		document.getElementById(myMap.imageName).onload = resetPanDivs;	
		myMap.layer.style.cursor="-moz-grab" 
		myMap.layer.style.cursor = "url('"+iconPath+"/pan.cur')";
		panning = false;
	}
	
}

function checkPanMouseOut() {
	
	xx = IMouse.x-panX  ;
	yy = IMouse.y-panY  ;
	
	
	panning = false;
	//reset the panDivs
  	document.getElementById(myMap.imageName).onload = resetPanDivs;	
	
	document.onselectstart = function(){ return true }
	stopPan()
	convertPanCoords()
	

}



//reset the Pan divs after pan
function resetPanDivs() {
	
	document.getElementById(myMap.imageName).style.top 	= 0 + "px";
	document.getElementById(myMap.imageName).style.left = 0 + "px"
	document.getElementById(myMap.layerName).style.top 	= 0 + "px";
	document.getElementById(myMap.layerName).style.left = 0 + "px"
	document.getElementById(myMap.imageName).onload = null
	
}



//convert for panning
function convertPanCoords() {
	mm = document.getElementById("mapLayer")

	//convert Screen extents to Coords
	coordWidth = parseFloat(myMap.envelope.maxx) - parseFloat(myMap.envelope.minx); // width
	coordHeight = parseFloat(myMap.envelope.maxy) - parseFloat(myMap.envelope.miny); // height
	
	xFraction = coordWidth /  parseFloat(mm.style.width);
	yFraction = coordHeight /  parseFloat(mm.style.height);
	
	//final coord...
	panStartX 		=  parseFloat(mm.style.left) * xFraction +  parseFloat(myMap.envelope.minx);
	panStartY		=  parseFloat(myMap.envelope.maxy) -  parseFloat(mm.style.top) * yFraction;
	panDifferenceX 	=  parseFloat(myMap.envelope.minx) - panStartX;
	panDifferenceY	=  parseFloat(myMap.envelope.maxy) - panStartY;
	
	panExtent = new Object()
	panExtent.minx = parseFloat(myMap.envelope.minx) + panDifferenceX
	panExtent.maxy = parseFloat(myMap.envelope.maxy) + panDifferenceY
	panExtent.maxx = parseFloat(myMap.envelope.maxx) + panDifferenceX
	panExtent.miny = parseFloat(myMap.envelope.miny) + panDifferenceY
	
	//do WORK HERE... NOT GOOD.
	myMap.envelope = panExtent;
	myMap.fullextent = false;

	myMap.getMap();
	
	
					
}








