/** 
 * This function will retrieve the contents of the XML file containing markers using AJAX.
 *
 * @author Momchil Kyurkchiev
 * @date March, 2007
 */

function getArrayOfMarkers(xmlFile) {  
  // parse XML data file into an array of marker HTML DOM nodes.        
  request.open("GET", xmlFile, true);
  request.onreadystatechange = ajaxHook;
  request.send(null);
} // end of getArrayOfMarkers function

// ajaxHook is invoked when the clients receives the XML form the server asynchronously.
function ajaxHook() {
  if (request.readyState == 4) {
    var xmlDoc = request.responseXML;
    var mapType = xmlDoc.documentElement.attributes[0].value;
    if (mapType == "") mapType = "googlemap"; // defaults to googlemap
    var markerHtmlNodes = xmlDoc.documentElement.getElementsByTagName("marker");
       
    // create a customized icon object 
    var baseIcon = new GIcon();
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.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);          

    // populate the markers array with Marker objects 
    for (var i = 0; i < markerHtmlNodes.length; i++) {     

      // each marker icon displays a letter name 
      var letter = String.fromCharCode("A".charCodeAt(0) + i%26);
      var icon = new GIcon(baseIcon);
      icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

      var point = new GLatLng(markerHtmlNodes[i].getAttribute("lat"), markerHtmlNodes[i].getAttribute("lng")); 

      // extend the current bounds 
      bounds.extend(point);

      markers[i] = new Marker(i,
          parseFloat(markerHtmlNodes[i].getAttribute("lat")), // lat
          parseFloat(markerHtmlNodes[i].getAttribute("lng")), // lng
          point, 
          new GMarker(point, icon), // gmarker
          markerHtmlNodes[i].getAttribute("html"), // html
          markerHtmlNodes[i].getAttribute("label"));  // label               
    }    
    
    // display the current markers on the appropriate map type (either campus map or world/google map)
    displayMap(mapType, markers);
  }        
}