//the google map object
var map;

//aray of markers so we can keep track of which to remove
var nodes = new Array();

// default icon
var baseicon = new GIcon();
baseicon.image = "http://www.brest-wireless.net/gmap/node_online.png";
baseicon.iconSize = new GSize(18, 38);
baseicon.shadowSize = new GSize(0, 00);
baseicon.iconAnchor = new GPoint(9, 28);
baseicon.infoWindowAnchor = new GPoint(9, 10);


// creates a node's marker
function createMarker(id, point,html,ssid,mac,wiki,email,iconfile) {
  var node = new Array(); //id + marker + ssid + mac + wiki + email + HTMLinfo
  
  node['id'] = id;
  
	if (iconfile) {
    ii = new GIcon(baseicon);
    ii.image = 'http://www.brest-wireless.net/gmap/' + iconfile;
  }	else {
    ii = baseicon;
  }
	node['marker'] = new GMarker(point,ii);
	node['ssid'] = ssid ? ssid : "";
	node['mac'] = mac ? mac : "";
	node['wiki'] = wiki ? wiki : "";
	node['email'] = email ? email : "";
	
	// builds the HTML info
  html += '<b><u>SSID: '+ssid+'</u></b> <br />';
  if (mac)   { html += '<b>MAC:</b> ' + mac +'<br />'; }
  if (wiki)  { html += '<b>Wiki:</b> <a href=http://www.brest-wireless.net/wiki/nodes:'+wiki+' target=_blank>' + wiki +'</a> <br />'; }
  if (email) { html += '<b>Email:</b> <a href=mailto:'+ email +'>' + email + '</a> <br />'; }
	node['html'] = html;

  // add an event listener
  GEvent.addListener(node['marker'], "click", function() {
    node['marker'].openInfoWindowHtml(node['html']);
  });
        
	return node;
}

// parses the nodes' DB
function update_map() {
  var request = GXmlHttp.create();
  request.open("GET", "data2.xml", true);
  
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
      var markers = xmlDoc.documentElement.getElementsByTagName("marker");
      
      for(var i = 0; i < markers.length; i++) {
        id = markers[i].getAttribute("id");
        var temp = markers[i];
        
        // coordinates
        var point = new GPoint(parseFloat(temp.getAttribute("lon")), parseFloat(temp.getAttribute("lat")));
        
        // creates a node
        nodes[id] = createMarker(id, point,"",temp.getAttribute("ssid"),temp.getAttribute("mac"),temp.getAttribute("wiki"), temp.getAttribute("email"),temp.getAttribute("icon")  );
        
        // ???
        //map.overlays.push(nodes[id]['marker']); 
        nodes[id]['marker'].initialize(map);
        nodes[id]['marker'].redraw(true);
      }
      
      map.reOrderOverlays();
    }
  }
  
  request.send(null);
}

// centers the map on the specified node
// and displays asociated informations
function goTo(id) {
  if((id >= 0) && (id < nodes.length)) {
    map.centerAtLatLng(nodes[id]['marker'].point);
    nodes[id]['marker'].openInfoWindowHtml(nodes[id]['html']);
  } else {
    map.closeInfoWindow();
  }
}

// where everything begins
function onLoad(id) {
  map = new GMap(document.getElementById("map"));
  
  // center on Infini AP ...
  center = new GPoint(-4.491026401519775,48.39073593478565);
  map.centerAndZoom(center, 3);
  
  map.openInfoWindow(map.getCenterLatLng(), document.createTextNode("Chargement ..."));
  
  map.setMapType(G_SATELLITE_TYPE);
  map.addControl(new GLargeMapControl());
  map.addControl(new GScaleControl());

  update_map();

  // update_map() needs time ...
  // ... we wait 2 seconds and continue
  window.setTimeout(function() {
    goTo(id);
  }, 2000);

}


