//variable to store hidden frame
var printingHiddenFrame;

//on load, create a hidden frame for printing the map
dojo.addOnLoad(function() {
  printingHiddenFrame = document.createElement("iframe");
  printingHiddenFrame.src = "http://aacoprod.aacounty.org/MyAA/pages/hiddenframe.html";
  dojo.style(printingHiddenFrame, "display", "none");
  dojo.body().appendChild(printingHiddenFrame);
});

//serialize the map's state and call layout.* through iframe
function printMap(m, type) {
  var state = {
    map: getMapState(m, type)
  }
  
  var iframeDocument = dojo.isIE ? printingHiddenFrame.contentWindow.document : printingHiddenFrame.contentDocument;
  iframeDocument.getElementById("appState").value = dojo.toJson(state);
  iframeDocument.PrintForm.submit();
}

//serialize map state into JSON
// {
//   extent: <Object, { xmin, ymin, xmax, ymax, spatialReference }>,
//   layers: <Object[], layer JSON>,
//   graphics: <Object[], { geometry, symbol, attributes, template }>
// }
function getMapState(m, mapType) {
  var layerStates = [], graphicsState = [], graphics = m.graphics.graphics, ReportContent;

  
  function checkTrashSite(){
	if(document.getElementById('trashSiteLink')){
		if(selectedAddress.annapolis){
			return "<TR><TD width='30%' style='font-weight:bold;'>City of Annapolis Site:</TD><TD width='70%'><a href='"+ document.getElementById('trashSiteLink').href + "'>" + document.getElementById('trashSiteLink').href + "</a></TD></TR><TR><TD>&nbsp;</TD></TR></TABLE>"
		}
		else{
			return "<TR><TD width='30%' style='font-weight:bold;'>Holiday Collection WebSite:</TD><TD width='70%'><a href='"+ document.getElementById('trashSiteLink').href + "'>" + document.getElementById('trashSiteLink').href + "</a></TD></TR><TR><TD>&nbsp;</TD></TR></TABLE>"
		}
	}
	else{
		return "</TABLE>";
	}
  }
  
  if (mapType == "gen"){
	  var trashLink = checkTrashSite();
	  //alert(trashLink);
	  ReportType = "General Information Report"; 
	  ReportContent =  
		  			"<TABLE BORDER='0' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-weight:bold;font-size:18pt;'><TD align='center'>MyAnneArundel</TD></TR>" +
		  			"<TR style='font-size:14pt;center;'><TD align='center'>General Information Report</TD></TR>" + 
		  			"<TR><TD>&nbsp;</TD></TR></TABLE> " +
		  			"<span style='font-size:8pt'><TABLE BORDER='0' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR><TD width='100%' style='font-size:9pt;font-weight:bold;'><img style='vertical-align:middle;' src='http://aacoprod.aacounty.org/MyAA/images/green-dot.png'>&nbsp;Address Entered:  " + selectedAddress.address  + " " + selectedAddress.city +   ", MD " + selectedAddress.zip + "</TD></TR></TABLE> " +
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD width='100%' style='border:1px solid black;' colspan='3'>County Council Information:</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>District:</TD><TD width='50%'>" + document.getElementById('repDistrict').innerHTML + "</TD>" +
		  			"<TD ROWSPAN='3' WIDTH='30%'ALIGN='center'>" + document.getElementById('repImg').innerHTML + "</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Representative:</TD><TD width='50%'>" + document.getElementById('repName').innerHTML + "</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>WebSite:</TD><TD width='50%'><a href='" + document.getElementById('repSiteLink').innerHTML + "'>" + document.getElementById('repSiteLink').href + "</a></TD></TR></TABLE>" +
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD style='border:1px solid black;' width='100%' colspan='3'>School Service Areas:</TD></TR> " +
		  			"<TR style='font-weight:bold;'><TD style='border:1px solid black;' width='20%'>Category</TD><TD style='border:1px solid black;font-weight:bold;' width='35%'>Name</TD><TD style='border:1px solid black;' width='45%'>Address</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Elementary</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('eSchoolName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('eSchoolAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Middle</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('mSchoolName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('mSchoolAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>High</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('hSchoolName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('hSchoolAddr').innerHTML + "</TD></TR> " +
		  			"</TABLE><BR>" +
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD width='100%' style='border:1px solid black;' colspan='3'>Bureau of Highways Information:</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Road District:</TD><TD width='80%'>" + document.getElementById('roadDistrictName').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Contact #</TD><TD width='80%'>" + document.getElementById('roadDistrictPhone').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='30%' style='font-weight:bold;'>Bureau of Highways WebSite:</TD><TD width='70%'><a href='" + document.getElementById('bohLink').href + "'>" + document.getElementById('bohLink').href + "</a></TD></TR><TR><TD>&nbsp;</TD></TR></TABLE>" +
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD width='100%' style='border:1px solid black;' colspan='3'>Trash/Recycling Pickup:</TD></TR> " +
		  			"<TR><TD width='30%' style='font-weight:bold;'>Trash Pickup Days:</TD><TD width='70%'>" + document.getElementById('trashDay').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='30%' style='font-weight:bold;'>Recycling Pickup Days:</TD><TD width='70%'>" + document.getElementById('recycleDay').innerHTML + "</TD></TR>" +
		  			trashLink + 
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD width='100%' style='border:1px solid black;' colspan='3'>Fire Department Service Area:</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Name:</TD><TD width='80%'>" + document.getElementById('fireDeptName').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Address:</TD><TD width='80%'>" + document.getElementById('fireDeptAddress').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='30%' style='font-weight:bold;'>Fire Department WebSite:</TD><TD width='70%'><a href='" + document.getElementById('fireLink').href + "'>" + document.getElementById('fireLink').href + "</a></TD></TR><TR><TD>&nbsp;</TD></TR></TABLE>" +
		  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD width='100%' style='border:1px solid black;' colspan='3'>Police Department Service Area</TD></TR> " +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Name:</TD><TD width='80%'>" + document.getElementById('policeDeptName').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='20%' style='font-weight:bold;'>Address:</TD><TD width='80%'>" + document.getElementById('policeDeptAddress').innerHTML + "</TD></TR>" +
		  			"<TR><TD width='30%' style='font-weight:bold;'>Police Department WebSite:</TD><TD width='70%'><a href='" + document.getElementById('policeLink').href + "'>" + document.getElementById('policeLink').href + "</a></TD></TR><TR><TD>&nbsp;</TD></TR></TABLE>" +
		  			"<BR>" + 
		  			"<TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0'>  " + 
		  			"<TR style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><TD style='border:1px solid black;' width='100%' colspan='3'>Nearest Points of Interest:</TD></TR> " +
		  			"<TR style='font-weight:bold;'><TD style='border:1px solid black;' width='20%'>Category</TD><TD style='border:1px solid black;font-weight:bold;' width='35%'>Name</TD><TD style='border:1px solid black;' width='45%'>Address</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>County Payment Center</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('payName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('payAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Library</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('libraryName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('libraryAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Senior Center</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('seniorName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('seniorAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Hospital</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('hospitalName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('hospitalAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Farmer's Market</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('farmName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('farmAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>MARC Station</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('marcName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('marcAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>MVA Location</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('mvaName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('mvaAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Park & Ride</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('parkrideName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('parkrideAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Post Office</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('postName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('postAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>VEIP Location</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('veipName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('veipAddr').innerHTML + "</TD></TR> " +
		  			"<TR><TD style='border:1px solid black;' width='20%'>Waste Management Convenience Center</TD><TD style='border:1px solid black;' width='30%'>" + document.getElementById('wmName').innerHTML + "</TD><TD style='border:1px solid black;' width='50%'>" + document.getElementById('wmAddr').innerHTML + "</TD></TR> " +
		  			"</TABLE></span><br><br>"
		  			;	 
	  
	  for (var i=genMap.parcelGraphicsLayer.graphics.length-1; i>=0; i--) {
			  graphicsState.push(genMap.parcelGraphicsLayer.graphics[i].toJson());
	  };
	  
	  for (var i=genMap.roadGraphicsLayer.graphics.length-1; i>=0; i--) {
		  graphicsState.push(genMap.roadGraphicsLayer.graphics[i].toJson());
	  };

	  for (var i=genMap.councilGraphicsLayer.graphics.length-1; i>=0; i--) {
		  graphicsState.push(genMap.councilGraphicsLayer.graphics[i].toJson());
	  };
  }
  else{
	var printArray =[];  
	
	function dataSort(a,b) {
		if(a.crimeDesc > b.crimeDesc){
			return 1;
		}
		else if(a.crimeDesc < b.crimeDesc){
			return -1;
		}
		else{
			return 0;
		}
	}
	
	crimeStats.crimeArray.sort(dataSort);
	
	ReportType = "Crime Statistics Report";
	
	if (document.getElementById('selectedCommunityCode').innerHTML != '350000'){
		for (i=0;i<crimeStats.crimeArray.length;i++) {
			if(crimeStats.crimeArray[i].crimeDesc == "Yearly Total"){
				var crimeArrayData = "<tr style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'><td style='border:1px solid black;'>" + crimeStats.crimeArray[i].crimeDesc + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year1 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year2 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year3 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].currentYear + "</td></tr>";
			}
			else{
				var crimeArrayData = "<tr><td style='border:1px solid black;'>" + crimeStats.crimeArray[i].crimeDesc + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year1 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year2 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].year3 + "</td><td align='center' style='border:1px solid black;'>" + crimeStats.crimeArray[i].currentYear + "</td></tr>";
			}
			printArray.push(crimeArrayData);
		}

		if (document.getElementById('communityCode').innerHTML == document.getElementById('selectedCommunityCode').innerHTML){
			 communityCodeNote = "";
		}
		else{
			communityCodeNote = "<TR><TD width='100%'style='border:none;font-size:9pt;'colspan='5'><b>Note: </b>The statistics listed below are for neighboring Community Code: <span style='font-weight:bold;'>" + document.getElementById('selectedCommunityCode').innerHTML + "</span></TD></TR>";
		}
		  
		  ReportContent = 
				"<TABLE BORDER='0' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
	  			"<TR style='font-weight:bold;font-size:18pt;'><TD align='center'>MyAnneArundel</TD></TR>" +
	  			"<TR style='font-size:14pt;center;'><TD align='center'>Crime Statistics Report</TD></TR>" + 
	  			"<TR><TD>&nbsp;</TD></TR></TABLE> " +
	  			"<span style='font-size:8pt'><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
	  			"<TR><TD width='5%' rowspan='2'><img style='vertical-align:middle;' src='http://aacoprod.aacounty.org/MyAA/images/green-dot.png'></TD><TD width='35%' style='font-size:9pt;font-weight:bold;vertical-align:middle;'><span style='vertical-align:middle;'>Address Entered:</span></TD><TD width='60%' style='font-size:9pt;vertical-align:middle;'>" + selectedAddress.address  + " " + selectedAddress.city +   ", MD " + selectedAddress.zip + "</TD></TR>" +
	  			"<TR><TD width='35%' style='font-size:9pt;font-weight:bold;'>Community Code:<br><span style='font-size:8pt;font-weight:none;'>(for address entered)</span></TD><TD width='60%'>" + document.getElementById('communityCode').innerHTML + "</TD></TR>" +
	  			"</TABLE> " +
	  			"<br><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>" +
	  			communityCodeNote + 
	  			"<tr style='font-size:9pt;font-weight:bold;background-color:#4C4545;color:white;border:1px solid black;'>" +
	  			"<td width='40%' style='border:1px solid black;'>Type of Incident</td>" +
	  			"<td width='15%' align='center' style='border:1px solid black;'>" + crimeStats.year_1 + "</td>" +
	  			"<td width='15%' align='center' style='border:1px solid black;'>" + crimeStats.year_2 + "</td>" +
	  			"<td width='15%' align='center' style='border:1px solid black;'>" + crimeStats.year_3 + "</td>" +
	  			"<td width='15%' align='center' style='border:1px solid black;'>" + crimeStats.current_Year + "</td></tr>" +
	  			printArray.join('') + "</table></span><br><br><b>Note:</b><span style='font-size:8pt'>The statistics reported are associated with the red shaded community code in the map image below</span>"; 
		  
		  for (var i=crimeMap.communityGraphicsLayer.graphics.length-1; i>=0; i--) {
			  graphicsState.push(crimeMap.communityGraphicsLayer.graphics[i].toJson());
		  };
	}
	else{
	  ReportContent =
			"<TABLE BORDER='0' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
  			"<TR style='font-weight:bold;font-size:18pt;'><TD align='center'>MyAnneArundel</TD></TR>" +
  			"<TR style='font-size:14pt;center;'><TD align='center'>Crime Statistics Report</TD></TR>" + 
  			"<TR><TD>&nbsp;</TD></TR></TABLE> " +
  			"<span style='font-size:8pt'><TABLE style='border:1px solid black;' CELLPADDING='1' CELLSPACING='0' width='100%'>  " + 
  			"<TR><TD width='5%' rowspan='2'><img style='vertical-align:middle;' src='http://aacoprod.aacounty.org/MyAA/images/green-dot.png'></TD><TD width='35%' style='font-size:9pt;font-weight:bold;vertical-align:middle;'><span style='vertical-align:middle;'>Address Entered:</span></TD><TD width='60%' style='font-size:9pt;vertical-align:middle;'>" + selectedAddress.address  + " " + selectedAddress.city +   ", MD " + selectedAddress.zip + "</TD></TR>" +
  			"<TR><TD width='35%' style='font-size:9pt;font-weight:bold;'>Community Code:<br><span style='font-size:8pt;font-weight:none;'>(for address entered)</span></TD><TD width='60%'>" + document.getElementById('communityCode').innerHTML + "</TD></TR>" +
  			"</TABLE> " +
  			"<TABLE BORDER='0' CELLPADDING='1' CELLSPACING='0' width='100%'>  " +
  			"<TR><TD>&nbsp;</TD></TR> " + 
  			"<TR><TD width='100%'style='border:none;font-size:9pt;'colspan='5'><b>Note: </b>Crime Statistics for the selected community (code: 350000) are maintained by the City of Annapolis and can be obtained from their Crime Statistics Site (http://www.ci.annapolis.md.us/info.asp?page=12647)</TD></TR> " +
  			"<TR><TD>&nbsp;</TD></TR> " +
  			"</TABLE>";
	  
	  for (var i=crimeMap.communityGraphicsLayer.graphics.length-1; i>=0; i--) {
		  graphicsState.push(crimeMap.communityGraphicsLayer.graphics[i].toJson());
	  };
	}
	
  }
 
  //serialize each layer's state
  dojo.forEach(m.layerIds, function(layerId) {
    layerStates.push( getLayerState(m.getLayer(layerId)));
  });
  
  //serialize graphics from last to first to retain graphics drawing order
  for (var i=graphics.length-1; i>=0; i--) {
    graphicsState.push(graphics[i].toJson());
  };
  
  return {
    extent: m.extent.toJson(),
    layers: layerStates,
    graphics: graphicsState,
    content:ReportContent,
    type: ReportType
  };
}

//return layer state as JSON
// {
//   id: <String>,
//   type: <String, class name>,
//   url: <String>,
//   visible: <Boolean>,
//   opacity: <Number>,
//   layerDefs: <String[], id:def>,
//   layers: <Number[]>
// }

function getLayerState(layer) {
//alert(" id:" + layer.id + " url:" + layer.url +  " visibleLayers:" + layer.visibleLayers);	
  return {	
    id: layer.id,
    type: layer.declaredClass,
    url: layer.url,
    visible: layer.visible,
    opacity: layer.opacity,
    layerDefs: layer.layerDefinitions,
    layers: layer.visibleLayers
  };
}


