/*
* print.js
* created:
*/

var print_map = new Object();
print_map.pWindow = '';
print_map.pActive = false;
print_map.layer = 'print';
print_map.title = '';
print_map.width_default = 922;
print_map.height_default = 682;
//print_map.width = 682;
//print_map.height = 352;
print_map.width = print_map.width_default;
print_map.height = print_map.height_default;

print_map.margin_right = 0.55;
print_map.dpi = 96; //default
print_map.image = '';
/*
* em_print_dialog
* brings up print dialog
*/
function em_display_dialog_print() {
	// shows/hides layer for print questions
	print_div = $(print_map.layer);
	//alert("print layer="+print_map.layer);
	if (print_map.pActive==true) {
		//alert('hide');
		// hides print layer
		//new Effect.Shrink(print_div, {direction: top-left});
		//new Effect.Fade(print_div, {duration: 3.0})
		new Effect.Opacity(
		print_div, { duration: 1.0, to:0.0, from:1.0, transition: Effect.Transitions.linear,
		afterFinish:function() { Element.hide(print_div); }
		}
		);
		//print_div.style.visibility = hide;
		//print_div.style.display = 'none';
		/*
		*/
		//print_div.style.zIndex = 1;
		//debug("em_display_dialog_print:hide.done "+Element.getStyle(print_div, 'display'));

	} else {
		// show print layer
		//alert('show');
		// set print form left
		print_div.style.left = width - (width/1.35);
		// set print form width
		print_div.style.width = width/2;
		// set print form height
		print_div.style.height = height/1.75;

		print_div.style.zIndex = 1003;
		//new Effect.Appear(print_div, {duration: 3.0})
		//new Effect.Grow('print',null, {"direction":"bottom-right"});
		print_div.style.visibility = show;

		print_div.style.opacity = 0.0;
		print_div.style.filter = 'alpha(opacity=0)'; // ie

		new Effect.Opacity(
		print_div, { duration: 1.0, to:1.0, from:0.0, transition: Effect.Transitions.linear,
		beforeStart:function() { Element.show(print_div); }
		}
		);
		//print_div.style.visibility = show;
		window.scrollTo(0,0);
		//print_div.style.display = 'block';
		/*
		*/

		//debug("em_display_dialog_print:show.done "+Element.getStyle(print_div, 'display'));
		// #print
	}
	print_map.pActive = !print_map.pActive;
}

/*
* em_print
* creates map window and shows you your map to print
*/
function em_print() {
	//
	// gather current layer info to create a new image
	// print based on 8.5x11 at this time

	//alert("If necessary remember to set your print layout to Landscape before printing.");
	/*
	* use prototype.js function $F
	* print_map.title = $F('map_title');
	*/
	print_map.title = $F('fld_map_title');
	getMap_for_print();

	/*
	// BB: Set print resolution
	t.printDPI = theForm.printRes.options[theForm.printRes.selectedIndex].value;
	// BB: Set print scale, if applicable
	//var theScale = theForm.scale.value;
	//if ((!isNaN(parseFloat(theScale))) && (parseFloat(theScale) > 0))
	//	t.printScale = parseFloat(theScale);
	// Get print size from form
	var theMapHeight = theForm.height.value;
	var theMapWidth = theForm.width.value;
	t.getPrintMap(theTitle, theMapWidth, theMapHeight);

	var useTextFrame = t.useTextFrame;
	t=null;
	if (opener) {
	window.close();
	} else {
	if (useTextFrame) document.location = "text.htm";
	}
	}


	// vAXL_screen has XML for screen map
	// vAXL_print

	//
	getMap


	*/

} // em_print


function em_print_window(){
	var printWin = open("print.html","PrintWindow","width="+(width/1.5)+",height="+(height/1.75)+",menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes");

	//write the print contents to the print window

	printWin.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
	printWin.document.writeln('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	printWin.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >');
	printWin.document.writeln('<head>');
	printWin.document.writeln(' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />');
	printWin.document.writeln(' <title>Enchanted Mountains Map</title>');
	printWin.document.writeln(' <link rel="stylesheet" type="text/css" href="css/print.css" media="all"/>');
	printWin.document.writeln('</head>');
	printWin.document.writeln('<body>');

	printWin.document.writeln('<div id="header">');
	printWin.document.writeln(' <div id="logo"><img src="graphics/logo_enchanted_mountains.jpg" alt="Enchanted Mountains of Cattaraugus County"/></div>');

	printWin.document.writeln(' <h1>'+print_map.title+'</h1>');
	printWin.document.writeln(' <p>Need help with your map? Please call 1-800-331-0543 or visit <a href="http://www.EnchantedMountains.info">www.EnchantedMountains.info</a>.</p>');
	printWin.document.writeln('</div>');
	var iTmpPointsNum = point_unique_id;
	var sTmpPoints = '';
	var iTmpCount=0;
	for (var tmpPID=0;tmpPID<iTmpPointsNum;tmpPID++) {
		if (aPoint_x[tmpPID] > 0 && aPoint_y[tmpPID] > 0 ) {
			var iTmpCookie = isPointCookie(aPoint_layer_id[tmpPID], aPoint_arc_point_id[tmpPID]);
			if (!iTmpCookie) {
				iTmpCount += 1;
				var tmpLayerID = getLayerIndexByLayerID(aPoint_layer_id[tmpPID]);
				sTmpPoints +='<li id="print_info_'+tmpPID+'" class="'+(iTmpCount%2==0?"x":"")+'">';
				sTmpPoints +='<img src="'+LayerSwatch[tmpLayerID].src+'" alt="Icon for '+LayerName[tmpLayerID]+' - '+aPoint_name[tmpPID]+'" />';
				sTmpPoints +='<span class="loc_num">'+aPoint_id[tmpPID]+')</span> <span class="loc_name">'+aPoint_name[tmpPID]+'</span> <address>'+aPoint_address[tmpPID]+' '+aPoint_city[tmpPID]+', '+aPoint_state[tmpPID]+' '+aPoint_zip[tmpPID]+'</address>'+
					' <span class="loc_contact"><span class="phone">'+aPoint_phone[tmpPID]+'</span>';
				var sTmpWebsite = link_repair(aPoint_web_site[tmpPID]);
				if (sTmpWebsite!=null) {
					sTmpPoints +=' <span class="website">'+link_remove_http(sTmpWebsite)+'</span>';
				}
				sTmpPoints +='</span></li>\n';
			}
		}
	}
	if (iTmpCount > 0) {
		printWin.document.writeln('<h4>Points</h4>');
		printWin.document.writeln('<ul class="points">');
		printWin.document.writeln(sTmpPoints);
		printWin.document.writeln('</ul>');
	}
	
	var sTmpSavedLocations = '';
	var iTmpCount=0;
	for (var tmpPID=0;tmpPID<iTmpPointsNum;tmpPID++) {
		if (aPoint_x[tmpPID] > 0 && aPoint_y[tmpPID] > 0 ) {
			var iTmpCookie = isPointCookie(aPoint_layer_id[tmpPID], aPoint_arc_point_id[tmpPID]);
			if (iTmpCookie != null) {
				iTmpCount += 1;
				var tmpLayerID = getLayerIndexByLayerID(aPoint_layer_id[tmpPID]);
				sTmpSavedLocations += '<li id="print_info_'+tmpPID+'" class="'+(iTmpCount%2==0?"x":"")+'">';
				sTmpSavedLocations += '<img src="'+LayerSwatch[tmpLayerID].src+'" alt="Icon for '+LayerName[tmpLayerID]+' - '+aPoint_name[tmpPID]+'" />';
			    var iTmpPosSaved = cookies_saved_position(aPoint_layer_id[tmpPID], aPoint_arc_point_id[tmpPID]);
			    sTmpSavedLocations += (iTmpPosSaved==-1?aPoint_id[tmpPID]:'<span class="loc_num lbl_saved_point">'+aLetters[iTmpPosSaved]+')</span> ');
			    sTmpSavedLocations += '<span class="loc_name">'+aPoint_name[tmpPID]+'</span> <address>'+aPoint_address[tmpPID]+' '+aPoint_city[tmpPID]+', '+aPoint_state[tmpPID]+' '+aPoint_zip[tmpPID]+
				    '</address> <span class="loc_contact"><span class="phone">'+aPoint_phone[tmpPID]+'</span>';
				var sTmpWebsite = link_repair(aPoint_web_site[tmpPID]);
				if (sTmpWebsite!=null) {
					sTmpPoints +=' <span class="website">'+link_remove_http(sTmpWebsite)+'</span>';
				}
				sTmpSavedLocations += '</span></li>\n';
			}
		}
	}
	if (iTmpCount > 0) {
		printWin.document.writeln('<h4>Your Saved Points (within current view)</h4>');
		printWin.document.writeln('<ul class="points">');
		printWin.document.writeln(sTmpSavedLocations);
		printWin.document.writeln('</ul>');
	}

	printWin.document.writeln('<div id="map-wrap">');
	if ($F('fld_print_image')=="print") {
		printWin.document.writeln('<img src="'+print_map.image+'" alt="Print Map" id="map" />');
	} else {
		printWin.document.writeln('<img src="'+$('theImage').src+'" alt="Screen Map" />');
	}
	printWin.document.writeln('</div>\n');
	printWin.document.writeln('</body></html>');
	printWin.document.close();
	printWin = null;
}



function getMap_for_print(envelope) {
	http_print = getHTTPObject();
	if ((http_print != null)) {
		var axl_print = getMapRequest_for_print(envelope);
		if(!isIE){http_print.overrideMimeType("text/xml")}
		// please note that for printing we use the variable print_url
		http_print.open("POST", print_url, true);
		http_print.onreadystatechange = printResponse_for_print;
		debug("sending axl for print :" + axl_print);
		http_print.send(axl_print);
	}
}

function getMapRequest_for_print(envelope){
	/*
	* 20060126 this differs from the original function getMapRequest
	* in that we use the variable vAXL_screen that has stored
	* the previous request and just replace
	* the IMAGESIZE tag
	* note: have to replace
	* <IMAGESIZE height="' + mheight + '" width="' + mwidth + '" />
	* with the PRINT height and width
	*/
	var axl_print = vAXL_screen;
	// recreate layerlist with all point layers off <LAYERLIST >...</LAYERLIST>
	
	// adjust image size
	pos_crop_start = axl_print.indexOf("<IMAGESIZE");
	if (pos_crop_start != -1) {
		//
		var pos_crop_end = axl_print.indexOf("/>",pos_crop_start);
		var axl_1 = axl_print.substring(0,pos_crop_start);
		debug('axl 1 print: '+axl_1);

		var sAxlImage = axl_print.substring(pos_crop_start,pos_crop_end);
		debug('sAxlImage = '+sAxlImage);
		/* print image size scaled from the current map size */
		var iTmpPrintWidth = print_map.width_default;
		var iTmpPrintHeight = (iTmpPrintWidth * mheight)/mwidth;
		if (iTmpPrintHeight > print_map.height_default) {
			iTmpPrintHeight = print_map.height_default;
			iTmpPrintWidth = (iTmpPrintHeight * mwidth) / mheight;
			
		}
		print_map.height = parseInt(iTmpPrintHeight);
		print_map.width = parseInt(iTmpPrintWidth);

		var axl_2 = axl_print.substring(pos_crop_end+2);
		// geocoding or point with label

		var sTmpProp = "</PROPERTIES>";
		var sTmpImage = "</GET_IMAGE>";
		var pos_prop_end = axl_2.indexOf(sTmpProp);
		var pos_image_end = axl_2.indexOf(sTmpImage);
		var sTmpAxl_1 = axl_2.substring(0,pos_prop_end+sTmpProp.length);
		var sTmpAxl_2 = axl_2.substring(pos_image_end);
		var iTmpPointsNum = point_unique_id;
		var sGeocodePrint = '';
		for (var tmpPID=0;tmpPID<iTmpPointsNum;tmpPID++) {
			if (aPoint_x[tmpPID] > 0 && aPoint_y[tmpPID] > 0 ) {
				var iTmpCookie = isPointCookie(aPoint_layer_id[tmpPID], aPoint_arc_point_id[tmpPID]);
				var tmpLayerID = getLayerIndexByLayerID(aPoint_layer_id[tmpPID]);
				var iTmpPosSaved = cookies_saved_position(aPoint_layer_id[tmpPID], aPoint_arc_point_id[tmpPID]);
				var sTmpLabel = (iTmpPosSaved==-1?aPoint_id[tmpPID]:aLetters[iTmpPosSaved]);
				255,240,2
				if (iTmpPosSaved == -1) {
					sGeocodePrint += geocodeGeneratePrint((tmpLayerID+'_'+aPoint_arc_point_id[tmpPID]), sTmpLabel, aPoint_longitude[tmpPID], aPoint_latitude[tmpPID], '6,86,6', '20', '255,255,255', '255,255,255');
				} else {
					sGeocodePrint += geocodeGeneratePrint((tmpLayerID+'_'+aPoint_arc_point_id[tmpPID]), sTmpLabel, aPoint_longitude[tmpPID], aPoint_latitude[tmpPID], '6,86,6', '20', '255,240,2', '255,255,255');
				}
			}
		}
		axl_2 = sTmpAxl_1 + '<LAYER type="acetate" name="GeoCode_acetate" visible="true">\n'+sGeocodePrint+
		'<OBJECT units="pixel">'+
		'<TEXT coords="20 5" label="EnchantedMountains.info 1-800-331-0543">'+
		'<TEXTMARKERSYMBOL font="Arial" fontsize="12" fontstyle="bold" antialiasing="true" transparency="0.3" fontcolor="51,102,0" />'+
		'</TEXT>'+'</OBJECT>'+
		'<OBJECT units="pixel">'+
		'<TEXT coords="20 20" label="'+print_map.width+' x '+print_map.height+'">'+
		'<TEXTMARKERSYMBOL font="Arial" fontsize="11" fontstyle="bold" antialiasing="true" transparency="0.3" fontcolor="51,102,0" />'+
		'</TEXT>'+'</OBJECT>'+
		'</LAYER>\n' + sTmpAxl_2;

		debug('axl 2 print: '+axl_2);
		axl_print = axl_1 + '<IMAGESIZE height="'+print_map.height+'" width="'+print_map.width+'" />' + axl_2;
	}
	debug('axl_print='+axl_print);
	return axl_print;
}

function geocodeGeneratePrint(tmpID, tmpLabel, tmpX, tmpY, tmpPointColor, tmpPointSize, tmpFontColor, tmpGlow) {
	// draw the point . . . also used to display any point with a label on map
	var sTmp='';
	//sTmp += '<LAYER type="acetate" name="geocode_'+tmpID+'">\n';
	//sTmp += '<OBJECT units="database">\n<POINT coords="' + tmpX + coordsDelimiter + tmpY + '">\n';
	//sTmp += '<SIMPLEMARKERSYMBOL type="circle" color="' + tmpPointColor + '" width="' + tmpPointSize +'" outline="255,255,255" overlap="false" />';
	//sTmp += '<RASTERMARKERSYMBOL overlap="true" url="http://maps.cattco.org/website/tourism/graphics/marker_test.gif" image="C:\ArcIMS\Website\tourism\graphics\marker_test.gif" transparency="1.0" antialiasing="false" />';
	//sTmp += '\n</POINT></OBJECT>\n';
	
	//if (geocodeLabel!="") {
	sTmp += '<OBJECT units="database">\n <TEXT coords="' + tmpX + coordsDelimiter + tmpY + '" label="' + tmpLabel + '">\n';
	sTmp += '  <TEXTMARKERSYMBOL transparency="0.9" fontcolor="' + tmpFontColor + '" fontsize="22" halignment="center" valignment="center" overlap="false" outline="0,0,0" antialiasing="true" />\n </TEXT>\n</OBJECT>\n';
		/* glowing="255,255,255" shadow="196,223,155" */
	//}
	//sTmp += '</LAYER>\n';
	return sTmp;
}

function printResponse_for_print(){

	if (http_print.readyState == 4) {
		if (http_print.status == 200) {

			isWorking = false;
			var result = http_print.responseText;
			var xmlDoc;

			if(document.implementation && document.implementation.createDocument) {
				// MOZILLA
				xmlDoc = document.implementation.createDocument("", "", null);
				xmlDoc.async="false";
				//info("Response Header: "+http_print.GetResponseHeader("Content-Type"));
				info("Result = "+result);
				xmlDoc.loadXML(result);
				debug("completed xmlDoc.loadXML");
			} else if (window.ActiveXObject){
				//IE
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(result);
			}

			//debug("Result:"+result);
			var theURL = xmlDoc.getElementsByTagName("OUTPUT").item(0).getAttribute("url");

			print_map.image = theURL;

		}else alert("Error retreiving data");

	}
	em_print_window();
}







//set the size of the print image
function setPrintSize(s){
	var w, h, r;
	r = 0.66;
	switch(s) {
		case 'letter':
		w = 795;
		r = 0.55;
		//document.thePrintForm.width.value = 600;
		//document.thePrintForm.height.value = 400;
		break;

		case 'tabloid':
		w = 1000;
		//document.thePrintForm.width.value = 1000;
		//document.thePrintForm.height.value = 600;
		break;

		case 'dsize':
		w = 2200;
		//document.thePrintForm.width.value = 2200;
		//document.thePrintForm.height.value = 1400;
		break;

		case 'esize':
		w = 3000;
		//document.thePrintForm.width.value = 3000;
		//document.thePrintForm.height.value = 2200;
		break;
		default:
		w = 600;
		//document.thePrintForm.width.value = 600;
		//document.thePrintForm.height.value = 450;
		break;
	}	// end switch
	h = w * r;
	document.thePrintForm.width.value = w;
	document.thePrintForm.height.value = h;
}	// end function

//geocode = geocodeGenerate((tmpLayerID+'_'+tmpPointID), pointName, xNum, yNum, '6,86,6', '20', '255,255,255', '255,255,255');
