function executeQuery(lyr, queryTask) {
	  gmap.clearOverlays();
	  
	  var curActiveLyr = null;
	  if (document.getElementById('active_trails').src.indexOf('id_active') != -1) {
	  	curActiveLyr = "Trails";
	  }
	  if (document.getElementById('active_parks').src.indexOf('id_active') != -1) {
		curActiveLyr = "Parks";
	  }
	  if (document.getElementById('active_zoning').src.indexOf('id_active') != -1) {
		curActiveLyr = "Zoning";
	  }

	  var inputs = document.getElementsByTagName('img');
	  for(var i=0; i < inputs.length; i++) {
			if(inputs[i].getAttribute('name') == 'active') {
				inputs[i].src = 'images/id_inactive.gif';
				if (inputs[i].getAttribute('id') == 'active_parks') {
					if (mapScale > parkMinScale) {
						document.getElementById('active_parks').src = "images/id_off.gif";
					} else {
						document.getElementById('active_parks').src = "images/id_inactive.gif";
					}
					if (mapScale === undefined) {
						document.getElementById('active_parks').src = "images/id_inactive.gif";
					}
				}
				//if (inputs[i].getAttribute('id') == 'active_landuse') {
				//	if (mapScale > landuseMinScale) {
				//		document.getElementById('active_landuse').src = "images/id_off.gif";
				//	} else {
				//		document.getElementById('active_landuse').src = "images/id_inactive.gif";
				//	}
				//}
				if (inputs[i].getAttribute('id') == 'active_zoning') {
					if (mapScale > zoningMinScale) {
						document.getElementById('active_zoning').src = "images/id_off.gif";
					} else {
						document.getElementById('active_zoning').src = "images/id_inactive.gif";
					}
				}

			}
	  }
	  


	if (curActiveLyr != lyr) {

		  // create the query
		  var query = new esri.arcgis.gmaps.Query();
		  query.where = "1=1";
		  switch(lyr) {
			case "Trails":
				qryLyr = "Trails";
				query.outFields = ["NAME"];
				document.getElementById('active_trails').src = "images/id_active.gif";
				break;
			case "Parks":
				qryLyr = "Parks";
				query.outFields = ["Name"];
				document.getElementById('active_parks').src = "images/id_active.gif";
				break;
			//case "Landuse":
			//	qryLyr = "Landuse";
			//	query.outFields = ["LandUse"];
			//	document.getElementById('active_landuse').src = "images/id_active.gif";
			//	break;
			case "Zoning":
				qryLyr = "Zoning";
				query.outFields = ["ZONING"];
				document.getElementById('active_zoning').src = "images/id_active.gif";
				break;
		  }


		  // execute the query
		  queryTask.execute(query, false, function(featureSet, error) { // callback
			// display error message (if any)
			if (error) {
			  alert("Error " + error.code + ": " + (error.message || (error.details && error.details.join(" ")) || "Unknown error" ));
			  return;
			}

			// add the feature set to the map
			var features = featureSet.features, geometry, attributes;
			//var featureType = featres.geometryType;
			for (var i = 0; i < features.length; i++) { // process each feature in the feature set
			  geometry = features[i].geometry;
			  attributes = features[i].attributes;
			  for (var j = 0; j < geometry.length; j++) { // Feature.geometry is an array of GPolygon/GPolyline/GMarker
				var geom = geometry[j];
				switch(lyr) {
					case "Trails":
						geom.setStrokeStyle({ color: "#000000", opacity: 0.00 });
						break;
					case "Parks":
						geom.setFillStyle({ color: "#000000", opacity: 0.00 });
						geom.setStrokeStyle({ color: "#000000", opacity: 0.00 });
						break;
					case "Landuse":
						geom.setFillStyle({ color: "#000000", opacity: 0.00 });
						geom.setStrokeStyle({ color: "#000000", opacity: 0.00 });
						break;
					case "Zoning":
						geom.setFillStyle({ color: "#000000", opacity: 0.00 });
						geom.setStrokeStyle({ color: "#000000", opacity: 0.00 });
						break;
				}

				GEvent.addListener(geom, "mouseover", GEvent.callbackArgs(geom, onMouseOverFunc, attributes));
				GEvent.addListener(geom, "mouseout", onMouseOutFunc);
				gmap.addOverlay(geom);
			  }
			}
			// Note that we are not using the MapExtension class here to add the FeatureSet
			// to the map as it supports only "click-to-open-infowindow" workflow
		  });
		  
	}
	  
	  
}

// show info window for the feature when the user moves the mouse over it
function onMouseOverFunc(attributes) {
  switch(qryLyr) {
	case "Trails":
		// highlight the feature
		var polyline = this;
		polyline.setStrokeStyle({ color: "#FF0000", opacity: 0.25 });

		// show the infowindow
		var html = "<b>Name:</b> " + attributes["NAME"];
		gmap.openInfoWindowHtml(polyline.getBounds().getCenter(), html);

		break;
	case "Parks":
		// highlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#FF0000", opacity: 0.25 });
		polygon.setStrokeStyle({ color: "#FF0000", opacity: 0.25 });

		// show the infowindow
		var html = "<b>Name:</b> " + attributes["Name"];
		gmap.openInfoWindowHtml(polygon.getBounds().getCenter(), html);

		break;
	case "Landuse":
		// highlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#FF0000", opacity: 0.25 });
		polygon.setStrokeStyle({ color: "#FF0000", opacity: 0.25 });

		// show the infowindow
		var html = "<b>Landuse:</b> " + attributes["LandUse"];
		gmap.openInfoWindowHtml(polygon.getBounds().getCenter(), html);

		break;
	case "Zoning":
		// highlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#FF0000", opacity: 0.25 });
		polygon.setStrokeStyle({ color: "#FF0000", opacity: 0.25 });

		// show the infowindow
		var html = "<b>Zoning:</b> " + attributes["ZONING"];
		gmap.openInfoWindowHtml(polygon.getBounds().getCenter(), html);

		break;
  }

}

// close info window when the user moves the mouse out of a feature
function onMouseOutFunc() {
  switch(qryLyr) {
	case "Trails":
		// unhighlight the feature
		var polyline = this;
		polyline.setStrokeStyle({ color: "#000000", opacity: 0.00 });

		break;
	case "Parks":
		// unhighlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#000000", opacity: 0.00 });
		polygon.setStrokeStyle({ color: "#000000", opacity: 0.00 });

		break;
	case "Landuse":
		// unhighlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#000000", opacity: 0.00 });
		polygon.setStrokeStyle({ color: "#000000", opacity: 0.00 });

		break;
	case "Zoning":
		// unhighlight the feature
		var polygon = this;
		polygon.setFillStyle({ color: "#000000", opacity: 0.00 });
		polygon.setStrokeStyle({ color: "#000000", opacity: 0.00 });

		break;
  }

  // hide the infowindow
  gmap.closeInfoWindow();
}

function zoningQueryEvent() {
	executeQuery('Zoning', queryZoningTask);
}

function parksQueryEvent() {
	executeQuery('Parks', queryParksTask);
}

function landuseQueryEvent() {
	executeQuery('Landuse', queryLanduseTask);
}

