/*
	c Harald Mayer (webmaster.austria@vienna.msf.org)
*/
var MsfMap = {
	// ============= main function, called from html-file ===========
	registerMap : function(container, opts) {
		if (document.all&&window.attachEvent) { // IE-Win
			window.attachEvent("onload", function () { MsfMap.createMap(container, opts); });
		  window.attachEvent("onunload", GUnload);
		} else if (window.addEventListener) { // Others
			window.addEventListener("load", function () { MsfMap.createMap(container, opts); }, false);
			window.addEventListener("unload", GUnload, false);
		}
	},

	showWholeMap : function () {
		this.initZoom = 1;
		this.map.setCenter(new GLatLng(1, 1), 1);
		this.setupMap(this.opts);
	},

	// ============= setup the map ===========
	setupMap : function (opts) {
		// don't call before first map.setCenter()!!!
		if (opts.showMapTypeControl) {
			this.map.addControl(new GMapTypeControl());
		}
		if (opts.enableScrollWheelZoom) {
			this.map.enableScrollWheelZoom();
		}
		if (opts.showOverviewMapControl) {
			this.map.addControl(new GOverviewMapControl(new GSize(150,150)));
		}
		if (opts.showScaleControl) {
			this.map.addControl(new GScaleControl());
		}

		this.map.removeMapType(G_HYBRID_MAP);
		this.map.removeMapType(G_NORMAL_MAP);
		this.map.removeMapType(G_SATELLITE_MAP);
		this.map.removeMapType(G_PHYSICAL_MAP);

		if(opts.mapTypes){
			for (var i=0; i<opts.mapTypes.length; i++) {
				this.map.addMapType(opts.mapTypes[i]);
			}
			this.defaultMapType = opts.mapTypes[0];
		} else {
			this.defaultMapType = G_PHYSICAL_MAP;
		}
		
		CustomGetTileUrl=function(a,b){
			return 'http://a.tile.openstreetmap.org/'+b+'/'+a.x+'/'+a.y+'.png';
		}
    var copyright = new GCopyright(1,
        new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 0,
        '(<a rel="license" href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>)');
    var copyrightCollection =
        new GCopyrightCollection('&copy; 2009 <a href="http://www.openstreetmap.org/">OpenStreetMap</a> Contributors');
    copyrightCollection.addCopyright(copyright);
		var tilelayers = [new GTileLayer(copyrightCollection,1,17)];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		var osmmap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), 'O.S.M.');
		this.map.addMapType(osmmap);
		
		this.map.setMapType(this.defaultMapType);


		// ====== Restricting the range of Zoom Levels =====
		// Get the list of map types      
		var mt = this.map.getMapTypes();
		// Overwrite the getMinimumResolution() and getMaximumResolution() methods
		for (var i=0; i<mt.length; i++) {
			mt[i].getMinimumResolution = function() {return opts.minZoom;}
			mt[i].getMaximumResolution = function() {return Math.max(opts.maxZoom,MsfMap.initZoom);}
		}
		
		// Add a zoom listener
		GEvent.addListener(MsfMap.map, "zoomend", function() {
			/*if (parseInt(MsfMap.map.getZoom()) < 4) {
				MsfMap.deleteLastTLabel();
			}*/
			// adjust circle around activeMarker to current zoom value
			if (MsfMap.activeMarker) {
				//MsfMap.drawCircle(MsfMap.activeMarker.pnt,MsfMap.getCircleRadius(),30,"#0055ff",1,1.0,"#0055ff",0.1);
			}
		});

		if (opts.showLargeMapControl) {
			this.map.addControl(new GLargeMapControl());
		} else if (opts.smallMapControl) {
			this.map.addControl(new GSmallMapControl());
		}
		
		if (opts.showMarkerOverlays) {
			exml = new EGeoXml("exml", MsfMap.map, opts.kmlUrl,{initZoom:opts.initZoom,elabelclass:"label",elabelopacity:70,elabeloffset:new GSize(-4,21)});
		} else {
			exml = new EGeoXml("exml", MsfMap.map, opts.kmlUrl,{initZoom:opts.initZoom});
		}
		exml.parse();
	},
	
	createMap : function(container, opts) {
		this.container = container;
		this.opts = opts;
		this.map = new GMap2(this.container,{backgroundColor:"#FFFFFF"});
		this.width = opts.width;
		this.height = opts.height;
		this.showWholeMap();
	}
};

function init(v) {
	//v==version of the map
	MsfMap.registerMap(document.getElementById("map"), {
		kmlUrl: "/earthquake-haiti/kml/haiti_earthquake_2010_v"+v+".kml",
		showLargeMapControl:1,
		smallMapControl:0,
		showMapTypeControl:1,
		showScaleControl:0,
		showOverviewMapControl:0,
		enableScrollWheelZoom:1,
		showMarkerOverlays:1,
		mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP],
		minZoom:4,
		maxZoom:12
	});
}