﻿// 19-11-07
//
// Company-Net
//
// Miller PX
//
// Gererate a google Map on the page referencing this script
// and add a collection of map overlays whos points are defined in the external xml file.
//
// The page referncing this script should contain an html div element named (ID) map

var currentMarker = null;

var currentPopUp = null;

if (GBrowserIsCompatible()) {

    // Center Points
    var LAT_CENTER = 55.0;
    var LNG_CENTER = -4.0;
    
    // default zoom level
    var ZOOM_LVL = 5;

    // Display the map 
    var map = new GMap2(document.getElementById("map"));

    // Center location
    map.setCenter(new GLatLng(LAT_CENTER,LNG_CENTER),ZOOM_LVL);
    
    // disable dragging
    map.disableDragging();
    
    // disable double click to zoom
    map.disableDoubleClickZoom();
    
    // disable scroll zoom
    map.disableScrollWheelZoom();

    // Read the data from countries.xml
    var request = GXmlHttp.create();
    request.open("GET", "RegionXml.axd", true);
    request.onreadystatechange = function() 
    {
        if (request.readyState == 4) 
        {
            var xmlDoc = GXml.parse(request.responseText);      
            // ========= Now process the polygons ===========
            var states = xmlDoc.documentElement.getElementsByTagName("region");
            regionArray = new Array(states.length);
            // read each region
            for (var a = 0; a < states.length; a++) 
            {
                // get region attributes
                var label  = states[a].getAttribute("name");
                var colour = states[a].getAttribute("colour");
                var clat = states[a].getAttribute("clat");
                var clng = states[a].getAttribute("clng");
                var propCnt = states[a].getAttribute("propCount");
                var regionId = states[a].getAttribute("regionID");
                // read each point on that line
                var points = states[a].getElementsByTagName("point");
                var pts = [];
                for (var i = 0; i < points.length; i++) 
                {
                    pts[i] = new GLatLng(parseFloat(points[i].getAttribute("lat")),parseFloat(points[i].getAttribute("lng")));
                }
                // GPolygon(points, strokeColor?, strokeWeight?, strokeOpacity?, fillColor?, fillOpacity?, opts?)
                var poly = new GPolygon(pts,"#000000",1,0,colour,0);
                poly.RegionName = label;
                poly.PropCount = propCnt;
                poly.RegionId = regionId;
                poly.Centre = new GLatLng(parseFloat(clat), parseFloat(clng));        
                
                // ----------------------------------
                // Poly click event handler - remove any existing popup and add a new one
                // as defined in XML     
                GEvent.addListener(poly,"click",function(){
                     var marker = new GMarker(this.Centre);
                     var html = BuildTipHTML(this.RegionName, this.PropCount + (this.PropCount!=1 ? " properties" : " property") , this.RegionId);
                     if(currentPopUp!=null) map.removeOverlay(currentPopUp);
                     currentPopUp = new EWindow(map,'0');
                     map.addOverlay(currentPopUp);
                     currentPopUp.openOnMarker(marker,html);
                });
                // add the current poly to the map
                map.addOverlay(poly);
            } 
        }
    }
    // request.send(null);
    
}
// display a warning if the browser was not compatible
else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
}