map = null;
baseIcon = null;
geocoder = null;
markers = new Array();

function createMap(section) {
	if (GBrowserIsCompatible()) {
		baseIcon = new GIcon();
		baseIcon.image = '/images/' + section + 'icon.png';
		baseIcon.shadow = '/images/' + section + 'iconshadow.png';
		baseIcon.iconSize = new GSize(22,22);
		baseIcon.shadowSize = new GSize(36,18);
		baseIcon.iconAnchor = new GPoint(11,22);
		baseIcon.infoWindowAnchor = new GPoint(22,11);
		baseIcon.infoShadowAnchor = new GPoint(22,11);
		geocoder = new GClientGeocoder();
	
		map = new GMap2(document.getElementById('map'));
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(35.27019384502174, -97.46302490234375), 11);
	}
}

function createHomeMarker(id,address) {
	if (geocoder) {
		geocoder.getLatLng(address,
			function(point) {
				if (point) {
					var marker = new GMarker(point,baseIcon);
					GEvent.addListener(marker, 'click', function() {
					if (document.getElementById("home"+id)) {
						marker.openInfoWindowHtml(document.getElementById('home'+id).innerHTML);
					}
					});
					map.addOverlay(marker);
					markers[id] = marker;
				}
			}
		);
	}
}

function createCommunityMarker(id, latitude, longitude) 
{
    var marker = new GMarker(new GPoint(latitude,longitude),baseIcon);
	GEvent.addListener(marker, "click", function() {
    if (document.getElementById('community'+id)) {
    	marker.openInfoWindowHtml(document.getElementById('community'+id).innerHTML);
    }
    });
	map.addOverlay(marker);
    markers[id] = marker;
}

function checkAddress(address) {
	var baseIcon = new GIcon();
	baseIcon.image = '/images/homeicon.png';
	baseIcon.shadow = '/images/homeiconshadow.png';
	baseIcon.iconSize = new GSize(22,22);
	baseIcon.shadowSize = new GSize(36,18);
	baseIcon.iconAnchor = new GPoint(11,22);
	baseIcon.infoWindowAnchor = new GPoint(22,11);
	baseIcon.infoShadowAnchor = new GPoint(22,11);
	if (geocoder) {
		geocoder.getLatLng(address,
			function(point) {
				if (!point) {
				  alert(address + ' not found');
				} else {
				  map.setCenter(point, 13);
				  var marker = new GMarker(point,baseIcon);
				  map.addOverlay(marker);
				  marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

function addClickListener(id) {
	GEvent.addListener(map, 'click', function(overlay, point) {
    document.getElementById('coordinates').innerHTML = '(' + 
      point.x.toString().substr(0,7) + ', ' + 
      point.y.toString().substr(0,7) + ')';
    document.getElementById('community_latitude').value = point.x;
    document.getElementById('community_longitude').value = point.y;
    map.clearOverlays();
    createCommunityMarker(id,point.x,point.y);
    });
}

function clickMarker(id)
{
	if ($('community_menu'+id).visible()) {
		markers[id].openInfoWindowHtml(document.getElementById('community'+id).innerHTML);
		$('map').style.height = ($('map').scrollHeight + $('community_menu'+id).scrollHeight) + 'px';
	} else {
		$('map').style.height = '476px';
	}
}

function closeInfoWindows()
{
	for (var i=0; i<markers.length; i++) {
		if (markers[i])
			markers[i].closeInfoWindow();
	}
}