
var DEFAULT_THEMATIC_FIELD = "CPEARN";
var DEFAULT_THEMATIC_SIGN = "dollar";
var DEFAULT_THEMATIC_FIELD_DESC = "Hourly earning in $";
//var MAPIMAGE_DATASOURCE = "icheung68:TA_Streets_GIWIS";
var MAPIMAGE_DATASOURCE = "bam";
var SCALE_ADDRESS_LOCATION = 400000;

var zoomScaleList = new Array();
zoomScaleList[0] = 5000;
zoomScaleList[1] = 10000;
zoomScaleList[2] = 50000;
zoomScaleList[3] = 100000;
zoomScaleList[4] = 500000;
zoomScaleList[5] = 1000000;
zoomScaleList[6] = 7000000;

var myExplorer;
var currentLocation = null;
var currentGroupLayerType = "vectorGroupLayer";
var poiMarkers = new Array();
var locMarkerStyle = null;
var thematicData = null;
var firstTimeThematic = true;
var maxExtent = new AWLatLonExtent(36.977733,-109.070602,41.02613,-101.995026);
var isStartup = true;

var thematicSign = DEFAULT_THEMATIC_SIGN;
var thematicField = DEFAULT_THEMATIC_FIELD;
var thematicFieldDesc = DEFAULT_THEMATIC_FIELD_DESC;
var thematicValues = null;
var timerId = 1;

var lastSearchLocation = null;
var lastSearchDistance = null;
var lastSearchWhereClause = null;
var lastSearchType = null;
var lastSearchLayer = null;
var lastSearchLon = null;
var lastSearchLat = null;
var lastSearchExtent = null;
var lastSearchCount = 0;


jsonurl = "JSON-RPC";
jsonrpc = null;

//////////////////////////////////////////////////////
///////////////// initialize /////////////////////////
//////////////////////////////////////////////////////

//loads the ArcWeb Explorer SWF map
function onBodyLoad() {

	AWUtils.insertMap("explorer", "dc8b794b9b4199aa636dd7dbff433a71", {"showMenu":"false", glt:"vectorGroupLayer", ds:MAPIMAGE_DATASOURCE});

	// initialize JSON-RPC
	try {
		jsonrpc = new JSONRpcClient(jsonurl);
	} catch(e) {
		if(e.message) {
			alert(e.message);
		} else {
			alert(e);
		}
	}
}

function onCreationComplete()
{
	// creating an instance of AWMap
	myExplorer = new AWMap("explorer");
	
	myExplorer.setLatLonExtent( maxExtent );
	
	myExplorer.setMouseHandler("SmoothPanMouseHandler");
    myExplorer.addMapChangedListener("onMapChanged");
	
	locMarkerStyle = new AWImgMarkerStyle();
	locMarkerStyle.id = "locMarker";
	locMarkerStyle.source = "http://arcweb.esri.com/out/mapicons/raster/cross_red.gif";
	locMarkerStyle.width = 16;
	locMarkerStyle.height = 16;
	locMarkerStyle.anchorX = 8;
	locMarkerStyle.anchorY = 8;
	myExplorer.addMarkerStyle(locMarkerStyle);

	// find location
	doLocationSearch();
}

//////////////////////////////////////////////////////
///////////////// navigation /////////////////////////
//////////////////////////////////////////////////////

function panMap(xPercent,yPercent) {
	// move %
	valueX = xPercent/100;
	valueY = yPercent/100;
	
	var extent = myExplorer.getLatLonExtent();
	var width = extent.maxLon-extent.minLon;
	var height = extent.maxLat-extent.minLat;
	
	extent.minLon = extent.minLon + valueX*width;
	extent.minLat = extent.minLat + valueY*height;
	extent.maxLon = extent.maxLon + valueX*width;
	extent.maxLat = extent.maxLat + valueY*height;

	if (currentGroupLayerType == "thematicGroupLayer")
		myExplorer.removeGroupLayer("thematicGroupLayer");
	myExplorer.setLatLonExtent( extent);
	if (currentGroupLayerType == "thematicGroupLayer")
		doThematicSearch();
}

function refreshMapByScale(scale) {
	var extent = myExplorer.getLatLonExtent();
	var center = new AWLatLon((extent.maxLat+extent.minLat)/2.0,(extent.maxLon+extent.minLon)/2.0);

	if (currentGroupLayerType == "thematicGroupLayer")
		myExplorer.removeGroupLayer("thematicGroupLayer");
	myExplorer.centerAndScale( center, scale );
	if (currentGroupLayerType == "thematicGroupLayer")
		doThematicSearch();
}

//////////////////////////////////////////////////////
///////////////// events /////////////////////////////
//////////////////////////////////////////////////////

function onMapChanged(event) {
    // make sure we stay inside the max extent
	newExtent = myExplorer.getLatLonExtent();
	if (newExtent.minLon < maxExtent.minLon 
		&& newExtent.maxLon > maxExtent.maxLon) {
		newExtent.minLon = maxExtent.minLon;
		newExtent.maxLon = maxExtent.maxLon;
	} else if (newExtent.minLon < maxExtent.minLon) {
		newExtent.maxLon = newExtent.maxLon + (maxExtent.minLon-newExtent.minLon);
		if (newExtent.maxLon > maxExtent.maxLon)
			newExtent.maxLon = maxExtent.maxLon;
		newExtent.minLon = maxExtent.minLon;
	} else if (newExtent.maxLon > maxExtent.maxLon) {
		newExtent.minLon = newExtent.minLon - (newExtent.maxLon-maxExtent.maxLon);
		if (newExtent.minLon < maxExtent.minLon)
			newExtent.minLon = maxExtent.minLon;
		newExtent.maxLon = maxExtent.maxLon;
	}
	if (newExtent.minLat < maxExtent.minLat 
		&& newExtent.maxLat > maxExtent.maxLat) {
		newExtent.minLat = maxExtent.minLat;
		newExtent.maxLat = maxExtent.maxLat;
	} else if (newExtent.minLat < maxExtent.minLat) {
		newExtent.maxLat = newExtent.maxLat + (maxExtent.minLat-newExtent.minLat);
		if (newExtent.maxLat > maxExtent.maxLat)
			newExtent.maxLat = maxExtent.maxLat;
		newExtent.minLat = maxExtent.minLat;
	} else if (newExtent.maxLat > maxExtent.maxLat) {
		newExtent.minLat = newExtent.minLat - (newExtent.maxLat-maxExtent.maxLat);
		if (newExtent.minLat < maxExtent.minLat)
			newExtent.minLat = maxExtent.minLat;
		newExtent.maxLat = maxExtent.maxLat;
	}
	myExplorer.setLatLonExtent(newExtent);	
	
	if (currentGroupLayerType == "thematicGroupLayer") {
		doThematicSearch();
		var legend = myExplorer.getGroupLayerProperty("thematicGroupLayer","legendUrl");
		document.getElementById("legendDiv").innerHTML = '<img src="'+legend+'" border="0">';
	} else {
		document.getElementById("legendDiv").innerHTML = "";	
	}
}

function checkLegendArrived() {
	if (myExplorer.getGroupLayerProperty("thematicGroupLayer","legendUrl") != null
		&& myExplorer.getGroupLayerProperty("thematicGroupLayer","legendUrl").length > 0) {
		var legend = myExplorer.getGroupLayerProperty("thematicGroupLayer","legendUrl");
		document.getElementById("legendDiv").innerHTML = '<img src="'+legend+'" border="0">';
		clearInterval(timerId);
	}
}

//////////////////////////////////////////////////////
///////////////// change /////////////////////////////
//////////////////////////////////////////////////////

function changeMapType(type) {
	// s - street; t - thematic
	if (type == 't' && currentGroupLayerType == "vectorGroupLayer") {
		document.getElementById("ThematicOptionsDiv").style.display="block";
		if (firstTimeThematic) {
			doThematicGroupSearch();
			firstTimeThematic = false;
		}
		doThematicSearch();
		changeSearchOptions("t");
		timerId = setInterval("checkLegendArrived()",1000); // 1 sec
		
	} else if (type == 's' && currentGroupLayerType == "thematicGroupLayer") {
		document.getElementById("POIOptionsDiv").style.display="none";
		document.getElementById("ThematicOptionsDiv").style.display="none";
		myExplorer.removeGroupLayer("thematicGroupLayer");
		myExplorer.addGroupLayer("vectorGroupLayer",MAPIMAGE_DATASOURCE);
		currentGroupLayerType = "vectorGroupLayer";
		changeSearchOptions("p");
		document.getElementById("legendDiv").innerHTML = '';
	}
}

function changeSearchOptions(type) {
	// p - poi; t - thematic
	if (type == "p") {
		document.getElementById("POIDiv").style.display="block";
		document.getElementById("ThematicDiv").style.display="none";
		document.getElementById("POIOptionsDiv").style.display="block";
		document.getElementById("ThematicOptionsDiv").style.display="none";
	} else {
		document.getElementById("POIDiv").style.display="none";
		document.getElementById("ThematicDiv").style.display="block";
		document.getElementById("POIOptionsDiv").style.display="none";
		document.getElementById("ThematicOptionsDiv").style.display="block";
	}
}

function updateOptionsList() {
	var type = document.getElementById("TYPE").value;
	if (type == "onestop")
		document.getElementById("institutesOptionsDiv").style.visibility="hidden";
	else if (type == "institutes")
		document.getElementById("institutesOptionsDiv").style.visibility="visible";
}

//////////////////////////////////////////////////////
///////////////// location ///////////////////////////
//////////////////////////////////////////////////////

function drawPointLocation(lat,lon,label) {
	latLon = new AWLatLon(lat, lon);
	currentLocation = latLon;
	lastSearchLocation = label;
	
	myMarker = new AWMarker();
	myMarker.id = "loc";
	myMarker.latlon = latLon;
	myMarker.markerStyleId = "locMarker";
	myMarker.data = {label:label};
	myExplorer.removeMarker("loc");
	myExplorer.addMarker(myMarker);

	if (currentGroupLayerType == "thematicGroupLayer")
		myExplorer.removeGroupLayer("thematicGroupLayer");
	myExplorer.centerAndScale(latLon, SCALE_ADDRESS_LOCATION);
	if (currentGroupLayerType == "thematicGroupLayer")
		doThematicSearch();
}

function drawExtentLocation(lat,lon,minLat,minlon,maxLat,maxlon,label) {
	latLon = new AWLatLon(lat, lon);
	currentLocation = latLon;
	lastSearchLocation = label;

	myMarker = new AWMarker();
	myMarker.id = "loc";
	myMarker.latlon = latLon;
	myMarker.markerStyleId = "locMarker";
	myMarker.data = {label:label};
	myExplorer.removeMarker("loc");
	myExplorer.addMarker(myMarker);

	extent = new AWLatLonExtent(minLat,minlon,maxLat,maxlon);

	if (currentGroupLayerType == "thematicGroupLayer")
		myExplorer.removeGroupLayer("thematicGroupLayer");
	myExplorer.setLatLonExtent( extent);
	if (currentGroupLayerType == "thematicGroupLayer")
		doThematicSearch();
}

function doLocationSearch() {
	try {
		// clear previous search
		document.getElementById("TableDiv").innerHTML = "";
		myExplorer.removeMarker(poiMarkers);
		if (!isStartup)
			document.getElementById("BannerDiv").style.display = "none";
		
		place = document.getElementById("location").value;
		jsonrpc.location.findLocation(doLocationSearchCallBack,place);
	} catch(e)  {
		alert("Exception: \n\n" + e);
	}
}
function doLocationSearchCallBack(result,e) {		
	if (e != null) {
		alert(e.message);
	} else {
		if (result.matchType == "NOMATCH" || result.candidates.length == 0) {
			tableCont = '<div class="content" style="padding-left:12px; padding-top:10px; padding-right:10px; padding-bottom:20px; line-height:12px">';
			tableCont += '<table width="703" border="0" cellspacing="0" cellpadding="0" style="height:100% ">';
			tableCont += '<tr align="left" valign="top" height="200"> ';
			tableCont += '<td height="200"><span class="subheadallcapsgray">Place or Address could not be found in Colorado.</span></td>';
			tableCont += '</tr></table></div>';
			document.getElementById("TableDiv").innerHTML = tableCont;
		} else if (result.candidates.length == 1) {
			if (result.candidates[0].extent == null)
				drawPointLocation(result.candidates[0].point.y, result.candidates[0].point.x, result.candidates[0].desc1);
			else
				drawExtentLocation(result.candidates[0].point.y, result.candidates[0].point.x, result.candidates[0].extent.minY, result.candidates[0].extent.minX, result.candidates[0].extent.maxY, result.candidates[0].extent.maxX, result.candidates[0].desc1);

			// start a POI search right away	
			if (isStartup)
				doPOISearch();
				
		} else if (result.candidates.length > 1) {
			tableCont = searchResultsTableContentTop("SEARCH RESULTS - "+result.candidates.length+" Places Found",false);
			tableCont += "<tr class=\"contentbold\"><td>Place Name</td>";
			for (k = 0; k < result.candidates.length; k++) {
				tableCont += "</tr><tr class=\"content\">";
				if (result.candidates[k].extent == null)
					tableCont += "<td><A href=\"JavaScript:drawPointLocation("+result.candidates[k].point.y+","+result.candidates[k].point.x+",'"+result.candidates[k].desc1+"');\">"+result.candidates[k].desc1+"</A></td>";
				else
					tableCont += "<td><A href=\"JavaScript:drawExtentLocation("+result.candidates[k].point.y+","+result.candidates[k].point.x+","+result.candidates[k].extent.minY+","+result.candidates[k].extent.minX+","+result.candidates[k].extent.maxY+","+result.candidates[k].extent.maxX+",'"+result.candidates[k].desc1+"');\">"+result.candidates[k].desc1+"</A></td>";
			}
			tableCont += "</tr>"+searchResultsTableContentBottom();

			document.getElementById("TableDiv").innerHTML = tableCont;
		}
	}
}

//////////////////////////////////////////////////////
///////////////// POI ////////////////////////////////
//////////////////////////////////////////////////////

function addPOIMarker(lat, lon, num, label) {

	markerStyle = new AWImgMarkerStyle();
	markerStyle.id = "poiMarker"+num;
	markerStyle.source = "http://arcweb.esri.com/out/mapicons/raster/square_red_"+num+".gif";
	markerStyle.width = 16;
	markerStyle.height = 16;
	markerStyle.anchorX = 8;
	markerStyle.anchorY = 8;
	myExplorer.addMarkerStyle(markerStyle);

	marker = new AWMarker();
	marker.id = "poi"+num;
	marker.latlon = new AWLatLon(lat, lon);
	marker.markerStyleId = "poiMarker"+num;
	marker.data = {label:label};
	myExplorer.addMarker(marker);
	
	poiMarkers[poiMarkers.length] = "poi"+num;
}

function doPOISearch() {
	if (currentLocation == null) {
		alert("Please find a place or address first.");
		return;
	}
	
	layer = document.getElementById("TYPE").value;
	if (layer == "-1") {
		alert("Please select a type of school first.");
		return;
	}
	
	try {
		var distance = parseInt(document.getElementById("distance").value);
		var whereClause = null;
		if (layer == "institutes") {
			var type = document.getElementById("PROFIT").value;
			if (type != "-1")
				whereClause = "PROFIT = '"+type+"'";
		}
		// we need these again for creating a report
		lastSearchDistance = distance;
		lastSearchWhereClause = whereClause;
		lastSearchType = type;
		lastSearchLayer = layer;
		lastSearchLon = currentLocation.lon;
		lastSearchLat = currentLocation.lat;
		lastSearchCount = 0;

		// clean up
		myExplorer.removeMarker(poiMarkers);
		poiMarkers = new Array();
		document.getElementById("TableDiv").innerHTML = "";
		
		jsonrpc.queryPOI.findPOIs(doPOISearchCallBack,
								currentLocation.lon, currentLocation.lat,
								layer,whereClause,distance);
	} catch(e)  {
		alert("Exception: \n\n" + e);
	}
}
function doPOISearchCallBack(result,e) {		
	if (e != null) {
		alert("Error: \n\n" + e.message);
	} else {
		if (result.resultSet.rows != null && result.resultSet.rows.length > 0) {
			lastSearchCount = result.resultSet.rows.length;
			
			tableCont = "";
			if (result.resultSet.rows.length > 30)
				tableCont = '<span class="content">The map will only display up to 30 locations. You may want to narrow your search by types of organizations.  The table will list all locations.<br>&nbsp;</span>';

			if (lastSearchLayer == "institutes")
				tableCont += searchResultsTableContentTop("SEARCH RESULTS - "+result.resultSet.rows.length+" Educational Organizations found",true);
			else
				tableCont += searchResultsTableContentTop("SEARCH RESULTS - "+result.resultSet.rows.length+" Comprehensive Onestop Centers found",true);

			// header fields
			tableCont += "<tr>";
			tableCont += "<td class=\"contentbold\">#</td>";
			tableCont += "<td class=\"contentbold\">Name</td>";
			tableCont += "<td class=\"contentbold\">Address</td>";
			for (i = 0; i < result.resultSet.fields.length; i++) {
				var fieldName = result.resultSet.fields[i].name;
				if (result.shortFields.indexOf(","+fieldName+",") > -1
					&& result.noDisplayFields.indexOf(","+fieldName+",") == -1) {
					// write out the descriptive name
					if (fieldName != 'P1_URL') {
						tableCont += "<td class=\"contentbold\">"+result.fieldsHash.map[fieldName]+"</td>";
					}
				}
			}
			tableCont += "<td class=\"contentbold\">Distance</td>";
			tableCont += "</tr>";
			 
			var currentExtent = myExplorer.getLatLonExtent();
			var extentNeedsChange = false;
			for (k = 0; k < result.resultSet.rows.length; k++) {
				var lat = parseFloat(result.resultSet.rows[k].fieldValues[result.posLat]);
				var lon = parseFloat(result.resultSet.rows[k].fieldValues[result.posLong]);
				var name = result.resultSet.rows[k].fieldValues[result.posName];
				var name_url = result.resultSet.rows[k].fieldValues[result.fieldPosHash.map["I_URL"]];
				var p1_url = result.resultSet.rows[k].fieldValues[result.fieldPosHash.map["P1_URL"]];
				
				// we show only up to 30 markers on the map
				if (k < 30) {
					addPOIMarker(lat, lon, (k+1), name);
				
					// expand the current extent so we see all markers, plus 10%
					if (lat < currentExtent.minLat) {
						currentExtent.minLat = lat - ((currentExtent.maxLat-lat)*0.1);
						extentNeedsChange = true;
					}
					if (lat > currentExtent.maxLat) {
						currentExtent.maxLat = lat + ((lat-currentExtent.minLat)*0.1);
						extentNeedsChange = true;
					}
					if (lon < currentExtent.minLon) {
						currentExtent.minLon = lon - ((currentExtent.maxLon-lon)*0.1);
						extentNeedsChange = true;
					}
					if (lon > currentExtent.maxLon) {
						currentExtent.maxLon = lon + ((lon-currentExtent.minLon)*0.1);
						extentNeedsChange = true;
					}
				}
					
				tableCont += "<tr>";
				tableCont += "<td class=\"content\">"+(k+1)+"</td>";
				tableCont += "<td class=\"content\">";
				if (name_url != null && name_url.length > 0) {
					tableCont += "<A href=\""+name_url+"\" target=\"_blank\">"+name+"</A>";
				} else {
					tableCont += name;
				}
				tableCont += "</td>";
				tableCont += "<td class=\"content\">"+result.addresses[k]+"</td>";
				for (l = 0; l < result.resultSet.rows[k].fieldValues.length; l++) {
					var fieldName = result.resultSet.fields[l].name;
					var value = result.resultSet.rows[k].fieldValues[l];
					if (result.shortFields.indexOf(","+fieldName+",") > -1
						&& result.noDisplayFields.indexOf(","+fieldName+",") == -1) {
						if (fieldName == 'PROG1' && p1_url != null && p1_url.length > 0) {
							if (value != null && value.length > 0) {
								tableCont += "<td class=\"content\"><A href=\""+p1_url+"\" target=\"_blank\">"+value+"</A></td>";
							} else 
								tableCont += "<td class=\"content\"><A href=\""+p1_url+"\" target=\"_blank\">web site</A></td>";
						} else if (fieldName != 'P1_URL') {
							if (value != null && value.length > 0) {
								tableCont += "<td class=\"content\">"+value+"</td>";
							} else 
								tableCont += "<td class=\"content\">&nbsp;</td>";
						}
					}
				}
				tableCont += "<td class=\"content\">"+result.resultSet.rows[k].fieldValues[result.posDist]+"</td>";
				tableCont += "</tr>";
			}
			tableCont += searchResultsTableContentBottom();

			document.getElementById("TableDiv").innerHTML = tableCont;

			if (extentNeedsChange) {
				if (currentGroupLayerType == "thematicGroupLayer")
					myExplorer.removeGroupLayer("thematicGroupLayer");
				myExplorer.setLatLonExtent( currentExtent);
				if (currentGroupLayerType == "thematicGroupLayer")
					doThematicSearch();
			}
			
		} else {
			tableCont = '<div class="content" style="padding-left:12px; padding-top:10px; padding-right:10px; padding-bottom:20px; line-height:12px">';
			tableCont += '<table width="703" border="0" cellspacing="0" cellpadding="0" style="height:100% ">';
			tableCont += '<tr align="left" valign="top" height="200"> ';
			tableCont += '<td height="200"><span class="subheadallcapsgray">No facilities found.</span></td>';
			tableCont += '</tr></table></div>';
			document.getElementById("TableDiv").innerHTML = tableCont;
		}
	}
	isStartup = false;
}

//////////////////////////////////////////////////////
///////////////// thematic /////////////////////////////
//////////////////////////////////////////////////////

function doThematicSearch() {

	if (document.getElementById("thematicField") && document.getElementById("thematicField").value) {
		value = document.getElementById("thematicField").value;
		pos = value.indexOf("-");
		thematicField = value.substring(0,pos);
		thematicSign = value.substring(pos+1,value.length);
		
		if (thematicField.indexOf("JOB05") > -1)
			thematicFieldDesc = "No. of Jobs in 2005";
		else if (thematicField.indexOf("JOB15") > -1)
			thematicFieldDesc = "No. of Jobs in 2015";
		else if (thematicField.indexOf("EARN") > -1)
			thematicFieldDesc = "Hourly earnings in 2005";
		
	} // else use default values

	try {
		extent = myExplorer.getLatLonExtent();
		jsonrpc.queryThematic.findCountyData(doThematicSearchCallBack, thematicField, 
											extent.minLon,extent.minLat,extent.maxLon,extent.maxLat);
	} catch(e)  {
		alert("Exception: \n\n" + e);
	}
}
function doThematicSearchCallBack(result,e) {		
	if (e != null) {
		alert(e.message);
	} else {
		s = "";
		if (result.resultSet.rows.length > 0) {
			for (k = 0; k < result.resultSet.rows.length; k++) {
				var fips = parseInt(result.resultSet.rows[k].fieldValues[result.posFips]);
				var value = parseFloat(result.resultSet.rows[k].fieldValues[result.posThematicField]);
				if (s.length > 0) s += ",";
				s += "{key:\"0"+fips+"\", value:\""+value+"\"}";
			}
		}
		thematicValues = s;
		if (thematicSign != null && (thematicSign == "dollar" || thematicSign == "percent"))
			eval ('thematicData = {precision:0,sign:"'+thematicSign+'",data:['+s+']};');
		else
			eval ('thematicData = {precision:0,data:['+s+']};');
		generateThematicMap();
	}
}

function generateThematicMap() {

/*
	var layerVisiblity = [
			  {name:"by State", visibility:"on"},
			  {name:"State Borders", visibility:"on"},
			  {name:"Shaded Relief", visibility:"off"},
			  {name:"Place Borders", visibility:"off"},
			  {name:"Oceans and Seas Names", visibility:"off"},
			  {name:"Congressional District Borders", visibility:"off"},
			  {name:"County Borders", visibility:"off"},
			  {name:"Major Highways", visibility:"off"},
			  {name:"Countries", visibility:"off"},
			  {name:"Other Areas", visibility:"off"},
			  {name:"U.S. City Labels", visibility:"off"},
			  {name:"U.S. Cities", visibility:"off"},
			  {name:"Major Water Bodies", visibility:"off"},
			  {name:"Oceans and Seas", visibility:"off"}
			  ];
*/
	thematicLayerVisiblity = [
			  {name:"County Labels", visibility:"on"},
			  {name:"by County", visibility:"on"},
			  {name:"by State", visibility:"off"},
			  {name:"by Zip", visibility:"off"},
			  {name:"U.S. City Labels", visibility:"on"}
			  ];

	myExplorer.removeGroupLayer("vectorGroupLayer");
	myExplorer.removeGroupLayer("thematicGroupLayer");
	myExplorer.addGroupLayer("thematicGroupLayer", "ArcWeb:ESRI.DemographicsByCounty.US",
							 {alpha:75,
							 thematicData:thematicData,
							 layerVisiblity:thematicLayerVisiblity,
							 numClasses:5,
							 mapLegend:{autoextend:"true", height:10, width:150, title:thematicFieldDesc}
							 });
	currentGroupLayerType = "thematicGroupLayer";
}

function checkSelectionOnGroup() {
	if (document.getElementById("thematicField").value == -1) {
		// select the next item
		for (i = 0; i < document.getElementById("thematicField").options.length; i++) {
			if (document.getElementById("thematicField").options[i].selected == true) {
				document.getElementById("thematicField").options[i+1].selected = true;
				break;
			}
		}
	}
}

function doThematicGroupSearch() {
	try {
		jsonrpc.thematicGroups.getThematicGroupInfo(doThematicGroupSearchCallBack);
	} catch(e)  {
		alert("Exception: \n\n" + e);
	}
}
function doThematicGroupSearchCallBack(result,e) {		
	if (e != null) {
		alert(e.message);
	} else {
		divCont = '<select id="thematicField" onChange="JavaScript:checkSelectionOnGroup();">';
		var lastGroup = "";
		for (i = 0; i < result.resultSet.features.length; i++) {
			newGroup = result.resultSet.features[i].feature.fieldValues[result.posGroup].value;
			if (newGroup != lastGroup) {
				divCont += '<option value="-1">'+newGroup+'</option>';
				lastGroup = newGroup;
			}
			divCont += '<option value="'+result.resultSet.features[i].feature.fieldValues[result.posField].value+'-'+result.resultSet.features[i].feature.fieldValues[result.posSign].value+'">&nbsp;&nbsp;'+result.resultSet.features[i].feature.fieldValues[result.posDesc].value+'</option>';
		}
		divCont += '</select>';
		document.getElementById("ThematicFieldDiv").innerHTML = divCont;
		
		for (i = 0; i < document.getElementById("thematicField").options.length; i++) {
			if (document.getElementById("thematicField").options[i].value == DEFAULT_THEMATIC_FIELD+"-"+DEFAULT_THEMATIC_SIGN) {
				document.getElementById("thematicField").options[i].selected = true;
				break;
			}
		}
	}
}

//////////////////////////////////////////////////////
///////////////// report /////////////////////////////
//////////////////////////////////////////////////////

function callPrintPage() {
	lastSearchExtent = myExplorer.getLatLonExtent();
	window.open("print.html","GIWIS_PrintPreview","width=860,height=570,menubar=1,toolbar=1,location=0,status=0,directories=0,scrollbars=yes,resizable=1");
}

function callReportPage() {
	lastSearchExtent = myExplorer.getLatLonExtent();
	if (lastSearchLayer == "onestop")
		window.open("report_onestop.html","GIWIS_Report","width=860,height=570,menubar=1,toolbar=1,location=0,status=0,directories=0,scrollbars=yes,resizable=1");
	else
		window.open("report_institutes_options.html","GIWIS_Report","width=860,height=570,menubar=1,toolbar=1,location=0,status=0,directories=0,scrollbars=yes,resizable=1");
}

//////////////////////////////////////////////////////
///////////////// result table ///////////////////////
//////////////////////////////////////////////////////

function searchResultsTableContentTop(msg,withPrint) {
//	tableCont = '<table width="703" border="0" cellspacing="0" cellpadding="0" style="height:100% ">';
	tableCont = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
    tableCont += '<tr align="left" valign="top"> ';
    tableCont += '<td colspan="3" height="20" valign="top" bgcolor="#E4E4E4">';
    tableCont += ' <table width="100%" border="0" cellspacing="0" cellpadding="0">';
    tableCont += '   <tr valign="middle"> ';
    tableCont += '   <td valign="middle"><span class="subheadallcapsgray">&nbsp;&nbsp;&nbsp;'+msg+'</span></td>';
	if (withPrint) {
    	tableCont += '    <td width="35"><span class="subheadallcapsgray"><A href="JavaScript:callPrintPage();"><img src="images/print-icon.jpg" width="27" height="26" border="0"></A></span></td>';
    	tableCont += '    <td width="161" valign="middle"><span class="subheadallcapsgray"><A href="JavaScript:callPrintPage();" border="0">Print Preview</A></span></td>';
    	tableCont += '    <td width="161" valign="middle"><span class="subheadallcapsgray"><A href="JavaScript:callReportPage();" border="0">Generate Report</A></span></td>';
	} else {
    	tableCont += '    <td width="35"><span class="subheadallcapsgray"><img src="images/spacer.gif" width="27" height="26" border="0"></span></td>';
    	tableCont += '    <td width="161"><span class="subheadallcapsgray">&nbsp;</span></td>';
    	tableCont += '    <td width="161"><span class="subheadallcapsgray">&nbsp;</span></td>';
	}
    tableCont += '  </tr>';
    tableCont += ' </table>';
    tableCont += '</td>';
    tableCont += '</tr>';
    tableCont += '<tr align="left" valign="top"> ';
    tableCont += '<td width="3" style="background-image: url(images/rep_3.gif)"><img src="images/rep_3.gif" width="3" height="1"></td>';
    tableCont += '<td height="100%" width="100%" class="style5">';
	tableCont += '  <div class="content" style="padding-left:12px; padding-top:10px; padding-right:10px; padding-bottom:20px; line-height:12px">';
	tableCont += '  <table border="1" cellpadding="3" cellspacing="0" bordercolor="#e4e4e4">';
	return tableCont;
}

function searchResultsTableContentBottom() {
	tableCont = '	</table>';
	tableCont += '  </div>';
	tableCont += ' </td>';
    tableCont += '<td width="3" style="background-image:  url(images/rep_5.gif)">&nbsp;</td>';
    tableCont += '</tr>';
    tableCont += '<tr align="left" valign="top"> ';
    tableCont += '  <td><img src="images/u_3.gif" width="3" height="3"></td>';
    tableCont += '<td style="background-image:  url(images/rep_4.gif)"><img src="images/rep_4.gif" width="1" height="3"></td>';
    tableCont += '<td><img src="images/u_4.gif" width="3" height="3"></td>';
    tableCont += '</tr>';
    tableCont += '<tr align="left" valign="top"> ';
    tableCont += '  <td><img alt=""  src="images/u_3.gif" width="3" height="3"></td>';
    tableCont += '  <td style="background-image:  url(images/rep_4.gif)"><img alt=""  src="images/rep_4.gif" width="1" height="3"></td>';
    tableCont += '  <td><img alt=""  src="images/u_4.gif" width="3" height="3"></td>';
    tableCont += '</tr>';
    tableCont += '</table><br>&nbsp;';
	return tableCont;
}

//////////////////////////////////////////////////////
///////////////// util ///////////////////////////////
//////////////////////////////////////////////////////

function doNothing() {
}

