// object
function MapPoint(latitude, longitude, text)
{
	this.point = new GLatLng(latitude, longitude);
	this.text = text;
}

// object
function GoogleMap(domObject, initZoom)
{
	/* properties / constructor */
	this.points = [];
	this.bounds = null;
	this.map = null;
	this.fixed = false;
	this.zoom = 12;
	
	if(initZoom != null) this.zoom = initZoom;
	
	if( GBrowserIsCompatible() )
	{	
		this.map = new GMap2(domObject);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.setCenter(new GLatLng(0, 0), this.zoom);
	}

	/* Methods */
	this.AddPoint = function(latitude, longitude, text)
	{
		this.points.push(new MapPoint(latitude, longitude, text));
		return this.points.length - 1;
	}
	
	this.CheckResize = function(){
		this.map.checkResize();
	}

	this.ClearPoints = function()
	{
		this.points.length = 0;
	}
	 
	this.CreateMarker = function(point, text)
	{
		var marker = new GMarker(point);
		var _map = this.map;
		if( text )
			GEvent.addListener(marker, "click", function(){_map.setCenter(point, 12); _map.openInfoWindowHtml(point, text);});
		this.map.addOverlay(marker);
	}
	
	this.FixMap = function(recordFix){
		this.CheckResize();
		this.SetCenter();
		if(recordFix == true) this.fixed = true;
	}
	
	this.SetMapType = function(mapType){
		this.map.setMapType(mapType);
	}

	this.Render = function()
	{
		if( this.map )
		{
			var minLat = 10000;
			var maxLat = -10000;
			var minLng = 10000;
			var maxLng = -10000;

			for( var i = 0; i < this.points.length; i++ )
			{
				var lat = this.points[i].point.lat();
				var lng = this.points[i].point.lng();

				if( lat > maxLat ) maxLat = lat;
				if( lat < minLat ) minLat = lat;
				if( lng > maxLng ) maxLng = lng;
				if( lng < minLng ) minLng = lng;

				this.CreateMarker(this.points[i].point, this.points[i].text);
			}
			
			this.bounds = new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng));
			this.FixMap();
		}
	}
	
	this.SetCenter = function(){
		if(this.points.length == 1){
			this.map.setCenter(this.bounds.getCenter(), this.zoom);
		} else {
			this.map.setCenter(this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds));
		}
	}
	
	this.MoveTo = function(index)
	{
		var point = this.points[index].point;
		var text = this.points[index].text;
		this.map.setCenter(point, this.zoom);
		this.map.openInfoWindowHtml(point, text);
	}
}