// aimsPrint.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,
*		aimsLegend.js
*/

// BB: Begin customization for print size
var printDPI = 300;		// BB: printer DPI--may be overridden by print form
var printHeight = 450;	// BB: default print dimensions
var printWidth = 450;	// BB
var printScale = 0;		// BB: RF scale (1:x number) for printing
var INCH_TO_MAP_UNITS = 69 * 63360	// BB: inches in one map unit -- converts DPI to scale
			// NOTE that for decimal-degree map, scale is only correct in x or y
			//		due to convergence of meridians toward the poles!
// BB: end section

aimsPrintPresent=true;

var printTitle = titleList[4];
var printMapURL="";
var printOVURL="";
var printLegURL="";

var legVis2=false;

/*
***************************************************************************************

Print functions 

***************************************************************************************
*/


// display print form
function printIt() {
	hideLayer("measureBox");
	if (useTextFrame) {
		parent.TextFrame.document.location = "printform.htm";
	} else {
		var Win1 = open("printform.htm","PrintFormWindow","width=575,height=150,scrollbars=yes,resizable=yes");
	}
}

// create web page for printing

//BB: Custom version of getPrintMap
function getPrintMap(title, w, h) {
	var tLeft, tRight, tBottom, tTop;
	showRetrieveMap();
	printTitle=title;
	
	legVis2=legendVisible;
	if (aimsLegendPresent) legendVisible=true;
	
	// set print size to input values
	if (w != "") printWidth = parseInt(w);	
	if (h != "") printHeight = parseInt(h);
	
	if (printScale > 0) {
	
		// set map extent if scale specified
		tLeft = eLeft;
		tRight = eRight;
		tBottom = eBottom;
		tTop = eTop;
		
		// Width/height of map in ground distance, scaled appropriately
		var iScaledWidth = printWidth/96 * printScale / INCH_TO_MAP_UNITS
		var iScaledHeight = printHeight/96 * printScale / INCH_TO_MAP_UNITS
		// Center of map
		var xCenter = (parseFloat(eRight) + parseFloat(eLeft)) / 2
		var yCenter = (parseFloat(eTop) + parseFloat(eBottom)) / 2
		
		// New map extents based on scale
		eLeft = xCenter - iScaledWidth/2
		eRight = xCenter + iScaledWidth/2
		eBottom = yCenter - iScaledHeight/2
		eTop = yCenter + iScaledHeight/2
	}
	
	// set XMLMode so get print res.
	XMLMode = 101;
	// enlarge legend in proportion to map
	var tempLegW = legWidth;
	var tempLegH = legHeight;
	legWidth = parseInt(printWidth/4);
	legHeight = parseInt(printWidth/3);
	
	var theString = writeXML();
	
	if (printScale > 0) {
		eLeft = tLeft;
		eRight = tRight;
		eBottom = tBottom;
		eTop = tTop;
	}
	
	legWidth = tempLegW;	// restore default leg vars
	legHeight = tempLegH;
	legendVisible = legVis2;
	sendToServer(imsURL,theString,101);
	theString=null;
}
//BB: end section

	// first get Map
/*function getPrintMap(title) {
	showRetrieveMap();
	printTitle=title;
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	iWidth=450;
	iHeight=450;
	legVis2=legendVisible;
	if (aimsLegendPresent) legendVisible=true;
	var theString = writeXML();
	iWidth=tempWidth;
	iHeight = tempHeight;
	legendVisible = legVis2;
	sendToServer(imsURL,theString,101);
	tempWidth=null;
	tempHeight=null;
	theString=null;
}*/
// second, get OVMap
function getPrintOV() {
	var tempWidth = i2Width;
	var tempHeight = i2Height;
	// BB: Customization for print sizing
	i2Width=parseInt(printWidth/4);		// scale OV map to main map
	i2Height=parseInt(printHeight/4);
	// BB: end section
	var tempDraw=drawOVExtentBox;
	drawOVExtentBox=true;
	var theString = writeOVXML();
	drawOVExtentBox=tempDraw;
	i2Width=tempWidth;
	i2Height = tempHeight;
	sendToServer(imsOVURL,theString,102);
	tempWidth=null;
	tempHeight=null;
	theString=null;
}
// third, get Legend
function getPrintLegend() {
	//  waiting for Legend tags
	if (printLegURL=="") printLegURL = "images/nolegend.gif";
	writePrintPage();
}
// fourth, write the web page
function writePrintPage() {
	var Win1 = open("","PrintPage");
	Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '">');
	Win1.document.writeln('<meta http-equiv="imagetoolbar" content="false">');
	Win1.document.writeln('<head><title>' + titleList[5] + '</title>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
	Win1.document.writeln('<FONT FACE="Arial"><B>');
	//BB Customization: omit table width
	Win1.document.writeln('<TABLE BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
	Win1.document.writeln('	<TR>');
	// BB Customization: scale title via style
	Win1.document.writeln('		<TH COLSPAN="2" style="font-size:' + parseInt(printWidth/20) + 'px">' + printTitle + '</TH>');
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('	<TR>');
	Win1.document.write('		<TD WIDTH="450" HEIGHT="450"');
	if (hasOVMap) Win1.document.write(' ROWSPAN="2"');
	Win1.document.writeln('>');
	//BB Customization: set map size
	Win1.document.writeln('			<IMG SRC="' + printMapURL + '" WIDTH=' + printWidth + ' HEIGHT=' + printHeight + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="Map">');
	Win1.document.writeln('		</TD>');
	if (hasOVMap) {
		//BB Customization: set cell size for OV
		Win1.document.writeln('		<TD HEIGHT="' + parseInt(printHeight/4) + '" ALIGN="CENTER">');
		//BB Customization: set OV size
		Win1.document.writeln('			<IMG SRC="' + printOVURL + '" WIDTH=' + parseInt(printWidth/4) + ' HEIGHT=' + parseInt(printHeight/4) + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="Overview">');
		Win1.document.writeln('		</TD>');
	}
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('	<TR>');
	Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
	// BB Customization: add Legend title, set size
	Win1.document.write('         <span style="font-size:' + parseInt(printWidth/40) + 'px">');
	Win1.document.writeln(legTitle + '</span><br><br> ');
	Win1.document.writeln('			<IMG SRC="' + printLegURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="Legend"');
	Win1.document.writeln(' width="' + parseInt(printWidth/4) + '" >');
	Win1.document.writeln('		</TD>');
	Win1.document.writeln('	</TR>');
	
	Win1.document.writeln('</TABLE>');
	Win1.document.writeln('</B></FONT>');
	Win1.document.writeln('</body></html>');
	Win1.document.close();
	
	legendVisible=legVis2;
	Win1=null;
	hideRetrieveMap();
}

