// aimsCustom.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*/

// global variables
	aimsCustomPresent=true;
	aimsDynamicLayerPresent = false;
	// change these to send XML response to custom function.
	// use numbers >= 1000 and match in useCustomFunction()
	// defaults are defined in aimsXML.js and use standard functions
	
	// xml response mode for selection
	selectXMLMode = 6;
	// xml response mode for identify
	identifyXMLMode = 7;
	// xml response mode for query
	queryXMLMode = 8;
	// xml response mode for find
	findXMLMode = 14;
	// xml response mode hyperlink
	hyperlinkXMLMode = 15;
	



// custom function for handling clicks 
// 		flow redirected here when
//		toolMode set to >=1000
function customMapTool(e) {
				if (toolMode == 1001) {
						// insert code here
						unprojectPoint(e);
						//
						return false;
					
				}
				if (toolMode == 1002) {
						// insert code here

				}

}

// send  XML response to custom function
//		flow  redirected here when
//		XMLMode >=1000
function useCustomFunction(theReply) {
	if (XMLMode==1001) {
		// insert code here
		extractPtVal(theReply);
	
	} else if (XMLMode==1002) {
		// insert code here
	} else {
		alert(msgList[55] + XMLMode + msgList[56]);
	}
	hideLayer("LoadData");
}

// add custom stuff to Map XML request. . . between selection and geocode
function addCustomToMap1(){
	var customString = "";
	/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coords="5 ' + (iHeight-10) + '" label="This is a test">\n';
		customString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		customString += 'threed="TRUE" glowing="' + modeMapGlow + '" />\n</TEXT>\n</OBJECT>';
		customString += '\n</LAYER>\n';
	*/
	return customString;
}

// add custom stuff to Map XML request. . . between clickpoints and copyright
function addCustomToMap2(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . under modeOnMap
function addCustomToMap3(){
	var customString = "";
	
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	// custom geocoded point layer, change marker properties as needed	
	customString += '<LAYER type="acetate" name="sdegeo_point" id="sdegeo_point" >\n';
	customString += '<OBJECT units="database" >\n';
	customString += '<POINT coords="' + sdegeox + ' ' + sdegeoy + '" >\n';
	customString += '<SIMPLEMARKERSYMBOL color="0,0,255" width="20" type="star" />\n';
	customString += '</POINT>\n';
	customString += '</OBJECT>\n';
	customString += '</LAYER>\n';
	

	return customString;
}


function addDynamicMap1(){
	var customString = "";
	var tmpActiveLayerIndex;
	var sFactor = (eRight-eLeft) / iWidth
	var tmplayerName= "NodeLabel";
	tmpActiveLayerIndex =ActiveLayerIndex 
	if (LayerName[tmpActiveLayerIndex]=="Nodes-Milepost")
	{
		for (var i=0;i<layerCount;i++) {
			if (LayerName[i]==tmplayerName) ActiveLayerIndex = i;
		}
	}	
	if ((NCDynamicLabel[tmpActiveLayerIndex] != "" ) && (sFactor>=LayerMinScale[tmpActiveLayerIndex]) && (sFactor<=LayerMaxScale[tmpActiveLayerIndex]) && (LayerVisible[tmpActiveLayerIndex]==1)) 
	{
			customString = '<LAYER type="featureclass" name="Selected Label" visible="true">'
			customString += '<DATASET fromlayer="' + LayerID[ActiveLayerIndex] + '"/>'
			customString += '<SIMPLELABELRENDERER  howmanylabels ="one_label_per_shape" field="' + NCDynamicLabel[ActiveLayerIndex] + '">'
			customString += '<TEXTSYMBOL antialiasing="true" font="Arial" fontstyle="regular" fontsize="10" glowing="204,255,255"  />'
			customString += '</SIMPLELABELRENDERER></LAYER>'
		
	}

	ActiveLayerIndex = tmpActiveLayerIndex;
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

function unprojectPoint(e) {	
	//send a request that will return unprojected coordinates
	getImageXY(e);
	getMapXY(mouseX,mouseY);
	
	var theString = '<ARCXML VERSION="1.0.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
	theString += '<ENVELOPE minx="' + (mapX-5) + '" miny="' + (mapY-5) + '" maxx="' + (mapX+5) + '" maxy="' + (mapY+5) + '" />\n';
	theString += '<IMAGESIZE height="1" width="1" />\n';
	theString += '<LAYERLIST >\n';
	for (var i=0;i<layerCount;i++) {
		theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
	}
	theString += '</LAYERLIST>\n';
        // FILTERCOORDSYS is the id of the ArcIMS service's output coordinate system.
       // FEATURECOORDSYS is the id of the desired coordinate system of the resulting coordinates.
	theString += '<FILTERCOORDSYS id="32118" />\n<FEATURECOORDSYS id="4269" />\n';
	theString += '</PROPERTIES>\n</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	sendToServer(imsURL,theString,1001);	//XMLmode for unproject xy is 1001

	return false;	
}
function extractPtVal(theReply) {
		var theXYs =  getEnvelopeXYs(theReply, 0)
		limitLeft = theXYs[0];
		limitBottom = theXYs[1];
		limitRight = theXYs[2];
		limitTop = theXYs[3];
		var theX = (limitLeft + limitRight)/2;
		var theY = (limitBottom + limitTop)/2;
		alert("X: " + theX + "\n" + "Y: " + theY);
}

function clearDynamicLayer(){

	aimsDynamicLayerPresent = false;
	

}
function SDEGeoSend(x, y){
	// determine maximum extent width and height in map units
	var gwidth = Math.abs(limitLeft - limitRight);
	var gheight = Math.abs(limitTop - limitBottom);
	// set new extent width and height (0.01 assumes map units in decimal degrees)
	var width_prop = 0.01 * gwidth;
	var height_prop = 0.01 * gheight;
	var new_minx = (x - width_prop);
	var new_miny = (y - height_prop);
	var new_maxx = (x + width_prop);
	var new_maxy = (y + height_prop);
	// set global variables to populate ENVELOPE element
	eLeft = new_minx;
	eBottom = new_miny;
	eRight = new_maxx;
	eTop = new_maxy;
	// set global variables to populate the lat/long of geocoded point
	sdegeox = x;
	sdegeoy = y;
	// set global variable to add custom layer to map image (see addCustomToMap4 function)
	aimsCustomPresent = true;

	// create the GET_IMAGE request
	var theRequest = writeXML();

	// send the request to the server, XMLMode equal to 1 (map image returned)
	sendToServer(imsURL, theRequest, 1);
}


function clearSDEGeocode(){
	aimsCustomPresent = false;
}


