function setupMap(target, lat, lon, zoom, controls)
{    
  if (GBrowserIsCompatible()) {
	  lat = (lat != undefined) ? lat : '32.4771';
	  lon = (lon != undefined) ? lon : '-93.7484';
	  zoom = (zoom != undefined) ? zoom : 10;
	  controls = (controls != undefined) ? controls : 'small';
	  
	  map = new GMap2($(target));
	  
	  if (controls == 'small') {
		  map.addControl(new GSmallMapControl());
	  }

      point = new GLatLng(lat, lon);
      map.setCenter(point, zoom);

      bounds = new GLatLngBounds();
      marker_mgr = new MarkerManager(map);
  }
}

function addMarker(latitude, longitude, letter, content)
{
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    point = new GLatLng(latitude, longitude);

    if (letter) {
    	var letteredIcon = new GIcon(baseIcon);
    	letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    }
    
    markerOptions = { icon:letteredIcon };
    marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindow(content);
	});
    
    marker_mgr.addMarker(marker, 0);
    bounds.extend(point);

    marker_mgr.refresh();
}

function zoomfit()
{
	newzoom = map.getBoundsZoomLevel(bounds);
	newcenter = bounds.getCenter();
	map.setCenter(newcenter, newzoom-1);
}

function initMap(results)
{
	var defaultCenter = new google.maps.LatLng(37.0625, -95.677068);
	var myOptions = {
		zoom: 3,
		center: defaultCenter,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("gmap"), myOptions);
	
	var mapBounds = new google.maps.LatLngBounds();
	
	var currIcon = new String('A');
	
	for(i=0; i < results.length; i++)
	{
		var record = results[i];
		
		lat = record.latitude;
		lng = record.longitude;
		
		if(lat != undefined && lng != undefined)
		{
			var latLng = new google.maps.LatLng(lat, lng);
			
			var marker = new google.maps.Marker({
				animation: google.maps.Animation.DROP,
				icon: 'http://www.google.com/mapfiles/marker' + currIcon + '.png',
				position: latLng,
				title: record.name
			});
			marker.setMap(map);
			
			var info = new google.maps.InfoWindow({
				content: formatRecord(record)
			});
			
			google.maps.event.addListener(marker, 'click', function(bubble, mark, map) {
				return function() {
					bubble.open(map, mark)
				}
			}(info, marker, map));
			
			mapBounds.extend(latLng);
			
			currIcon = String.fromCharCode(currIcon.charCodeAt() + 1);
		}
		
		if(latLng != undefined)
			map.fitBounds(mapBounds);
	}
}

function formatRecord(record)
{
	var content = '<b>' + record.name + '</b>';
	
	content += '<p>';
	content += ((record.address1 != undefined && record.address1 != '') ? record.address1 + '<br />' : '');
	content += ((record.address2 != undefined && record.address2 != '') ? record.address2 + '<br />' : '');
	content += ((record.city != undefined && record.city != '') ? record.city + ', ' : '');
	content += ((record.state != undefined && record.state != '') ? record.state : '');
	content += ((record.description != undefined && record.description != '') ? record.zipcode : '');
	content += '</p><br />';
	
	content += ((record.description != undefined && record.description != '') ? '<p>Description:<br/>' + record.description + '</p>' : '');
	
	return content;
}






