/**
* Heavily based on Mike Bostocks work: http://bost.ocks.org/mike/leaflet/
* @param  {[type]} window    [description]
* @param  {[type]} document  [description]
* @param  {[type]} L         [description]
* @param  {[type]} undefined [description]
* @return {[type]}           [description]
*/
(function (window, document, L, undefined) {
	'use strict';

	/* create leaflet map */
	var map = L.map('map', {
		center: [44.8997, -0.8157],
		zoom: 9
	});

	/* add default stamen tile layer */
	new L.tileLayer('http://{s}.tiles.mapbox.com/v3/examples.map-vyofok3q/{z}/{x}/{y}.png', {
		minZoom: 0,
		maxZoom: 18,
		attribution: 'Map data © <a href="http://www.openstreetmap.org">OpenStreetMap contributors</a>'
	}).addTo(map);

	var svg = d3.select(map.getPanes().overlayPane).append('svg'),
	g = svg.append('g').attr('class', 'leaflet-zoom-hide');

	d3.json('scripts/gironde-epci.topo.json', function (collection) {
		var bounds = d3.geo.bounds(topojson.feature(collection, collection.objects['gironde-epci.geo']));
		var path = d3.geo.path().projection(projectPoint);


		var feature = g.selectAll('path')
			.data(topojson.feature(collection, collection.objects['gironde-epci.geo']).features)
			.enter().append('path');

		map.on('viewreset', reset);
		reset();

		// Reposition the SVG to cover the features.
		function reset() {
			var bottomLeft = projectPoint(bounds[0]),
				topRight = projectPoint(bounds[1]);

			svg.attr('width', topRight[0] - bottomLeft[0])
				.attr('height', bottomLeft[1] - topRight[1])
				.style('margin-left', bottomLeft[0] + 'px')
				.style('margin-top', topRight[1] + 'px');

			g.attr('transform', 'translate(' + -bottomLeft[0] + ',' + -topRight[1] + ')');
			
			feature.attr('d', path);
		}

		// Use Leaflet to implement a D3 geographic projection.
		function projectPoint(x) {
			var point = map.latLngToLayerPoint(new L.LatLng(x[1], x[0]));
			return [point.x, point.y];
		} 
	});
}(window, document, L));
@@ -1093,20 +1093,220 @@
 ectAll('
-path
+.entity')%0A%09%09%09.data(topojson.feature(collection, collection.objects%5B'gironde-epci.geo'%5D).features)%0A%09%09%09.enter()%0A%09%09%09%09.append('path')%0A%09%09%09%09.attr('class', 'entity')%0A%09%09%09;%0A%09%09var label = g.selectAll('.entity-label
 ')%0A%09%09%09.d
@@ -1399,16 +1399,21 @@
 .enter()
+%0A%09%09%09%09
 .append(
@@ -1413,22 +1413,61 @@
 append('
-path')
+text')%0A%09%09%09%09.attr('class', 'entity-label')%0A%09%09%09
 ;%0A%0A%09%09map
@@ -1965,19 +1965,644 @@
  path);%0A
-%09%09%7D
+%0A%09%09%09label.attr('id', function (d) %7B return d.id; %7D)%0A%09%09%09%09.attr('class', 'entity-label')%0A%09%09%09%09.attr('transform', function (d) %7B return 'translate(' + path.centroid(d) + ')'; %7D)%0A%09%09%09%09.attr('x', -20)%0A%09%09%09%09.attr('dy', '.35em')%0A%09%09%09%09.text(function (d) %7B return toProperCase(d.id); %7D)%0A%09%09%09;%0A%09%09%7D%0A%0A%09%09/*%0A%09%09%09Preserve keywords (CUB, COBAN, COBAS, CDC) in uppercase.%0A%09%09%09@return %7Bstring%7D%0A%09%09 */%0A%09%09function toProperCase(str) %7B%0A%09%09%09var pc =  str.replace(/%5Cw%5CS*/g, function (s) %7B%0A%09%09%09%09return s.charAt(0).toUpperCase() + s.substr(1).toLowerCase();%0A%09%09%09%7D);%0A%09%09%09return pc.replace(/cub%7Ccoban%7Ccobas%7Ccdc/i, function (m) %7B%0A%09%09%09%09return m.toUpperCase();%0A%09%09%09%7D);%0A%09%09%7D;
 %0A%0A%09%09// U
@@ -2780,17 +2780,16 @@
 .y%5D;%0A%09%09%7D
- 
 %0A%09%7D);%0A%7D(

add labels to entities