var mapExtension, identifyTask, overlays;
          var gmap = null;
          var dynMapOv = null;
		  var geocoder = null;

      function initialize(){
        // create the map
        if (GBrowserIsCompatible()) {
        gmap = new GMap2(document.getElementById("map_canvas"));
		//var mapControl = new GMapTypeControl ();
        //gmap.addControl(new GLargeMapControl());
        gmap.addControl(new GMapTypeControl());
		gmap.removeMapType(G_SATELLITE_MAP);
		gmap.addMapType(G_PHYSICAL_MAP);
        gmap.setCenter(new GLatLng(39.85, -110), 5);
        gmap.enableScrollWheelZoom();
        gmap.setMapType(G_PHYSICAL_MAP);
		gmap.addControl(new GNavLabelControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7,38)));
        geocoder = new GClientGeocoder();

		
        // add zoom control....
        var boxStyleOpts = { opacity: .2, border: "2px solid blue" };
        var otherOpts = {
          buttonHTML: "<img src='zoom-button.gif' />",
          buttonZoomingHTML: "<img src='zoom-button-activated.gif' />",
          buttonZoomingHTML: 'Drag a square on the map (click here to reset)',
          buttonZoomingStyle: {background:'blue', color:'white', width:'100%', height:'100%'},
          buttonStartingStyle: {width: '17px', height: '17px'},
          overlayRemoveTime: 0 };
        gmap.addControl(new DragZoomControl(boxStyleOpts, otherOpts, {}),
            new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(23,7)));
        // add large map control under the zoom control since the large map control has different sizes...
        gmap.addControl(new GLargeMapControl3D(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,38)));

        mapExtension = new esri.arcgis.gmaps.MapExtension(gmap);

        // create a dynamic map service layer
        var dynamicMap = new esri.arcgis.gmaps.DynamicMapServiceLayer("http://map.commercecrg.com/ArcGIS/rest/services/Web/MapSearch/MapServer", null, 1, dynmapcallback);

        // create an identify task
        identifyTask = new esri.arcgis.gmaps.IdentifyTask("http://map.commercecrg.com/ArcGIS/rest/services/Web/MapSearch/MapServer");

        // register click event listener for the map
        GEvent.addListener(gmap, "click", identify);
        }
      }

      function identify(overlay, latLng) {
        if (overlay) return;
        clearResults();

        // set the identify parameters
        var identifyParameters = new esri.arcgis.gmaps.IdentifyParameters();
        identifyParameters.geometry = latLng; // location where the user clicked on the map
        identifyParameters.tolerance = 4;
        identifyParameters.layerIds = [ 0 ];
        identifyParameters.layerOption = "all";
        identifyParameters.bounds = gmap.getBounds();
        var mapSize = gmap.getSize();
        identifyParameters.width = mapSize.width;
        identifyParameters.height = mapSize.height;

        // execute the identify operation
        identifyTask.execute(identifyParameters, function(response, error) { // function to be called when the result is available
          // display error message (if any) and return
          if (hasErrorOccurred(error)) return;

          // note that the location where the user clicked on the map (latLng) is visible in this function through closure
          addResultToMap(response, latLng);
        });
      }

function addResultToMap(response, point) {
          // aggregate the result per map service layer
          var idResults = response.identifyResults;

          // if there are no results do not add anything
          if (idResults == 0){  
            return;
            }
          
          layers = { "0": [] };
          for (var i = 0; i < idResults.length; i++) {            
            var result = idResults[i];
            layers[result.layerId].push(result);
          }


          // create and show the info-window with tabs, one for each map service layer
          var tabs = [];
          for (var layerId in layers) {
            var results = layers[layerId];
            var count = results.length;
            var label = "", content = "";
            switch(layerId) {
              case "0":
                // label = "Active Listings";
                // content = "Total features returned: <b>" + count + "</b>";
				content += "<div id='mapping-search-bubble'><table class='identify' border='0'>";
                if (count == 0) break;
                // content += "<table class='identify' border='0'><th>Property</th><th>Address</th><th>City</th><th>State</th><th>Price</th><th>Rate</th><th>Size</th><th>Size2</th><th>Contact</th><th>EMail</th><th>Factsheet</th>";
                for (var j = 0; j < count; j++) {
                  var attributes = results[j].feature.attributes;
                  content += "<tr>";
                  content += "<td><b>" + attributes["FACTSHEET"]  + "</b></td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["ADDRESS"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["CITY"] + ", " + attributes["STATE"] + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + "Major Use: " + attributes["SUB_DBF"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["NICEPRICE"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["NICERATE"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["NICESIZE"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["NICESIZE2"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";
                  content += "<td>" + attributes["BROKERPAGE"] + " " + attributes["EMAIL"]  + "</td>";
				  content += "</tr>";
				  content += "<tr>";                  
                  content += "<td id='last_row'>" + attributes["PHOTOPATH"] + "</td>";
                  content += "</tr>";
                }
                content += "</table></div>";
                break;
            }
            tabs.push(new GInfoWindowTab(label, content));
          }
          gmap.openInfoWindowTabsHtml(point, tabs);
      }

    function dynmapcallback(groundov) {
      //Add groundoverlay to map using gmap.addOverlay()
      gmap.addOverlay(groundov);
      dynMapOv = groundov;
    }
      function showFeature(layerId, index) {
        mapExtension.removeFromMap(overlays);
        var idResult = layers[layerId][index];
        overlays = mapExtension.addToMap(idResult, {polygonOptions: { clickable: false}});
      }

      function clearResults() {
        mapExtension.removeFromMap(overlays);
        gmap.closeInfoWindow();
      }

      function hasErrorOccurred(error) {
        if (error) {
          alert("Error " + error.code + ": " + (error.message || (error.details && error.details.join(" ")) || "Unknown error" ));
          return true;
        }
        return false;
      }
	  
	   function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              gmap.setCenter(point, 15);
                        }
          }
        );
      }
    }


