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

// object
function GoogleMap(domObject, width, height)
{
	/* properties / constructor */
	this.points = [];
	this.map = null;
	this.bounds = null;
	
	if( GBrowserIsCompatible() )
	{	
		this.map = new GMap2(domObject);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.setCenter(new GLatLng(0, 0), 13);
	}

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

	this.ClearPoints = function()
	{
		this.points.length = 0;
	}
	
	this.CreateMarker = function(point, text)
	{
		var marker = new GMarker(point);
		var _map = this.map;
		GEvent.addListener(marker, "click", function(){_map.openInfoWindowHtml(point, text);});
		this.map.addOverlay(marker);
	}

	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.ResetFocus();
		}
	}
	
	this.ResetFocus = function()
	{
		if( this.bounds )
		{
			this.map.checkResize();
			this.map.setCenter(this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds)-1);			
		}
	}
	
	this.MoveTo = function(index)
	{
		var point = this.points[index].point;
		var text = this.points[index].text;
		this.map.setCenter(point, 12);
		this.map.openInfoWindowHtml(point, text);
	}
}