/** google.maps.api3.js
 */

$(document).ready(function () {	
	// Define Google Maps components
	var geocoder = new google.maps.Geocoder();
	var point;
	var mapViewAreaID = 'mapLocation';
	
	// Get Community Location Info
	var Latitude = parseFloat(commInfo.Latitude);
	var Longitude = parseFloat(commInfo.Longitude);
	var name = commInfo.Name;
	var address = commInfo.Address1 + ", " + commInfo.City + ", " + commInfo.State + ", " + commInfo.Zip;
	
	// Get the location Geocoding
	if (Latitude && Longitude) {
		point = new google.maps.LatLng(Latitude, Longitude);
		addMarker();
	} else {
		geocoder.geocode(
			{ 'address' : address },
			function (results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					point = results[0].geometry.location;
					addMarker();
				} else {
					alert ("Error in Geocoding...");
				}
			}
		);
	}
	
	function addMarker() {
		// Initialize Map Object
		var mapDIVID = document.getElementById(mapViewAreaID);
		var myOptions = {
			zoom: 13,
			center: point,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var googleMap = new google.maps.Map(mapDIVID, myOptions);
	
		var marker = new google.maps.Marker({
			position: point,
			map: googleMap,
			title: name
		});
		
		var infoWindow = new google.maps.InfoWindow();
		var thumbNail = "";
		var address2 = "";
		/*
		 * As of now, written spec does not include this.
		if (commInfo.Photo) {
			thumbNail = "<img id='mapInfoImage' src='" + commInfo.Photo + "' alt='' title='' align='left' />";
		}
		*/
		if (commInfo.Address2) {
			address2 = commInfo.Address2 + "<br />";
		}
		var html = 	"<div id='MD_infoWindow'>" +
						thumbNail +
						"<strong class='infoCommName'>" + name + "</strong><br />" +
						"<em>" + commInfo.Address1 + "<br />" + address2 +
						commInfo.City + ", " + commInfo.State + " " + commInfo.Zip + "</em><br /><br />" +
						"<strong>Phone: " + commInfo.Phone + "</strong><br />" +
						"Fax: " + commInfo.Fax +
					"</div>";
	
		google.maps.event.addListener(marker, 'click', function() {
			var map = marker.getMap();
			mapID = map.getDiv().id;
			infoWindow.setContent(html);
			infoWindow.open(map, marker);
		});
	}
});

