// == Google Maps ==
// because the function closure trick doesnt work there
var gmarkers = [];
var htmls = [];
var i = 0;
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];
		
function CreateHowToFindUsMap(CountryXML){
	if (GBrowserIsCompatible()) {
	
		// create the map
		var map = new GMap2(document.getElementById("how-to-find-us-map"));
		map.setCenter(new GLatLng(0, 0), 15);
		// Add controls
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl(new GSize(120, 120)));
		
		// Enable double left click to zoom in and double right click to zoom out
		map.enableDoubleClickZoom()
		
		// ===== Start with an empty GLatLngBounds object =====     
		var bounds = new GLatLngBounds();
		
		// Read the data from all_offices.xml
		var request = GXmlHttp.create();
		request.open("GET", "/xml/" + CountryXML, true);
		request.onreadystatechange = function(){
			if (request.readyState == 4) {
				var xmlDoc = GXml.parse(request.responseText);
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				
				for (var i = 0; i < markers.length; i++) {
					// obtain the attribues of each marker
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var lng = parseFloat(markers[i].getAttribute("lng"));
					var point = new GLatLng(lat, lng);
					var html = markers[i].getAttribute("html");
					var label = markers[i].getAttribute("label");
					// create the marker
					var marker = CreateHowToFindUsMarker(point, label, html);
					map.addOverlay(marker);
					// ==== Each time a point is found, extent the bounds to include it - only used for the overview page =====
					bounds.extend(point);
				}
				// Open the first marker
				gmarkers[0].openInfoWindowHtml(htmls[0]);
				
				// ===== determine the zoom level from the bounds =====
				map.setZoom(map.getBoundsZoomLevel(bounds));
				
				// ===== determine the centre from the bounds ======
				var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
				var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
				
				// Added to clat to push the map down slightly
				map.setCenter(new GLatLng(clat, clng));
			}
		}
		request.send(null);
	}
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

// A function to create the marker and set up the event window
function CreateHowToFindUsMarker(point,name,html) {
    var marker = new GMarker(point);
		
    // The info window version with the "to here" form open
    to_htmls[i] = html + '<div class="get_directions"><p>Get Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a></p>' +
                         '<p class="start_address">Start address:</p><form action="http://maps.google.com/maps" method="get" target="_blank">' +
                         '<input type="text" SIZE=20 MAXLENGTH=20 name="saddr" id="saddr" value="" />' +
                         '<INPUT class="directions_button" value="Get Directions" TYPE="SUBMIT">' +
                         '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                         // "(" + name + ")" + 
                         '"/></div>';
                         
    // The info window version with the "to here" form open
    from_htmls[i] = html + '<div class="get_directions"><p>Get Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b></p>' +
                           '<p class="end_address">End address:</p><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
                           '<input type="text" SIZE=20 MAXLENGTH=20 name="daddr" id="daddr" value="" />' +
                           '<INPUT class="directions_button" value="Get Directions" TYPE="SUBMIT">' +
                           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                           // "(" + name + ")" + 
                           '"/></div>';

    // The inactive version of the direction info
    html = html + '<div class="get_directions"><p>Get Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></p></div>';
			
    GEvent.addListener(marker, "click", function() {
     marker.openInfoWindowHtml(html);
    });
		
    // save the info we need to use later for the side_bar
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
}

function CreateGlobalReachMap(){
	if (GBrowserIsCompatible()) {
	
		// create the map
		var map = new GMap2(document.getElementById("global-reach-map"));
		//map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(0, 0), 15);

		// Enable double left click to zoom in and double right click to zoom out
		map.enableDoubleClickZoom()
		
		// ===== Start with an empty GLatLngBounds object - only used for the overview page =====     
		var bounds = new GLatLngBounds();
		
		// Read the data from global_reach.xml
		var request = GXmlHttp.create();
		request.open("GET", "/xml/global_reach.xml", true);
		request.onreadystatechange = function(){
			if (request.readyState == 4) {
				var xmlDoc = GXml.parse(request.responseText);
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
				
				for (var i = 0; i < markers.length; i++) {
					// obtain the attribues of each marker
					var lat = parseFloat(markers[i].getAttribute("lat"));
					var lng = parseFloat(markers[i].getAttribute("lng"));
					var point = new GLatLng(lat, lng);
					var html = markers[i].getAttribute("html");
					var label = markers[i].getAttribute("label");
					// create the marker
					var marker = CreateGlobalReachMarker(point, label, html);
					map.addOverlay(marker);
					
					// ==== Each time a point is found, extent the bounds to include it - only used for the overview page =====
					bounds.extend(point);
				}
				// ===== determine the zoom level from the bounds =====
				map.setZoom(map.getBoundsZoomLevel(bounds));
				
				// ===== determine the centre from the bounds ======
				var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
				var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
				map.setCenter(new GLatLng(clat, clng));
			}
		}
		request.send(null);
	}
	else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

// A function to create the marker and set up the event window
function CreateGlobalReachMarker(point,name,html) {
    var marker = new GMarker(point);
		
    // The info window version with the "to here" form open
    to_htmls[i] = html;
                         
    // The info window version with the "to here" form open
    from_htmls[i] = html;

    // The inactive version of the direction info
    html = html;
			
    GEvent.addListener(marker, "click", function() {
     marker.openInfoWindowHtml(html);
    });
		
    // save the info we need to use later for the side_bar
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
}

// functions that open the directions forms
function tohere(i) {
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}