var jj_locations = new Array();
var jj_map = null;

var locationsLatitudes = new Array();
var locationsLongitudes = new Array();
var locationsDivs = new Array();
// Creates a marker at the given point with the given number label
function createMarker(point, HTML) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(HTML);
  });
  return marker;
}

function addLocationPopUp(lat, lon, divToLoad) {
	if (debug_map) {
		alert("Adding location: " + lat + ", " + lon + " with popup of div id: " + divToLoad);
	}
	//Make sure we've initialized
	if (jj_map == null) {
		alert("addLocation(): You must call buildMap() before you can add a location.");
		return;
	}
	
	//Get the html from the div
	var div = document.getElementById(divToLoad);
	if (div) {
		var popHTML = div.innerHTML;
		//Build the popup
		//jj_map.openInfoWindow(new GLatLng(lat, lon), popHTML);
		
		var point = new GLatLng(lat, lon);
  		jj_map.addOverlay(createMarker(point, popHTML));

	}
	else {
		alert("addLocation(): Invalid div id: " + divToLoad);
		return;
	}
}

function buildMap(divToBuildIn, lat, lon, zoom) {
	if (GBrowserIsCompatible()) {
	  jj_map = new GMap2(document.getElementById(divToBuildIn));
	  jj_map.setCenter(new GLatLng(lat, lon), zoom);
	}
	else {
		alert("We're sorry, but this browser is not capable of displaying Google Maps. You will not be able to see the locations for MSI Wireless.");
	}
	jj_map.addControl(new GSmallMapControl());
}

function getMap() {
	if (jj_map == null) {
		alert("There is no map initialized. Call buildMap to build the map.");
	}
	return jj_map;
}
function jj_onLoad() {
	if (debug_map) {
		alert("Building map!");
	}
	buildMap(startDiv, startLatitude, startLongitude, startZoom);
	if (debug_map) {
		alert("Add locations: " + locationsLatitudes.length);
	}
	for (i = 0; i < locationsLatitudes.length; i++) {
		//Make sure we have valid data
		if (locationsLatitudes[i] && locationsLongitudes[i] && locationsDivs[i]) {
			addLocationPopUp(locationsLatitudes[i], locationsLongitudes[i], locationsDivs[i]);
		}
		else {
			alert("Invalid location: " + i + ". Check to make sure latitude, longitude, and div for this location are initialized properly.");
		}
	}
	if (onMapLoad) {
		onMapLoad(jj_map);
	}
}
	
if (!document.all) {
	//FIREFOX IS BEING USED
	window.addEventListener("load",jj_onLoad,false);
}
else {
	//INTERNET EXPLORER IS BEING USED
	window.attachEvent("onload",jj_onLoad);
}