//
// File : bertha-rtwgmap-core.js
//
// Description : "Core" Javascript functions for the Bertha Worm Survey Google Map
//
// Contents    : 
//               getMarkersForRegion(regionId)

 		var mapImage ;
        var markerListing = "";
        var maxPrintableMarkers = 10;

	//
	// get all the gmarkers for the specified region
	//
	function getMarkersForRegion(regionId){
    	currentRegionId = regionId;
	    map.closeInfoWindow();    
    	closeDirections();

		// sf : 20080515 we're always filtering! No need to unapply
    	//document.getElementById("filterButton").checked = false;
    	//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);
	}
	
   var printableMapWindow;

	var update = function() {
		var listings = top.consoleRef.document.getElementById("listings");
		if(listings) {
			listings.innerHTML = markerListing;
			top.consoleRef.focus();
			top.consoleRef.print();
		} else {
			setTimeout('update()',1000);
		}
 	}
 	
 	
	//
 	//
 	// 	
    var getPrintableMapUrl = function() {    
	     var c = map.getCenter();
	     var xcoord = c.lng();
    	 var ycoord = c.lat();
        var center = ycoord + "," + xcoord;
	   	var markerListing = "";
	   	
	   	var mapImgUrl = "";
		mapImgUrl += "http://maps.google.com/staticmap?center=" + center ;
		mapImgUrl += "&zoom=" + map.getZoom();
		mapImgUrl += "&size=600x480";
		mapImgUrl += "&markers=";
		var count = 0;
		for(var i=0;i<printableMarkers.length && count < maxPrintableMarkers; i++) {
				
		    var gmarker = gmarkers[printableMarkers[i].id];           
            if( !gmarker.isHidden() && map.getBounds().contains( gmarker.getPoint() ) ) {                          
    			var character = count + 97; // 'a'
                var color = printableMarkers[i].color;
                
	    		var lat = printableMarkers[i].latitude;
    			var lng = printableMarkers[i].longitude;
    			
    			mapImgUrl += lat + "," + lng;
    			mapImgUrl += "," + color + unescape("%" + character.toString(16));
    			mapImgUrl += "%7C";
    			count ++;
    		}
		}
              
	 	return mapImgUrl;
 	}
 	
 	
    //
    // print the current listings
    // see printableMarkers, returnFilteredMarkers
    //
    var printCurrentListings = function(title) {
    
   		setCookie("printableMapUrl", getPrintableMapUrl());
   
    
	    top.consoleRef=window.open('/app68/listings/bertha/bertha_printable_map.jsp','myconsole',
									  'width=600,height=400'
									 +',menubar=0'
									 +',toolbar=1'
									 +',status=0'
								     +',scrollbars=1'
									 +',resizable=1');   	

		//
		//
		//
		markerListing = "<table border='0'>";
		var count = 0;
		for(var i=0;i<printableMarkers.length && count < maxPrintableMarkers; i++) {
				
		    var gmarker = gmarkers[printableMarkers[i].id];           
            if( !gmarker.isHidden() && map.getBounds().contains( gmarker.getPoint() ) ) {                          
    			var character = count + 65; // 'A'

	    		var lat = printableMarkers[i].latitude;
    			var lng = printableMarkers[i].longitude;
    			
    			if(count%2 == 0)
	    			markerListing += "<tr>";
    			
    			markerListing += "<td valign='top'>";

    			markerListing += "<br/><h1>"  +  unescape("%" + character.toString(16)) + "</h1>";    			
    		    markerListing += "<br/>" + printableMarkers[i].additionalInfo.trapCountsTableHtml;
    		    markerListing += "</td>";
    		    
    		    markerListing += "<td>&nbsp;</td>";
    		    
    			if(count%2 != 0)
	    		    markerListing += "</tr>";
	    		    
    			count ++;
    		}
		}
		markerListing += "</table>";
	
		setTimeout('update()',1000);
    }
 	
 	
 	
 	var printMap = function() {
		var counties = top.printableMapWindow.document.getElementById("counties");
		if(counties) {
			top.printableMapWindow.focus();
			top.printableMapWindow.print();
		} else {
			setTimeout('printMap()',1000);
		}
 	}
 	
 	
 	
 	//
    // 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.
    //
    // TODO determine which attributes can be modified across all maps
    // and make some common apis so the whole createMap() function doesn't
    // have to be copied/overriden
    //
    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));

            }
        }    
    }
 	
 	