var GoogleMap = new Class({

	Extends: Options,
	options: {
		points: null,
		center: false,
		zoom: 10,
		global: null
	},

	initialize: function(cont, options) {
		if (!GBrowserIsCompatible()) return;
		
		this.setOptions(options);
		
		// create map and controls
		this.map = new GMap2(typeof(cont) == 'string' ? $(cont) : cont);
		this.map.addControl(new GSmallMapControl());
		
		// centering
		if (this.options.center) {
			var center = typeof(this.options.center) == 'boolean' ? this.options.points[0] : this.options.center;
			this.map.setCenter(new GLatLng(center.lat, center.lon), this.options.zoom);
		}
		else {
			this.setGlobals();
			this.map.setCenter(new GLatLng(this.options.globals.center.lat, this.options.globals.center.lon), this.options.globals.zoom);
		}
		
		// add points
		if (this.options.points)
			this.options.points.each(function(point) {
				var p = new GLatLng(point.lat, point.lon);
				var marker = new GMarker(p);
				this.map.addOverlay(marker);
			}, this);
	},
	
	focusPoint: function(point) {
		this.map.savePosition();
		this.map.panTo(new GLatLng(point.lat, point.lon));
	},
	
	setCenterPoint: function(point) {
		this.map.panTo(new GLatLng(point.lat, point.lon));
	},
	
	setZoom: function(zoom) {
		this.options.zoom = zoom;
		this.map.setZoom(zoom);
	},
	
	savePosition: function() {
		this.map.savePosition();
	},
	
	returnToSavedPosition: function() {
		this.map.returnToSavedPosition();
	},
	
	checkResize: function() {
		var oldCenter = this.map.getCenter();
		this.map.checkResize();
		this.map.panTo(oldCenter);
	},
	
	setGlobals: function() {
		var bndLat = [180, -180];
		var bndLon = [90, -90];
		this.options.points.each(function(point) {
			if (point.lat < bndLat[0]) bndLat[0] = point.lat;
			if (point.lat > bndLat[1]) bndLat[1] = point.lat;
			if (point.lon < bndLon[0]) bndLon[0] = point.lon;
			if (point.lon > bndLon[1]) bndLon[1] = point.lon;
		}, this);
		var bnds = new GLatLngBounds(new GLatLng(bndLat[0], bndLon[0]), new GLatLng(bndLat[1], bndLon[1]));
		
		this.options.globals = {};
		this.options.globals.zoom = this.map.getBoundsZoomLevel(bnds);
		this.options.globals.center = { lat: (bndLat[0] + bndLat[1]) / 2, lon: (bndLon[0] + bndLon[1]) / 2 };
	}
	
})