function initMap(div_id, settings) {
    if (typeof div_id == 'string') {
        var elem = document.getElementById(div_id);
    } else if (typeof div_id == 'object') {
        var elem = div_id;
    } else {
        return; // failed
    }
    if (!elem) {
        return; // that element does not exist
    }
    var map = new GMap2(elem);
    map.setUIToDefault();
    // check for global mapCenter
    var center = mapCenter || [40.466438,-106.811861];
    map.setCenter(new GLatLng(center[0],center[1]), 11);
    return map;
}

function mapOverlay(map, kml_file)
{
    var geox = new GGeoXml(kml_file);
    GEvent.addListener(geox, "load", function(){
        geox.gotoDefaultViewport(map);
        map.setCenter(geox.getDefaultCenter());
    });
    map.addOverlay(geox);
}

function plotPoint(gps, map) {
    point = getPoint(gps);
    map.setCenter(point, 15, G_NORMAL_MAP);
    map.addOverlay(new GMarker(map.getCenter()));
}

function getPoint(coords) {
    point = new GLatLng(coords[1], coords[0]);
    return point;
}

function createMarker(point, html, icon) {
    icon = icon || G_DEFAULT_ICON;
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}

function plotMap(mapPoints, map) {
	var bounds = new GLatLngBounds();
	var display = false;
	$.each( mapPoints, function(i, o){
		if (o.gps) {
			display = true;
			point = getPoint(o.gps);
			var marker = createMarker(point, o.html);
			map.addOverlay(marker);
			bounds.extend(point);
		}
	});
	if (display) {
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	} else {
		//map.o obfuscated object containing map ID
		$(map.o).hide();
	}
}

function plotLandmarks(landmarks, map) {
    $.each(landmarks, function(i, l){
        var icon = new GIcon();
        icon.image = l.icon.icon;
	var iconSize = l.icon.icon_size.split(',');
        icon.iconSize = new GSize(iconSize[0], iconSize[1]);
        icon.shadow = l.icon.shadow;
	var shadowSize = l.icon.shadow_size.split(',');
        icon.shadowSize = new GSize(shadowSize[0], shadowSize[1]);
	var iconAnchor = l.icon.anchor.split(',');
        icon.iconAnchor = new GPoint(parseInt(iconAnchor[0]), parseInt(iconAnchor[1]));
        var windowAnchor = l.icon.window_anchor.split(',');
		icon.infoWindowAnchor = new GPoint(parseInt(windowAnchor[0]), parseInt(windowAnchor[1]));
        icon.transparent = l.icon.transparent;
        icon.imageMap = l.icon.image_map.split(',');
        var point = getPoint(l.gps);
        var marker = createMarker(point, l.html, icon);
        map.addOverlay(marker);
    });
}

// Via http://dawsdesign.com/drupal/google_maps_circle_overlay
// This file adds a new circle overlay to GMaps2
// it is really a many-pointed polygon, but look smooth enough to be a circle.
var CircleOverlay = function(latLng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
	this.latLng = latLng;
	this.radius = radius;
	this.strokeColor = strokeColor;
	this.strokeWidth = strokeWidth;
	this.strokeOpacity = strokeOpacity;
	this.fillColor = fillColor;
	this.fillOpacity = fillOpacity;
}

// Implements GOverlay interface
CircleOverlay.prototype = GOverlay;

CircleOverlay.prototype.initialize = function(map) {
	this.map = map;
}

CircleOverlay.prototype.clear = function() {
	if(this.polygon != null && this.map != null) {
		this.map.removeOverlay(this.polygon);
	}
}

// Calculate all the points and draw them
CircleOverlay.prototype.redraw = function(force) {
	var d2r = Math.PI / 180;
	circleLatLngs = new Array();
	var circleLat = this.radius * 0.014483;  // Convert statute miles into degrees latitude
	var circleLng = circleLat / Math.cos(this.latLng.lat() * d2r);
	var numPoints = 40;
	
	// 2PI = 360 degrees, +1 so that the end points meet
	for (var i = 0; i < numPoints + 1; i++) { 
		var theta = Math.PI * (i / (numPoints / 2)); 
		var vertexLat = this.latLng.lat() + (circleLat * Math.sin(theta)); 
		var vertexLng = this.latLng.lng() + (circleLng * Math.cos(theta));
		var vertextLatLng = new GLatLng(vertexLat, vertexLng);
		circleLatLngs.push(vertextLatLng); 
	}
	
	this.clear();
	this.polygon = new GPolygon(circleLatLngs, this.strokeColor, this.strokeWidth, this.strokeOpacity, this.fillColor, this.fillOpacity);
	this.map.addOverlay(this.polygon);
}

CircleOverlay.prototype.remove = function() {
	this.clear();
}

CircleOverlay.prototype.containsLatLng = function(latLng) {
	// Polygon Point in poly 
	if(this.polygon.containsLatLng) {
		return this.polygon.containsLatLng(latLng);
	}
}

CircleOverlay.prototype.setRadius = function(radius) {
	this.radius = radius;
}

CircleOverlay.prototype.setLatLng = function(latLng) {
	this.latLng = latLng;
}


