var pano;
var panoClient;
var sview;
var map;
var svoverlay = null;
var dynamicMap = null;
var map;
var zoom;
var bounds;
var blayer=14
var x;
var y;
var c;
//tiled map service variables
    var mapExtension;
    var gOverlays = null;
    var mapOptions ;
	var endpoints=[];
	var legend;


function load() {
urlParams(esri.urlToObject(location.href));
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("gmap"));
mapExtension = new esri.arcgis.gmaps.MapExtension(map);       
//  Add map controls     
map.addControl(new GLargeMapControl());
map.addControl(new GScaleControl());
map.addMapType(G_PHYSICAL_MAP);
map.addControl(new GMapTypeControl());
map.enableDoubleClickZoom();

// add and position custom street view control.
//var topleft=new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100,7));
//map.addControl(new StreetView_Control(), topleft);


// Set x,y center and default zoom level.

x ="-84.11";
y =28.2;
c=6;
map.setCenter(new GLatLng(y, x),c);

if (ezlat!=0) {

var point1 = new GLatLng(ezminy,ezminx);
var point4 = new GLatLng(ezmaxy,ezmaxx);
var bound2 = new GLatLngBounds(point1,point4);
//var bound = new GLatLngBounds();
//var point1 = new GLatLng(ezminy,ezminx);
//var point2 = new GLatLng(ezminy,ezmaxx);
//var point3 = new GLatLng(ezmaxy,ezminx);
//var point4 = new GLatLng(ezmaxy,ezmaxx);
// bound.extend(point1);
// bound.extend(point2);
// bound.extend(point3);
// bound.extend(point4);
//map.setZoom(map.getBoundsZoomLevel(bound));
//map.setCenter(new GLatLng(ezlat,ezlong),11);
map.setCenter(bound2.getCenter(), map.getBoundsZoomLevel(bound2)); 
ezlat=0;

//x =ezlong;
//y =ezlat;
//c=14;

}
//alert(bounds);


// function calls for the local dynamic local gis layers.
addDynamicMap();
showAllLayers();
// Heat maps:
//legend = document.getElementById("legend");
//hidelegend();     

// STREET VIEW STUFF
//StreetView_InitializePanorama(); 
//StreetView_panoDiv.style.visibility="hidden";
//StreetView_panoDiv.style.height="0px";       
//  END OF STREET VIEW STUFF

} // end is compatible
} // end load





//*********   GIS MAP DATA FUNCTIONS
function mapcallback(mapservicelayer) {
      map.addOverlay(mapservicelayer);
    }
    
function selectlayer(checkbox) {
// KK Show a specific layer from a checkbox list.
// Get the selected value from the selectedIndex; the layer value is the layer index in the map service.
// "c" is counter to itrate through all the checked boxes so the layer index passed to map service is always in order with no gaps.
// "v" is values of checkbox -- cprresponds to map service layer index
// "i" is index of actual checkboxes on the web form
var layers=[];
layers[0]=blayer;  // blayer is the community boundary layer for the community type.  Gets set in dynamic js in vb.net function.
c=0;   
for (i=0; i<document.form.cbLayers.length; i++){
if (document.form.cbLayers[i].checked==true){
c++
var v=document.form.cbLayers[i].value;
//alert("Checkbox "+v+", counter " +c+ " is checked!")    
		layers[c]=v;
		}   //end if
 }          // end for loop

dynamicMap.setVisibleLayers(layers); 
}           // END selectlayer

function showAllLayers() {
showLayer(blayer);
}

function showLayer(blayer) {
// toggle a single layer on using the index of the layer in the mxd
// also set optional boundary layer (bid). 
		var layers=[];
		layers[0]=blayer;
      	dynamicMap.setVisibleLayers(layers);
}  

function addDynamicMap() {
      dynamicMap = new esri.arcgis.gmaps.DynamicMapServiceLayer("http://gpvm002.gisplanning.net/ArcGIS/rest/services/florida/florida/MapServer", null,.65, null);
      GEvent.addListener(dynamicMap, "load", function(groundov) {
      map.addOverlay(groundov);
      dynamicMap.show();
      });
}

function resetMap() {
//resets map
mapExtension.removeFromMap(gOverlays);
load();
hidelegend();  
// reset heat map drop down list if it exists:
if (document.getElementById('lstHeatMap'))
  {document.getElementById('lstHeatMap').selectedIndex=0;}
      
// make sure layer controls boxes are unchecked:
for (i=0; i<document.form.cbLayers.length; i++){
 document.form.cbLayers[i].checked=false;
}
}
// end of local map functions  

// Performs a geocode against Google.
	function geocode(address) {
	
		dojo.byId("results").innerHTML = "";
		var addressValue = "";
		if (address) {
		addressValue = address;
		} else {
		addressValue = document.getElementById("address").value;
		}
		
		
		
		
		

		
		
		var geocoder = new GClientGeocoder;
		
	      	geocoder.getLatLng(addressValue, function(point) {	        
	        if (point) { 		  
	          // Set the GEOMETRY field
		  var pointGeometry = new esri.geometry.Point(point.x, point.y, new esri.SpatialReference({"wkid": 4326}));        
 
		  // Makes call to see if point is in Enterprise Zone
		  // Need to have proxy set up first
		 
		  isEntZone(pointGeometry,addressValue);
		  
	          map.addOverlay(new GMarker(point));
	          map.setCenter(point, 13);
	          //alert(test);
	         
	        }
	        
	        
	        
	       
	      });
	      
	      
	     
	      		if (ezlat!=0) {
	      				document.getElementById("ibtnPrint").href='ezlocator.asp?print=yes&ezone=' + ezname
	      				document.getElementById("lnkEmail").href='emailform.aspx?u=' + 'http://www.floridaprospector.com/ezlocator.asp?ezone=' + ezname
	      				}
	      		
	      		else {
	      		document.getElementById("ibtnPrint").href='ezlocator.asp?print=yes&address=' + addressValue
	      		document.getElementById("lnkEmail").href='emailform.aspx?u=' + 'http://www.floridaprospector.com/ezlocator.asp?address=' + addressValue
		}
		
	      
	      
    }
    
    function isEntZone(point,address) {      
    	identifyTask = new esri.tasks.IdentifyTask(dynamicMap.url);
    	identifyParameters = new esri.tasks.IdentifyParameters();
    	identifyParameters.geometry = point;
    	identifyParameters.layerIds=[14];
    	identifyParameters.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
    	identifyParameters.tolerance=0;
    	identifyParameters.returnGeometry = true;
    	minPoint = point.offset(-1,-1);
    	maxPoint = point.offset(1, 1);
    	identifyParameters.mapExtent = new esri.geometry.Extent(minPoint.x, minPoint.y, maxPoint.x, maxPoint.y, new esri.SpatialReference({"wkid": 4326}));    	
    	identifyTask.execute(identifyParameters, function(identifyResults) {
    	  if (identifyResults.length > 0) {
    	  
    	  
    	  //PM added to get name
    	  ;
    	 var idResults = identifyResults;
    	 
    	 var layerresults;
    	 layerresults = {displayFieldName:null,features:[]};
    	 layerresults.features.push(idResults[0].feature);
    	 var zonename=layerresults.features[0].attributes['ZONE_NAME']
    	  var zoneid=layerresults.features[0].attributes['ZONEID']
    	  var zoneid2=zoneid.replace('EZ-','')
    	   var address2=address.replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ')
    	  
    	    //var zonename=idResults[0].feature.attributes['ZONE_NAME'];
    	    //var zoneid=idResults[0].feature.attributes['ZONEID'];
    	    
    	    var zoneurl='http://www.floridaenterprisezone.com/PageView.asp?Org_id=' + zoneid2
    	    
    	    var zoneurllink='<a href="' + zoneurl + '">' + zoneid + '</a>'
    	   
    	    dojo.byId("results").innerHTML = "Address: " + address2 + "<br><br>Yes, address appears to be in an enterprise zone. Please contact the local zone coordinator for official confirmation.<br><br>Enterprise Zone: " +  zonename + "<br>" + "Zone ID: " + zoneurllink;
    	  } else {
    	    
    	    dojo.byId("results").innerHTML = "No, address does not appear to be in an enterprise zone. Please contact the local zone coordinator for questions and official address verification";
    	  }
    	  
    	  
    	
    	  
    	});
    	    	    	
    }
    
    function urlParams(urlObj) {
    	if (urlObj.query) {
    	//geocode(urlObj.query.address);
    	if (urlObj.query.address) {
    		geocode(urlObj.query.address + ', FL');
    		}
    	}
    }
    
