var map;
var geoXml; 
var toggleState = 1;
var overlayControl;
var overmap;

function initialize() {
    if (GBrowserIsCompatible()) {
        // Need to add a placeholder to store the kml file?

        geoXml = new GGeoXml("http://www.savethechildren.org.uk/kroobay-new/bwi_assets/Kroo_Bay_1.kml"); 

        map = new GMap2(document.getElementById("map"));
        map.addControl(new GMapTypeControl());
        
        
        map.setCenter(new GLatLng(8.487992,-13.240719), 17); 
        map.addControl(new GLargeMapControl());
        
        
        // ====== Restricting the range of Zoom Levels =====
        // Get the list of map types      
        var mt = map.getMapTypes();      
        
        // Overwrite the getMinimumResolution() and getMaximumResolution() methods
        for (var i=0; i<mt.length; i++) {
            mt[i].getMinimumResolution = function() {return 2;}
            mt[i].getMaximumResolution = function() {return 18;}
        }        
        
        map.setCenter(new GLatLng(8.487992,-13.240719), 17); 
        map.addControl(new GLargeMapControl());
        
        // Add a move listener to restrict the bounds range
        GEvent.addListener(map, "move", function() {
            checkBounds();
        });
      
        // The allowed region which the whole map must be within
        var allowedBounds = new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180));
      
        // If the map position is out of range, move it back
        function checkBounds() {
        // Perform the check and return if OK
            if (allowedBounds.contains(map.getCenter())) {
              return;
            }
            // It`s not OK, so find the nearest allowed point and move there
            var C = map.getCenter();
            var X = C.lng();
            var Y = C.lat();
            var AmaxX = allowedBounds.getNorthEast().lng();
            var AmaxY = allowedBounds.getNorthEast().lat();
            var AminX = allowedBounds.getSouthWest().lng();
            var AminY = allowedBounds.getSouthWest().lat();
            if (X < AminX) {X = AminX;}
            if (X > AmaxX) {X = AmaxX;}
            if (Y < AminY) {Y = AminY;}
            if (Y > AmaxY) {Y = AmaxY;}
            //alert ("Restricting "+Y+" "+X);
            map.setCenter(new GLatLng(Y,X));
         }
        
         map.addOverlay(geoXml);
         map.setMapType(G_SATELLITE_MAP);
     
         overlayControl = new GOverviewMapControl();
         map.addControl(overlayControl);
         setTimeout("checkOverview()",100);
    }//if
}//function 

function checkOverview() {
    overmap = overlayControl.getOverviewMap();
    if (overmap) {      
        setTimeout("overmap.setMapType(G_SATELLITE_MAP);",1);
    } else {
        setTimeout("checkOverview()",100);
    }
}
    
function toggleMyKml() {
    if (toggleState == 1) {
        map.removeOverlay(geoXml);
        toggleState = 0;
    } else {
        map.addOverlay(geoXml);
        toggleState = 1;
    }
}
