var geocoder = null;
var map = null;
var locationCount = -1;
// Creates a marker whose info window displays the letter corresponding
// to the given index.
initialize();

function createMarker(point, htmlstring) {
    var marker = new GMarker(point);
    map.addOverlay(marker);
    GEvent.addListener(marker, "click", function () {
        marker.openInfoWindowHtml(htmlstring);
    });
}
function addMarker(point, address, htmlString) {
    if (point) {
        createMarker(point, htmlString);
    } else if (geocoder) {
        geocoder.getLatLng(address, function (pointResultAsync) {
            if (pointResultAsync) {
                createMarker(pointResultAsync, htmlString);
            }
        });
    }
}
function addMarkersFromXML() {
    var request = GXmlHttp.create();
    request.open('GET', 'http://gametruckparty.com/wp-content/themes/gametruck/js/gtloc.xml', true);
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            var xmlDoc = request.responseXML;
            var xmlrows = xmlDoc.documentElement.getElementsByTagName("loc");
            locationCount = xmlrows.length;
            for (var i = 0; i < locationCount; i++) {
                var xmlrow = xmlrows[i];
                var point = null;
                // check to see if we have lat/lon information - this will lessen
                // calls to the Geocoder, which can only handle ~500-600 requests per IP per hour!
                var xmlcellLat = xmlrow.getElementsByTagName("lat")[0];
                if (xmlcellLat) {
                    var xmlcellLon = xmlrow.getElementsByTagName("lon")[0];
                    if (xmlcellLon) {
                        var lat = parseFloat(xmlcellLat.firstChild.data);
                        var lon = parseFloat(xmlcellLon.firstChild.data);
                        point = new GLatLng(lat, lon);
                    }
                }                
                // get the address - we'll use this to look up the location using the Geocoder
                // if we don't have coordinates
                var xmlcellAddress = xmlrow.getElementsByTagName("address")[0];
                var celltextAddress = xmlcellAddress.firstChild.data;
                var htmlString = "<div style='font-family:tahoma;font-size:12px;'>" + "<b>" + celltextAddress + "</b>";// + "<br/><br/>";
                addMarker(point, celltextAddress, htmlString);
            }
        }
    };
    request.send(null);
}
function initialize() {
    if (GBrowserIsCompatible()) {
	 var map_canvas = document.getElementById("map_canvas");
        map = new GMap2(map_canvas);
        map.setCenter(new GLatLng(39.54257455694, -96.85546875), 3);
        map.setUIToDefault();
        geocoder = new GClientGeocoder();
        addMarkersFromXML();
    }
}
