// 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;
	// 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
						unprojectPt(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 coord="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 = "";
		/*
		customString += '<LAYER type="ACETATE" name="theMode">\n';
		customString += '<OBJECT units="PIXEL">\n<TEXT coord="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';
		alert(customString);
		*/
	return customString;
}

// add custom stuff to Map XML request. . . on top of everything
function addCustomToMap4(){
	var customString = "";
	
	return customString;
}

// extract layers to download
function extractIt() {
	hideLayer("measureBox");
	alert(msgList[51]);
}

function unprojectPt(e) {
  	//alert("in unprojectPt");
  	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="26986" />\n<FEATURECOORDSYS id="4326" />\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) {
  	//alert("in extractPtVal");
	var theXYs = getEnvelopeXYs(theReply, 0);
	aleda_limitLeft = theXYs[0];
	aleda_limitBottom = theXYs[1];
	aleda_limitRight = theXYs[2];
	aleda_limitTop = theXYs[3];
	var theX = (aleda_limitLeft + aleda_limitRight)/2;
	var theY = (aleda_limitBottom + aleda_limitTop)/2;
	var degrees_lat = parseInt(theY);
	var nFract = Math.abs(theY - degrees_lat);
	var minutes_lat = parseInt(nFract*60);
	//var seconds_lat = Math.round(nFract*3600 - minutes_lat*60);
	var seconds_lat = nFract*3600 - minutes_lat*60;
	seconds_lat = seconds_lat.toFixed(2);
	var degrees_long = parseInt(theX);
	nFract = Math.abs(theX - degrees_long);
	var minutes_long = parseInt(nFract*60);
	//var seconds_long = Math.round(nFract*3600 - minutes_long*60);
	var seconds_long = nFract*3600 - minutes_long*60;
	seconds_long = seconds_long.toFixed(2);
	alert("DMS Latitude:\nDegrees: " + degrees_lat + " \nMinutes: " + minutes_lat + " \nSeconds: " + seconds_lat + "\n\nDMS Longitude: \nDegrees: " + degrees_long + " \nMinutes: " + minutes_long + " \nSeconds: " + seconds_long + "\n\n==================\nDecimal Degrees:\nY: " + theY.toFixed(6) + " Latitude \n" + "X: " + theX.toFixed(6) + " Longitude");	
}

