// Shortcut functions for Google Maps
// Requires that the Google AJAX API already be loaded.
// Assumes MEDIA_URL is already set

google.load('maps', '2.x');
var maps = {
	create_map : function(element, type) {
		maps.init();
		var map = new google.maps.Map2(element);
		map.setCenter(new google.maps.LatLng(38.937841, -92.334255), 13);
		if (type == 'large') {
			map.addControl(new google.maps.LargeMapControl());
			map.addControl(new google.maps.ScaleControl());
		} else if (type == 'small') {
			map.addControl(new google.maps.SmallMapControl());
		} else {
			map.addControl(new google.maps.SmallZoomControl());
		}
		return map;
	},
	add_marker : function(map, lat, lng, icon, html) {
		if (typeof map.digmo_markers == 'undefined') map.digmo_markers = [];
		var point  = new google.maps.LatLng(lat, lng);
		var marker = new google.maps.Marker(point, {icon: maps.icons[icon]});
		if (html) google.maps.Event.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); });
		map.addOverlay(marker);
		map.digmo_markers.push(marker);
		return marker;
	},
	center_map : function(map, max_zoom) {
		if (typeof max_zoom == 'undefined') max_zoom = 14;
		if (typeof map.digmo_markers == 'undefined') map.digmo_markers = [];
		if (map.digmo_markers.length > 0) {
			var lat1, lat2, lng1, lng2, p;
			lat1 = lat2 = map.digmo_markers[0].getLatLng().lat();
			lng1 = lng2 = map.digmo_markers[0].getLatLng().lng();
			for (var i = 1; i < map.digmo_markers.length; ++i) {
				p = map.digmo_markers[i].getPoint();
				if (p.lat() == 0 && p.lng() == 0) continue
				lat1 = Math.min(lat1, p.lat()); lat2 = Math.max(lat2, p.lat());
				lng1 = Math.min(lng1, p.lng()); lng2 = Math.max(lng2, p.lng());
			}
			bounds = new google.maps.LatLngBounds(new google.maps.LatLng(lat1, lng1), new google.maps.LatLng(lat2, lng2));
			map.setCenter(bounds.getCenter(), Math.min(max_zoom, map.getBoundsZoomLevel(bounds)));
		}
	},
	init : function() {
		var icon = new google.maps.Icon(google.maps.DEFAULT_ICON);
		icon.image      = MEDIA_URL + 'img/maps/tag.png';
		icon.shadow     = MEDIA_URL + 'img/maps/tag_shadow.png';
		icon.iconSize   = new google.maps.Size(11, 16);
		icon.iconAnchor = new google.maps.Point(5, 16);
		icon.shadowSize = new google.maps.Size(20, 16);
		maps.icons = {
			white  : icon,
			blue   : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_blue.png'  ),
			gray   : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_gray.png'  ),
			green  : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_green.png' ),
			orange : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_orange.png'),
			purple : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_purple.png'),
			red    : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_red.png'   ),
			yellow : new google.maps.Icon(icon, MEDIA_URL + 'img/maps/tag_yellow.png')
		};
	}
};

