// screenCapture.js
/*
*		
* Written by Ben Britton, IDWR
* This routine uses some variables that are declared for use by ESRI's "Print tool"
*		
*/

// display initial form that shows the options for captured-image size
function captureIt() {
	hideLayer("measureBox");
	if (useTextFrame) {
		parent.TextFrame.document.location = "/_common_UI/captureform.htm";
	} else {
		var Win1 = open("/_common_UI/captureform.htm","CaptureFormWindow","width=575,height=350,scrollbars=yes,resizable=yes");
	}
}


// Set dimensions for requested image and send the request to the server.
function getCapturedMap(resx, indSB, indNA) {
	showRetrieveMap();
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	switch (resx) {
		case 'asdrawn': { 
//			iWidth=iWidth;
//			iHeight=iHeight;
			break;
		}
		case 'low': { 
			iWidth=640;
			iHeight=480;
			break;
		}
		case 'medium': { 
			iWidth=800;
			iHeight=600;
			break;
		}
		case 'high': { 
			iWidth=1024;
			iHeight=768;
			break;
		}
		case 'veryhigh': { 
			iWidth=1280;
			iHeight=1024;
			break;
		}
		default: { 
//			iWidth=iWidth;
//			iHeight=iHeight;
			break;
		}
	}
//	var indSB = false;
//	var indNA = false;
	var theString = writeCaptureXML(indSB, indNA);
	iWidth=tempWidth;
	iHeight = tempHeight;
	sendToServer(imsURL,theString,1005);
	tempWidth=null;
	tempHeight=null;
	theString=null;
}

//Put the captured image on the user's screen.
function writeCapturePage(theReply,indSB, indNA) {

	printMapURL = getURL(theReply);

	var Win1 = open(printMapURL,"CapturePage","scrollbars=yes,resizable=yes,toolbar=yes,directories=no,location=no,status=no,menubar=no");
	hideRetrieveMap();
}

// prepare the request in xml format for Main Map.  indSB and indNA are true if Scale-bar or NorthArrow are to be included
function writeCaptureXML(indSB, indNA) {
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n<ENVELOPE minx="' + forceComma(eLeft) + '" miny="' + forceComma(eBottom) + '" maxx="' + forceComma(eRight) + '" maxy="' + forceComma(eTop) + '" />\n';
	theString += '<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" />\n';
//brb	var tmpWidth = iWidth * 300 / 96;
//brb	var tmpHeight = iHeight * 300 / 96;
//brb	theString += '<IMAGESIZE height="' + tmpHeight + '" width="' + tmpWidth + '" dpi="300" />\n';
	var visString = "";
	if (aimsLayersPresent) {
		// tell the server which layers are to be visible
//		if (toggleVisible) {
			theString += '<LAYERLIST >\n';
			for (var i=0;i<layerCount;i++) {
				if (LayerVisible[i]==1) {
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="true" ';
					if (aimsClassRenderPresent) {
						theString += addSpecialRenderToMap(i);
					} else {
						theString += '/>\n';
					}
					
				}
				else {
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
				}
				
			}
			theString += '</LAYERLIST>\n';
//		}
	}
	
	// map background color
	if (mapTransparent) {
		theString += '<BACKGROUND color="255,255,254" transcolor="255,255,254" />\n';
	} else {
		if (mapBackColor!="") {
			theString += '<BACKGROUND color="' + mapBackColor + '" />\n\n';
		}
	}
//	if (aimsLegendPresent) {
//		// create a legend image
//		if (legendVisible) theString += addLegendToMap();
//	}
	
	theString += '</PROPERTIES>\n';
	
	// buffer
	if (aimsBufferPresent) {
		if (showBuffer) theString += addBufferToMap();
	}
	
	// select
	if (aimsSelectPresent) {
		theString += addSelectToMap();
	}	
	
	// any custom stuff to fit here
	if (aimsCustomPresent) theString += addCustomToMap1();
	
	
	if (aimsClickPresent){
		// clickpoints 
		if (clickCount>0) {
			// draw click points and lines between them on map
			var clickColor = selectColor;
			if (clickType==1) clickColor = clickMarkerColor;
			theString += '<LAYER type="acetate" name="allTheClicks">\n';
			if (clickCount>1) {
				theString += '<OBJECT units="database">\n<LINE coords="' + forceComma(clickPointX[0]) + coordsDelimiter + forceComma(clickPointY[0]);
				for (var i=1;i<clickCount;i++) {
					theString += pairsDelimiter  + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]); 
				}
				theString += '" >\n';
				theString += '<SIMPLELINESYMBOL type="solid" color="' + clickMarkerColor;
				theString += '" width="3" />\n</LINE>\n</OBJECT>\n'; 
				theString += '<OBJECT units="database">\n<LINE coords="' + forceComma(clickPointX[0]) + coordsDelimiter + forceComma(clickPointY[0]);
				for (var i=1;i<clickCount;i++) {
					theString += pairsDelimiter  + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]); 
				}
				theString += '" >\n';
				theString += '<SIMPLELINESYMBOL type="solid" color="255,255,255" width="1" />\n</LINE>\n</OBJECT>\n'; 
	 		}
			for (var i=0;i<clickCount;i++) {
				theString += '<OBJECT units="acetate">\n<POINT coords="' + forceComma(clickPointX[i]) + coordsDelimiter + forceComma(clickPointY[i]) + '">\n';
				theString += '<SIMPLEMARKERSYMBOL  type="' + clickMarkerType + '"';
				theString += ' color="' + clickMarkerColor + '" width="' + clickMarkerSize + '" />\n</POINT>\n</OBJECT>\n';
			}
			theString += '</LAYER>\n';
		}
	}
	
	// geocoding or point with label
	if (showGeocode) {
		// draw the point . . . also used to display any point with a label on map
		theString += '<LAYER type="acetate" name="GeoCode1">\n';
		theString += '<OBJECT units="database">\n<POINT coords="' + forceComma(geocodeX) + coordsDelimiter + forceComma(geocodeY) + '">\n';
		theString += '<SIMPLEMARKERSYMBOL  type="circle"  color="' + geocodePointColor + '" width="' + geocodePointSize +'" overlap="false" />\n</POINT></OBJECT>\n';
		if (geocodeLabel!="") {
			theString += '<OBJECT units="database">\n<TEXT coords="' + forceComma(geocodeX) + coordsDelimiter + forceComma(geocodeY) + '" label="' + geocodeLabel + '">\n';
			theString += '<TEXTMARKERSYMBOL fontcolor="' + geocodePointColor + '" fontsize="12" shadow="64,64,64" glowing="255,255,0" halignment="right" valignment="top" overlap="false" /></TEXT></OBJECT>\n';
		}
		theString += '</LAYER>\n';
		
	}
	
	// any custom stuff to fit here
	if (aimsCustomPresent) theString += addCustomToMap2();

//	if (drawCopyright) {
	if (indNA) {
		// draw text on the map
		theString += '<LAYER type="acetate" name="theCopyright">\n';		
		theString += '<OBJECT units="pixel">\n<TEXT coords="' + CopyrightCoords + '" label="' + CopyrightText + '">\n';
		theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
		theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
		//if (CopyrightBackground.toUpperCase()=="TRUE") theString += 'background="' + CopyrightBackground + '" backcolor="' + CopyrightBGColor + '" ';
		if (CopyrightBackground.toUpperCase()=="TRUE") theString += 'blockout="' + CopyrightBGColor + '" ';
		theString += 'shadow="32,32,32" overlap="false" ';
		if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
		theString += '/>\n</TEXT>\n</OBJECT>\n';
		theString += '</LAYER>\n';
	}
//	if (drawNorthArrow) {
	if (indNA) {
		// draw a north arrow
		theString += '<LAYER type="acetate" name="theNorthArrow">\n';
		theString += '<OBJECT units="pixel">\n<NORTHARROW type="' + NorthArrowType + '" size="' + NorthArrowSize + '" coords="' + NorthArrowCoords + '" shadow="32,32,32" ';
		theString += 'angle="' + NorthArrowAngle + '" antialiasing="True" overlap="False" />\n</OBJECT>\n';
		theString += '</LAYER>\n';
	}
//	if (drawScaleBar) {
	if (indSB) {
		// draw a scale bar
		//ScaleBarPrecision = numDecimals;
		theString += '<LAYER type="acetate" name="theScaleBar">\n';
		xDistance = eRight - eLeft;
		//yDistance = eTop - eBottom;
		//theString += '<OBJECT units="database">\n';
		//theString += '<SCALEBAR coord="' + ((xDistance * 0.60)+eLeft) + coordsDelimiter + (((yDistance/iHeight)*2)+eBottom) + '" outline="' + ScaleBarBackColor + '" ';
		theString += '<OBJECT units="pixel">\n';
		theString += '<SCALEBAR coords="' + parseInt(iWidth * (6/10)) + coordsDelimiter + '2" ';
		if (MapUnits=="DEGREES") theString += 'mode="geodesic" ';
		///*
		theString += 'outline="' + ScaleBarBackColor + '" ';
		theString += 'font="' + ScaleBarFont + '" fontcolor="' + ScaleBarFontColor + '" style="' + ScaleBarStyle + '" barcolor="' + ScaleBarColor + '" ';
		if (MapUnits!="DEGREES") {
			theString += 'mapunits="' + MapUnits.toLowerCase() + '" ';
		}
		theString += 'scaleunits="' + ScaleBarUnits.toLowerCase() + '" antialiasing="True" ';
		var sDistance = getScaleBarDistance();
		if (MapUnits=="DEGREES") {
			if (xDistance<1/10){
				theString += 'precision="' + ScaleBarPrecision + '" ';
			}
		} else if (sDistance<5) {
			theString += 'precision="' + ScaleBarPrecision + '" ';
		}
		theString += 'screenlength="' + parseInt(iWidth * (2/10)) + '" ';
		//theString += 'distance="' + sDistance + '" ';
		theString += 'fontsize="' + ScaleBarSize + '" barwidth="' + ScaleBarWidth + '" overlap="False"  ';
		//*/
		theString += '/>\n</OBJECT>\n';
		theString += '</LAYER>\n';	
		
	}
	
	// any custom stuff to fit here
	if (aimsCustomPresent) theString += addCustomToMap3();

	if (drawModeOnMap) {
		// draw the current mode on the map
		theString += '<LAYER type="acetate" name="theMode">\n';
		theString += '<OBJECT units="pixel">\n<TEXT coords="5' + coordsDelimiter + (iHeight-10) + '" label="' + modeBlurb + '">\n';
		theString += '<TEXTMARKERSYMBOL fontstyle="BOLD" fontsize="12" font="ARIAL" fontcolor="' + modeMapColor + '" ';
		theString += 'threed="TRUE" glowing="' + modeMapGlow + '" overlap="false" />\n</TEXT>\n</OBJECT>';
	
	}
	
	// any custom stuff to be drawn on top of everything
	if (aimsCustomPresent) theString += addCustomToMap4();

	theString += '</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	return theString;
}

