/* namespace 'mapSetup' contains functionality that sets up the map */

var mapSetup; // acts as namespace for everything in this file

if (!mapSetup) {
    mapSetup = {};
}

(function () {

    /* Create the map */
    mapSetup.createMap = function () {
        
        SWMap.isMapReady = false;
       
        // create the map div
        var mapDiv = document.createElement('div');
        mapDiv.id = 'map';
		document.getElementById("mapOverlayContainer").appendChild(mapDiv);
        //document.body.appendChild(mapDiv);
                    
        var map = SWMap.map = new GMap2(mapDiv);
        
        var options = map.options = {breadcrumbTrailDiv: 'breadcrumb-nav'};
        
        var divs = map.divs = {};
        divs.mapDiv = mapDiv;
        divs.breadcrumbTrailDiv = options.breadcrumbTrailDiv || null;
        divs.mapSelectionControlDiv = options.mapSelectionControlDiv || null;
        divs.zoomControlDiv = options.zoomControlDiv || null;
        divs.boundsReportDiv = options.boundsReportDiv || null;
                
        var info = SWMap.info = {};
        info.mapWidth = 512;
        info.mapHeight = 384;
        info.zoomLevels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; // zoom levels which are selectable by external zoom control //TODO not used anymore, is it?
        info.minZoom = info.zoomLevels[0];
        info.maxZoom = info.zoomLevels[info.zoomLevels.length - 1];
        
        SWMap.animationSelectionControl = new SW.Controls.AnimationSelectionControl();
        SWMap.regionCalendar = new SW.Controls.Calendar("calendarContainer", "calendarButton", SW.Controls.Calendar.regionChangeHandler);
        SWMap.savedViews = new SW.Controls.SavedViews('saved-views', 'your-saved-views', SWMap.initialSavedViews ? SWMap.initialSavedViews : null, SWMap.userPrefs.isUserLoggedIn);
        SWMap.perspectiveView = new SW.Controls.PerspectiveView(SWMap.animationSelectionControl.controlContainer);
        SWMap.tooltip = new SW.Controls.Tooltip();
        SWMap.dateBar = new SW.Controls.DateBar('dateBar', 'monthYearLabel', general.getDate());
        SWMap.animationControl = new animation.AnimationControl(SWMap.obCodes, SWMap.dateBar);
        SWMap.markerGrid = new SW.Markers.MarkerGrid();
        SWMap.regionTrail = new SW.Controls.RegionTrail(map, divs.breadcrumbTrailDiv);
        SWMap.loadingMessage = new mapGeneral.LoadingMessage();
        SWMap.markerSelectionControl = new SW.Controls.MarkerSelectionControl(SWMap.animationSelectionControl.buttonContainer);
        SWMap.regionDataTabs = new SW.Controls.RegionDataTabCollection('region-forecast-data-container');
        SWMap.loadingIndicator = new SW.Controls.LoadingIndicator();
        SWMap.noMarkersAlertControl = new SW.Controls.NoMarkersAlertControl(SWMap.tooltip);
                    
        setupMapTypes(map); // do this before adding map type control
        map.addControl(new GMapTypeControl());
        map.addControl(SWMap.zoomSlider = new SW.Controls.ZoomSliderControl());
        map.addControl(new GScaleControl());
        map.addControl(SWMap.noMarkersAlertControl);
        
        var world = SWMap.world = {};
        world.regions = [],  // the array of all regions
        world.regionsByZoom = [];  // array of arrays of regions ordered by zoom level, e.g. regionsByZoom[2] is array of the ClickableRegionDivs at zoom level 2 (used by GLatLngBounds.getTightestContainingRegion() )
        world.regionsById = []; // all regions hashed by id
        
        // must set an initial view with map type so subsequent map settings will work
        map.setCenter(new GLatLng(0, 179.5), 0, G_SATELLITE_MAP);  // KLUDGE ALERT: setting to 180 would make the map animation frames not show up for the world region because of the way the double divs work; close enough solution for now; setting zoom to 0 ensures zoom handler gets triggered wherever the first view is
        
        handlers.setupMapHandlers(); // must come after animationControl, markerGrid, etc. are set up
        handlers.setupControlsHandlers();
        
        //SWMap.animTileManager = new SW.AnimTiles.TileManager(map);
        SW.Regions.initRegions(SWMap.initialRegionsData); // success sets isMapReady to true
        
        map.disableDoubleClickZoom();
        
        if (SWMap.userPrefs.enableScrollZoom) {
            general.disableScrolling(mapDiv); // the map container div needs to eat the scroll events to prevent the page from scrolling
            map.enableScrollWheelZoom();
            //map.enableContinuousZoom(); // only has effect when using mousewheel to zoom
        }
        
        handlers.positionMapDiv();
        
        var interval = setInterval(function () {
            if (SWMap.isMapReady) {
                SWMap.historyControl.goToStartingState();            
                mapDiv.style.visibility = 'visible';
                setTimeout(function () {SWMap.loadingMessage.hide();} , 10);
                clearInterval(interval);
            }
        }, 20);

        return map;
    };
    
    
    function setupMapTypes (map) {
        map.removeMapType(G_NORMAL_MAP);
        map.removeMapType(G_HYBRID_MAP);
        
        var roadType = new GMapType(
            G_HYBRID_MAP.getTileLayers(),
            G_HYBRID_MAP.getProjection(),
            'Road'
        );        
        
        map.addMapType(roadType);
        map.addMapType(G_PHYSICAL_MAP);
    }
    
})();
