﻿var map;
var gdir;
var gmarkers = [];
var gmap_list = "";
var i = 0;
var sidePanel= false;

   
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];


function PopMarker(i) {
    GEvent.trigger(gmarkers[i], "click");
}

function createMarker(point, name, html, sidePanelitem, bDirection) {

    gmap_list += '<a href="javascript:myclick(' + i + ')">' + sidePanelitem + '</a>';     
    var baseIcon = new GIcon();
    baseIcon.shadow = '/images/marker_shadow.png';
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25); 
    
    var icon = new GIcon(baseIcon);
    icon.image = '/images/marker_01.png';
 
    var marker = new GMarker(point, icon);    

    if (bDirection)
    {
        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
           '<br>Start address or full postcode:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                  // "(" + name + ")" + 
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
           '<br>End address or full postcode:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
    }

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    gmarkers[i] = marker;
    i++;
    return marker;
}

function LoadMap(XmlFile, bDirection) {

    if (GBrowserIsCompatible()) {

        var request = GXmlHttp.create();
        request.open("GET", XmlFile, true);
        request.onreadystatechange = function() {
            if (request.readyState != 4) {
            } else {
                var xmlDoc = GXml.parse(request.responseText);    
                            
                var center = xmlDoc.documentElement.getElementsByTagName("center");
                var lat = parseFloat(center[0].getAttribute("lat"));
                var lng = parseFloat(center[0].getAttribute("lng"));
                var zoom = parseFloat(center[0].getAttribute("zoom"));
                var control = center[0].getAttribute("control").toString();
                var typeControl = center[0].getAttribute("typeControl").toString();
                var pop = (center[0].getAttribute("pop").toString() == "true");
                sidePanel = (center[0].getAttribute("sidePanel").toString() == "true");
                
                var point = new GLatLng(lat, lng);
                
                map = new GMap2(document.getElementById("map"));   
                
                
                if (control == "l"){
                    map.addControl(new GLargeMapControl());
                }
                else{    
                    map.addControl(new GSmallMapControl());
                }
                if (typeControl == "true"){              
                    map.addControl(new GMapTypeControl());
                }          
                //map.enableScrollWheelZoom();       
                map.setCenter(point, zoom);  
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                for (var i = 0; i < markers.length; i++) {
                    lat = parseFloat(markers[i].getAttribute("lat"));
                    lng = parseFloat(markers[i].getAttribute("lng"));
                    point = new GLatLng(lat, lng);
                    var html = markers[i].getAttribute("html");
                    var label = markers[i].getAttribute("label");                       
                    var sidePanelitem = "";
                    if (sidePanel)
                        var sidePanelitem = markers[i].getAttribute("sidePanelItem");
                        
                    var marker = createMarker(point, label, html, sidePanelitem, bDirection);     
                    map.addOverlay(marker);
                }
                if (pop){
                    PopMarker(0);
                }
                if (document.getElementById("MapSidePanel") != null)
                    document.getElementById("MapSidePanel").innerHTML = gmap_list;
            }
            
        }
        request.send(null);

    }
    else {
        alert("Sorry, the Google Maps API is not compatible with your browser");
    }
}

      function myclick(i) {
        GEvent.trigger(gmarkers[i], "click");
      }


      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }