/*
	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;
		}
		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 Math.min(opts.minZoom,MsfMap.initZoom);}
			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());
		}

		// Add kml overlay
		/*if (opts.kmlUrl) {
			var geoXml = new GGeoXml(opts.kmlUrl);
			GEvent.addListener(geoXml, 'load', function() {
				if (geoXml.loadedCorrectly()) {
					geoXml.gotoDefaultViewport(MsfMap.map);
					if (MsfMap.opts.initZoom > -1) {
						MsfMap.map.setZoom(opts.initZoom);
					}
				}
			});
			this.map.addOverlay(geoXml);
		} else if (opts.initZoom > -1) {
			MsfMap.map.setZoom(opts.initZoom);
		}*/

		var exml = new EGeoXml("exml", MsfMap.map, opts.kmlUrl,{elabelclass:"label",elabelopacity:70,elabeloffset:new GSize(-4,21)});
		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();
	}
};