function MapSet(id, center_lat, center_lng, zoom, type_control)
{
	if(!GBrowserIsCompatible())
		return false;
	
	this.markers=[];
	this.circle=null;
	this.centerMarker=null;
	this._ready=false;
	
	addEvent(window, 'load', function()
	{
		this.map = new GMap2(document.getElementById(id));
		this.map.setCenter(new GLatLng(center_lat, center_lng), zoom);
		this.map.addControl(new GSmallMapControl());
		if(!!type_control)
			this.map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10)));
		
		this._ready=true;
	}.bind(this));
	
	return this;
}
/*
MapSet.prototype.setMarker=function(image, shadow)
{
	markerIcon = new GIcon(G_DEFAULT_ICON);
	markerIcon.image = 'http://calibratecpas.com/images/marker.png';
	markerIcon.shadow = 'http://calibratecpas.com/images/shadow.png';
	markerIcon.imageMap = [0,0, 50,0, 50,23, 0,23];
	markerIcon.iconSize = new GSize(50, 23);
	markerIcon.shadowSize = new GSize(50, 50);
}
*/
MapSet.prototype.addMarker=function(lat, lng, onclick)
{
	var fn=function(lat, lng, html)
	{
		var point, options, marker;
		
		options = {};
		point = new GLatLng(lat, lng);
		if(!!this._marker_icon)
		options.icon=this._marker_icon;
		marker = new GMarker(point, options);
		
		GEvent.addListener(marker, 'click', onclick.bind(this));
		
		this.map.addOverlay(marker);
		this.markers.push(marker);
	}.partial(lat, lng, onclick).bind(this).defer(function()
	{
		return (this._ready);
	}.bind(this));
	
	return this;
}

MapSet.prototype.drawLocations=function(resp)
{
	var locations, i, html, lat, lng;
	
	this.map.clearOverlays();
	this.map.setCenter(new GLatLng(resp.zipcode.lat, resp.zipcode.lng), 11);

	locations = resp.locations;
	var i = locations.length;
	while(i--)
	{
		html = '<div><strong>'+htmlentities(locations[i].name)+'</strong><br />'+htmlentities(locations[i].address)+'<br />'+htmlentities(locations[i].city)+', '+htmlentities(locations[i].state)+', '+htmlentities(locations[i].zip);
		if(!!locations[i].phone)
			html+= '<br />'+htmlentities(locations[i].phone);
		if(!!locations[i].services)
			html+='<br />'+htmlentities(locations[i].services);
		html+='</div>';
		lat = locations[i].lat;
		lng = locations[i].lng;
		this.addMarker(lat, lng, html);
	}
	
	this.drawCircle(resp.radius);
}

MapSet.prototype.drawCircle=function(radius, units)
{
	var center, bounds, points, d, lat1, lng1, lat2, lng2, a, tc, y, dlng, x, point;
	
	if(!units)
		units='MI';
	
	center = this.map.getCenter();
	bounds = new GLatLngBounds();
	points = [];
	
	if(!!this.circle)
		this.map.removeOverlay(circle);
	
	if(units == 'KM')
		d = radius/6378.8;	// radians
	else //miles
		d = radius/3963.189;	// radians
	
	lat1 = (Math.PI/180)* center.lat(); // radians
	lng1 = (Math.PI/180)* center.lng(); // radians
	
	for (a = 0; a < 361; a++)
	{
		tc = (Math.PI/180)*a;
		y = Math.asin(Math.sin(lat1)*Math.cos(d)+Math.cos(lat1)*Math.sin(d)*Math.cos(tc));
		dlng = Math.atan2(Math.sin(tc)*Math.sin(d)*Math.cos(lat1),Math.cos(d)-Math.sin(lat1)*Math.sin(y));
		x = ((lng1-dlng+Math.PI) % (2*Math.PI)) - Math.PI;
		point = new GLatLng(parseFloat(y*(180/Math.PI)),parseFloat(x*(180/Math.PI)));
		points.push(point);
		bounds.extend(point);
	}
	
	if (d < 1.5678565720686044)
		this.circle = new GPolygon(points, '#000000', 2, 1, '#000000', 0.25);
	else
		this.circle = new GPolygon(points, '#000000', 2, 1);
	this.map.addOverlay(this.circle); 
	
	this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
	
	return this;
}