// This array stores data about each google map to be drawn on the page.
// Each array item should be an array with the keys: "div_id", "address", "latitude", and "longitude".
var googleMapRegistry = new Array();

function createMap(div_id, title, latitude, longitude, address) {
    var map = new GMap2(document.getElementById(div_id));
    map.addControl(new GLargeMapControl());
    var point = new GLatLng(latitude, longitude);
    map.setCenter(point, 13);
    
    var size = 0.001;
    //var size = 0.0005;
    var opacity = 1.0;
    var width = 1;

    var marker = new GMarker(point);
    map.addOverlay(marker);
    var myUrl = "http://maps.google.com/maps?oi=map&q="+escape(address)+"("+escape(title)+")@"+latitude+","+longitude;
    GEvent.addListener(marker, 'click', function() {
        document.location = myUrl;
    });
    return map;
}

function drawGoogleMaps() {
    if(GBrowserIsCompatible()) {
        for(i=0; i<googleMapRegistry.length; i++) {
            var div_id = googleMapRegistry[i]["div_id"];
            var title = googleMapRegistry[i]["title"];
            var latitude = googleMapRegistry[i]["latitude"];
            var longitude = googleMapRegistry[i]["longitude"];
            if( (! latitude) && (! longitude) ) { continue; }
            var address = googleMapRegistry[i]["address"];
            createMap(div_id, title, latitude, longitude, address);
        }
    } else {
        alert("Your browser's Javascript implementation is not compatible with the Google Maps API.");
    }
}

function registerGoogleMap(div_id, title, latitude, longitude, address) {
    if(googleMapRegistry.length == 0) {
        // Attach an event handler that will draw the maps after the whole page has loaded.
        // IE will freak if we try to draw sooner :(
        // Also add an unload handler to free memory.

        // Handle Firefox
        if(window.addEventListener) {
            window.addEventListener("load", drawGoogleMaps, false);
            window.addEventListener("unload", GUnload, false);
        // Handle IE
        } else if(document.attachEvent) {
            window.attachEvent("onload", drawGoogleMaps);
            window.attachEvent("onunload", GUnload);
        }
    }
    var ary = new Array();
    ary["title"] = title;
    ary["div_id"] = div_id;
    ary["latitude"] = latitude;
    ary["longitude"] = longitude;
    ary["address"] = address;
    googleMapRegistry[googleMapRegistry.length] = ary;
}
