/*
 * Map object to initialize and show a map in the website. 
 */

var map_type = {"DEFAULT":0,
		"ACTIVITY_STREAM":1,
		"ITEM_GENERATOR":2,
		"MISSION_DETAILS":3
		};

function Map(opts) {this.opts = opts}

/*
 * Map properties.
 * DO NOT ACCESS THESE OUTSIDE THE EXPOSED INTERFACE.
 */
Map.prototype.dimension = {"width":"auto","height":"auto"};//dimensions of the div object
Map.prototype.div = null;//pointer to div object that contains the map
Map.prototype.map = null;//GMap2 object representing the map
Map.prototype.center = {"latitude":37.795271066302035,"longitude":-122.43524551391602};//location on earth where map is centered
Map.prototype.zoom = 12;//zoom level
Map.prototype.type = null;//map type given by the enum above
Map.prototype.opts = null;//options for GMap2 constructor

/*
 * Map methods.
 */
Map.prototype.Initialize = function(anchor,dimension,center,zoom,threed,type) {
	if(anchor) this.SetAnchor(anchor);
	if(type) {
		switch(type) {
		case map_type.ACTIVITY_STREAM: this.type = map_type.ACTIVITY_STREAM;
			break;
		case map_type.ITEM_GENERATOR: this.type = map_type.ITEM_GENERATOR;
			break;
		case map_type.MISSION_DETAILS: this.type = map_type.MISSION_DETAILS;
			break;
		default: this.type = map_type.DEFAULT;
			break;
		}
	} else this.type = map_type.DEFAULT;
	if(dimension) {
		this.SetWidth(dimension.width);
		this.SetHeight(dimension.height);
	}
	this.CreateMap(threed);
	if(center) this.SetCenter(center);
	else this.SetCenter(this.center);
	if(zoom) this.SetZoom(zoom);
	else this.SetZoom(this.zoom);
	return this.map;
}

Map.prototype.SetWidth = function(width) {
	if(width < 0) this.dimension.width = "auto";
	else this.dimension.width = width+"px";
	if(this.div != null) this.div.style.width = this.dimension.width;
}

Map.prototype.SetHeight = function(height) {
	if(height < 0) this.dimension.height = "auto";
	else this.dimension.height = height+"px";
	if(this.div != null) this.div.style.height = this.dimension.height;
}

Map.prototype.SetAnchor = function(anchor) {
	this.div = anchor;
	this.div.style.overflow = "hidden";
}

Map.prototype.GetAnchor = function() {
	return this.div;
}

Map.prototype.CreateMap = function(threed) {
	this.map = new GMap2(this.div,this.opts);
	if(threed) {
		this.map.addMapType(G_SATELLITE_3D_MAP);
		this.map.getEarthInstance(getEarthInstanceCB);
		this.map.setMapType(G_SATELLITE_3D_MAP);
	} else {
		this.map.addMapType(G_PHYSICAL_MAP);
		this.map.enableScrollWheelZoom();
		if(this.type == map_type.DEFAULT) {
			this.map.setMapType(DEFAULT_MAP_STYLE);
			this.map.addControl(new FancyMapControl());
			this.map.addControl(new FancyMapButtons());
		} else if(this.type == map_type.ACTIVITY_STREAM){
			this.map.setMapType(G_PHYSICAL_MAP);
			this.map.disableDoubleClickZoom();
			this.map.disableScrollWheelZoom();
			GEvent.addDomListener(this.div,"mouseover",function() {mouseovermap = true;});
			GEvent.addDomListener(this.div,"mouseout",function() {mouseovermap = false;});
			this.map.checkResize();
		} else if(this.type == map_type.ITEM_GENERATOR) {
			this.map.setMapType(G_NORMAL_MAP);
			this.map.disableDoubleClickZoom();
			this.map.addControl(new FancyMapButtons(G_NORMAL_MAP));
			this.map.addControl(new FancyMapControl());
		} else if(this.type == map_type.MISSION_DETAILS) {
			this.map.setMapType(DEFAULT_MAP_STYLE);
			this.map.disableScrollWheelZoom();
			this.map.addControl(new FancyMapControl());
			this.map.addControl(new FancyMapButtons());
		}
		var mapControl = new GHierarchicalMapTypeControl();
		mapControl.clearRelationships();
		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
	}
}

Map.prototype.SetCenter = function(center) {
	if(center) {
		this.map.setCenter(new GLatLng(center.latitude,center.longitude));
		this.center = center;
	}
}

Map.prototype.SetZoom = function(zoom) {
	if(zoom) {
		this.map.setZoom(zoom);
		this.zoom = zoom;
	}
}

Map.prototype.GetMap = function() {
	return this.map;
}

var ge = null;
function getEarthInstanceCB(object) {
	ge = object;
	//You can now manipulate ge using the full Google Earth API.
	ge.getWindow().setVisibility(true);
	//fetch the KML
	var url = BASE_URL+'/ClusteredWorldState.kml';
	
	google.earth.fetchKml(ge, url, finishedFetchKml);
	ContinueLoad();
}

function finishedFetchKml(object) {
	if (!object) {
		console.log("Bad or NULL KML in finishedFetchKml (map.js)");
	    return;
	}
	ge.getFeatures().appendChild(object);
}
