var map, manager;
var centerLatitude = 48.529732, centerLongitude = -4.065628, startZoom = 10;


function createMarkerClickHandler(marker, title, number, text) {
  return function() {
    marker.openInfoWindowHtml(
      "<h3>" + title + "</h3><p>(" + number + " inscrits)</p>" +
      "<p class='liste'>" + text + "</p>"
    );
    return false;
  };
}


function createMarker(pointData) {
  var latlng = new GLatLng(pointData.latitude, pointData.longitude);

  var icon = new GIcon();
  if (pointData.nbr > 29) {
  icon.image = 'Images/red_marker36.png';
  icon.iconSize = new GSize(36, 36);
  icon.iconAnchor = new GPoint(14, -5);
  opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.nbr,
		"labelOffset": new GSize(-11, 6)
		};
  }
  else if (pointData.nbr > 19) {
  icon.image = 'Images/red_marker32.png';
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(14, -5);
  opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.nbr,
		"labelOffset": new GSize(-14, 3)
		};
  }
  else if (pointData.nbr > 9) {
  icon.image = 'Images/red_marker28.png';
  icon.iconSize = new GSize(28, 28);
  icon.iconAnchor = new GPoint(14, -3);
  opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.nbr,
		"labelOffset": new GSize(-16, 1)
		};
  }
  else {
  icon.image = 'Images/red_marker24.png';
  icon.iconSize = new GSize(24, 24);
  icon.iconAnchor = new GPoint(14, -5);
  opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.nbr,
		"labelOffset": new GSize(-17, 0)
		};
  }
  
  
  icon.infoWindowAnchor = new GPoint(25, 7);

  
 
  var marker = new LabeledMarker(latlng, opts);
  var handler = createMarkerClickHandler(marker, pointData.name, pointData.nbr, pointData.inscrits);
	
  GEvent.addListener(marker, "click", handler);

  /*var listItem = document.createElement('li');
  listItem.innerHTML = '<div class="label">'+pointData.nbr+'</div><a href="' + pointData.nbr + '">' + pointData.name + '</a>';
  listItem.getElementsByTagName('a')[0].onclick = handler;

  document.getElementById('sidebar-list').appendChild(listItem);*/

  return marker;
}

/*function windowHeight() {
  // Standard browsers (Mozilla, Safari, etc.)
  if (self.innerHeight) {
    return self.innerHeight;
  }
  // IE 6
  if (document.documentElement && document.documentElement.clientHeight) {
   return document.documentElement.clientHeight;
  }
  // IE 5
  if (document.body) {
    return document.body.clientHeight;
  }
  // Just in case. 
  return 0;
}*/

/*function handleResize() {
  var height = windowHeight() - 30;
  document.getElementById('map').style.height = height + 'px';
  /*document.getElementById('sidebar').style.height = height + 'px';
}
*/
function init() {
  /*handleResize();*/
	
  map = new GMap(document.getElementById("map"));
  map.addControl(new GSmallMapControl());
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom); //centre la carte au lat et long spécifiés plus haut
  map.addControl(new GMapTypeControl());

  manager = new MarkerManager(map);
	
  // This is a sorting trick, don't worry too much about it.
  //markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
  batch = [];
  for(id in markers) {
    batch.push(createMarker(markers[id]));
  }
  manager.addMarkers(batch, 10);
  manager.refresh();
}

/*window.onresize = handleResize;*/
window.onload = init;
window.onunload = GUnload;

