//<![CDATA[

// global variables
      var map; 
	  var gdir;
	  var type;
	  var gmarkers = [];

		var iconBlue = new GIcon(); 
		iconBlue.image = 'http://www.google.com/mapfiles/markerH.png';
		iconBlue.shadow = 'http://www.google.com/mapfiles/shadow50.png';
		iconBlue.iconSize = new GSize(20, 34);
        iconBlue.shadowSize = new GSize(37, 34);
        iconBlue.iconAnchor = new GPoint(9, 34);
        iconBlue.infoWindowAnchor = new GPoint(9, 2);

	//Assign icon colours to service types 
    var customIcons = [];
	customIcons["hub"] = iconBlue;

    function load() {
      if (GBrowserIsCompatible()) {
	  //create the map
        map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(44.647239,-63.573911), 16); 
		var customUI = map.getDefaultUI();
		customUI.controls.menumaptypecontrol = false;
		customUI.zoom.scrollwheel = false;
		map.setUI(customUI);
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		gdir=new GDirections(map, document.getElementById("directions"));

      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "Missing Address: The address was either missing or had no value.";
      // catch Directions errors 
      GEvent.addListener(gdir, "error", function() {
      var code = gdir.getStatus().code;
      var reason="Code "+code;
      if (reasons[code]) {
      reason = reasons[code]
        } 
      alert("Failed to obtain directions - "+reason);
      });
       GDownloadUrl("/wp-content/themes/hub/hub.xml", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
          type = markers[i].getAttribute("type"); //global variable used in filter function
	        name = markers[i].getAttribute("name");
			var lat = markers[i].getAttribute("lat");
			var lng = markers[i].getAttribute("lng");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createTabbedMarker(point,  type, name, lat, lng);
			map.addOverlay(marker);	
			if (type == "hub") {
            marker.show(); 
			}
		 else {
			marker.hide();
			}
		  }
        });		
      }
    }

//Function to clear text from Directions input box
	function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}
	
	// Request directions
      function getDirections() {
        var saddr = document.getElementById("saddr").value
        var daddr = document.getElementById("daddr").value
        gdir.load("from: "+saddr+" to: "+daddr, { "locale": "en" });
		
      }

	 function zoomIn() {
        map.zoomIn();
      }      

      function zoomOut() {
        map.zoomOut();
      }      

//function to clear directions and recenter map, used in infoWindow
function recenterMap() {
	gdir.clear();
	map.checkResize();
	map.setCenter(new GLatLng(44.647239,-63.573911), 16);
}

 function createTabbedMarker(point, type, lat, lng) {
  var marker = new GMarker(point, customIcons[type]);
  marker.type = type;                                 
 gmarkers.push(marker);
      var html = '<div class="infoWindow"><h4>'+name+'</h4><p class="zoom"><a href="#zoominlink" onclick="javascript:zoomIn(); return false;"><strong>Zoom In</strong></a>  <a href="#zoomoutlink" onclick="javascript:zoomOut(); return false;"><strong>Zoom Out</strong></a></div>';
      return marker;

    }

    //]]>