var g_interestMap = null;
var g_interestMapPoints = new Array();
var g_interestMapCenter = null;

function InterestMapShowPoint(pointIndex) {

	var marker = null;
	var infoWindow = null;

	if ((0 <= pointIndex) && (g_interestMapPoints.length > pointIndex)) {

		// get marker and info window objects
		marker = g_interestMapPoints[pointIndex].marker;
		infoWindow = g_interestMapPoints[pointIndex].infoWindow;

		// close all other info windows
		for (var index = 0; g_interestMapPoints.length > index; index++) {

			// close all marker info windows except for the requested marker
			if (pointIndex != index) {
				g_interestMapPoints[index].infoWindow.close();
			}
		}

		// open info window
		infoWindow.open(g_interestMap, marker);

		// center on marker
		g_interestMap.panTo(marker.getPosition());
	}

	return;
}

function ModuleInterestMapInitialize() {

	g_interestMapCenter = new google.maps.LatLng(37.400, -80.300);
	var mapOptions = {
		zoom: 9,
		center: g_interestMapCenter,
		mapTypeId: google.maps.MapTypeId.HYBRID
	};

	// create map
	g_interestMap = new google.maps.Map(document.getElementById('module-interest-map'), mapOptions);

	// create markers for all points.  Created the associated info windows for each marker
	for (var index = 0; index < g_interestMapPoints.length; index++) {

		// create marker for map
		InterestMapAttachMarker(g_interestMapPoints[index]);
	}

	return;
}

function InterestMapAttachMarker(interestMapPoint) {

	var markerLocation = null;
	var marker = null;
	var content = '';

	// get latitude and longitude of point
	var markerLocation = new google.maps.LatLng(interestMapPoint.markerLat, interestMapPoint.markerLng);

	// create marker for point
	var marker = new google.maps.Marker({
		position: markerLocation,
		map: g_interestMap,
		title: interestMapPoint.title
	});

	// create content of info window for marker
	var content = interestMapPoint.description;

	// add info window to marker
	var infoWindow = InterestMapAttachInfoWindow(marker, content);

	// add marker and info window to this point
	interestMapPoint.marker = marker;
	interestMapPoint.infoWindow = infoWindow;

	return;
}

function InterestMapAttachInfoWindow(marker, content) {

	var infoWindow = null;

	// create info window
	infoWindow = new google.maps.InfoWindow({
		content: content
	});

	google.maps.event.addListener(marker, 'click', function() {

		// close all open info windows
		for (var index = 0; g_interestMapPoints.length > index; index++) {
			g_interestMapPoints[index].infoWindow.close();
		}

		// open this info window
		infoWindow.open(g_interestMap, marker);

		// center map on marker
		g_interestMap.panTo(marker.getPosition());
	});

	google.maps.event.addListener(infoWindow, 'closeclick', function() {
		g_interestMap.panTo(g_interestMapCenter);
	});

	return infoWindow;
}

function InterestMapPoint(id, title, description, markerImage, markerLat, markerLng) {

	id = id.substr(id.lastIndexOf('-') + 1);

	this.id = id;
	this.title = title;
	this.description = description;
	this.markerImage = markerImage;
	this.markerLat = markerLat;
	this.markerLng = markerLng;
	this.marker = null;
	this.infoWindow = null;
}

// add code to load on window initialization
if (null == window.onload) {
	window.onload = ModuleInterestMapInitialize;
}
else {
	currentLoadFunc = window.onload;
	window.onload = function() {
		currentLoadFunc();
		ModuleInterestMapInitialize();
	}
}

