//
// File : weeds-rtwgmap-core.js
//
// Description : "Core" Javascript functions for the Weed Survey Google Map
//

   
    
	//
	// get all the gmarkers for the specified region
	//
	function getMarkersForRegion(regionId){
    	currentRegionId = regionId;
	    map.closeInfoWindow();    
    	closeDirections();
    	//unapplyFilter();
    	//filterChanged();
    	
	    for(var i = 0; i < regions.length; i++)
    	{
        	if(regions[i].id == regionId) {
    	        map.setCenter(new GLatLng(regions[i].latitude, regions[i].longitude), regions[i].zoomLevel);
	            globalRegion = regions[i];
        	    break;
	        }
    	}

		// the default is to get the rtwgmap lib to return/list the markers in the current view ...
		eval(getDataProviderClassName()).getMarkersForRegion(returnFilteredMarkers, globalRegion.id);
	}
	
 	
 	//
    // Initialization for the Bertha Map
    // Overrides the default so we can set the max zoom-in level
    // and other attributes differently than the other maps.
    //
    var createMap = function (){
        if (GBrowserIsCompatible()) {
			var mapDiv = document.getElementById("map");
	
            map = new GMap2(mapDiv);

	        GEvent.addListener(map, "zoomend",function(oldLevel, newLevel){ manageMarkers(oldLevel, newLevel); });
    	    GEvent.addListener(map, "moveend",function(){ manageMarkers(null, null); });
        	GEvent.addListener(map, "dragend",function(){ globalRegion = null; }); // user moved the map, there is no region now

			map.enableContinuousZoom();

            //Initialize directions
            //directionsPanel = document.getElementById("sideListing");//directions_div");
            directions = new GDirections(map);//, directionsPanel);
            directions.isVisible = false;
            GEvent.addListener(directions, "error", handleErrors);

      		GEvent.addListener(directions,"load", function() {
        		setTimeout('renderDirections(map,"map",directions,document.getElementById("sideListing"))', 1);
      		});
      
            var mt = map.getMapTypes();
            //Overwrite min and max resolution parameters
            for(var i = 0; i < mt.length; i++)
            {
                mt[i].getMinimumResolution = function () {return 5;}
                mt[i].getMaximumResolution = function () {return 10;}
            }

            map.addControl(new GLargeMapControl());
            
            recenterMap;

//               map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.visibility="hidden"; 

            GEvent.addListener(map, "move",function(){checkBounds();});                    
       
            var allowedBounds = new GLatLngBounds(new GLatLng(48.5,-120.0), new GLatLng(60.9,-109.0));
            
            function checkBounds(){
                  if(allowedBounds.contains(map.getCenter())){
                return;
                  }
              var c = map.getCenter();
              var xcoord = c.lng();
              var ycoord = c.lat();

              var aMaxX = allowedBounds.getNorthEast().lng();
              var aMaxY = allowedBounds.getNorthEast().lat();
              var aMinX = allowedBounds.getSouthWest().lng();
              var aMinY = allowedBounds.getSouthWest().lat();

              if(xcoord < aMinX){xcoord = aMinX;}
              if(xcoord > aMaxX){xcoord = aMaxX;}
              if(ycoord < aMinY){ycoord = aMinY;}
              if(ycoord > aMaxY){ycoord = aMaxY;}
              map.setCenter(new GLatLng(ycoord,xcoord));

            }
        }    
    }
