 
 # Home page with map
 mainContent = ->
+	log "mainContent()"
 	renderMap()
 	renderFlags()
 	Obs.observe ->
-		if mapReady() and map?
+		log "  Map bounds and minzoom set"
+		if mapReady()
 			# Limit scrolling to the bounds and also limit the zoom level
 			loc1 = L.latLng(Db.shared.get('game', 'bounds', 'one', 'lat'), Db.shared.get('game', 'bounds', 'one', 'lng'))
 			loc2 = L.latLng(Db.shared.get('game', 'bounds', 'two', 'lat'), Db.shared.get('game', 'bounds', 'two', 'lng'))
-			map.setMaxBounds(L.latLngBounds(loc1, loc2))
-			map._layersMinZoom = map.getBoundsZoom(L.latLngBounds(loc1, loc2))
+			bounds = L.latLngBounds(loc1, loc2)
+			map.setMaxBounds(bounds)
+			map.fitBounds(bounds);
+			map._layersMinZoom = map.getBoundsZoom(bounds)
 		Obs.onClean ->
 			if map?
+				log "  Map bounds and minzoom reset"
 				map.setMaxBounds()
 				map._layersMinZoom = 0
 	addBar()

			window.map = L.mapbox.map('OpenStreetMap', 'nlthijs48.4153ad9d', {zoomControl:false, updateWhenIdle:false, detectRetina:true})
			layer = L.mapbox.tileLayer('nlthijs48.4153ad9d')
			log "  Initialized MapBox map"
			#mapReady.modify () -> (L? and map?)
		Obs.onClean ->
			log "onClean() of setupMap()"
			if map?
				map.remove()
				window.map = null
				log "  removed map"

# Add flags to the map
renderFlags = ->
	log "rendering flags"
	Db.shared.observeEach 'game', 'flags', (flag) !->
		if mapReady() and map?
			if not window.flagMarkers?
				log "flagMarkers list reset"
				window.flagMarkers = [];
			location = L.latLng(flag.get('location', 'lat'), flag.get('location', 'lng'))
			log "Added flag"
			marker = L.marker(location)
			marker.bindPopup("lat: " + location.lat + "<br>long: " + location.lng)
			marker.addTo(map)
			flagMarkers.push marker
			#log 'Added marker, marker list: ', flagMarkers
			
			log "Added circle"
			if not window.flagCircles?
				log "flagCircles list reset"
				window.flagCircles = [];

				scoresContent()
			else if page == 'log'
				logContent()

exports.renderSettings = !->
	if Db.shared
