﻿var gmarkers = []; // global var to house markers.  Used to set default html window bubble.
var defaultLocationMarker; // global var to house the default location for the selected subset. Only applies for city location views.
var province, city; // global vars to house the current city and province
var map; // global var to house the page map
var bounds; // global var to house the map boundaries
var allLocations = true; // global var indicating whether all locations are being viewed
var currentMapType; // global var to house the current map type
var browser = navigator.appName;
    
// creates and loads the map
function loadMap() 
{
    if (GBrowserIsCompatible()) 
    {
        map = new GMap2(document.getElementById("map"));
        
        /* google earth control */
        map.addMapType(G_SATELLITE_3D_MAP);
		map.addControl(new GHierarchicalMapTypeControl());
      
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        // set the center to a default location since we don't know what the center should be yet (not until all the points are plotted
        map.setCenter(new GLatLng(0, 0), 0);
        
        bounds = new GLatLngBounds();
        GDownloadUrl("data.xml", parseMapXml);
        
        if (currentMapType != null)
            map.setMapType(currentMapType);
    }
}

function getMarkers(xml, expr)
{
    var markers;
    
    if (browser == "Netscape")
    {
        var iterator;
        var temp;
        markers = [];
        
        iterator = xml.evaluate(expr, xml.documentElement, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
        
        while (temp = iterator.iterateNext())
        {
            markers.push(temp);
        }
    }
    else if (browser == "Microsoft Internet Explorer")
    {
        xml.setProperty("SelectionLanguage", "XPath");
        markers = xml.selectNodes(expr);    
    }
    
    return markers;
}

// parses the locations xml file, creating markers and setting up the map boundaries
function parseMapXml(data) 
{
    var xml = GXml.parse(data);
    var markers;
    var m;
    
    gmarkers = [];

    if ((province != null && province.length > 0) && (city != null && city.length > 0))
    {
        markers = getMarkers(xml, "/Locations/Province[@name='" + province + "']/City[@name='" + city + "']/markers/marker");
    }
    else if ((province != null && province.length > 0) && (city == null || city.length == 0))
    {
        markers = getMarkers(xml, "/Locations/Province[@name='" + province + "']/child::*/markers/marker");
    }
    else
    {
        markers = xml.documentElement.getElementsByTagName("marker");
    }
    
    for (var i = 0; i < markers.length; i++) 
    {
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                parseFloat(markers[i].getAttribute("lng")));
        
        // extend the map bounds to include the new point
        bounds.extend(point);
        map.addOverlay(createMarker(point, markers[i].getAttribute("name"), markers[i].getAttribute("address"), markers[i].getAttribute("defaultLocation")));
    }
     
    // set the zoom of the map to include all points
    var zoomLevel = map.getBoundsZoomLevel(bounds);
    
    if (zoomLevel == 17)
        zoomLevel = 12;
    else if(zoomLevel == 3)
        zoomLevel = 4;
    else
        zoomLevel--;
    
    map.setZoom(zoomLevel);
    
    // set the map center to the center of the bound
    map.setCenter(bounds.getCenter());
    
    if (allLocations)
        setTimeout("GEvent.trigger(gmarkers[0], 'click')", 0);
}
        
// creates markers on the map for each loaded location and wires up the click event
function createMarker(point, name, address, defaultLocation) 
{
    var add = address.replace('|','<br />')
    add = add.replace('|','<br />');
    add = add.replace('|','<br />');
    add = add.replace('|','<br />');
    add = add.replace('|','<br />');
    add = add.replace('|','<br />');
    add = add.replace('|','<br />');
    var marker = new GMarker(point);

    GEvent.addListener(marker, "click", function() 
        {
            marker.openInfoWindowHtml("<div class='bubble'><h1>" + name + '</h1><p>' + add + "</p></div>");
            document.getElementById("mapLocation").innerHTML = "<h1>" + name + "</h1><p>" + add + "</p>";
        });
      
    // fire the click event on this marker if it is the default location and only city markers are being displayed.
    if (defaultLocation == "true" && (city != null && city.length > 0))
    {
        defaultLocationMarker = marker;
        setTimeout("GEvent.trigger(defaultLocationMarker, 'click')", 0);
    }
       
    gmarkers.push(marker);
    return marker;
}

// updates the province and city variables, clears the selected location div, and reloads the map with the new locations
function updateLocation(newProvince, newCity)
{
    currentMapType = map.getCurrentMapType();

    if (newProvince == '' && newCity == '')
        allLocations = true;
    else
        allLocations = false;
        
    province = newProvince;
    city = newCity;
    document.getElementById("mapLocation").innerHTML = "";
    
    loadMap();
}

