var w = 1280,
    h = 800;

var projection = d3.geoAlbersUsa();

var path = d3.geoPath()
    .projection(projection);

var svg = d3.select("body").insert("svg:svg", "h2")
    .attr("width", w)
    .attr("height", h);

var states = svg.append("svg:g")
    .attr("id", "states");

var circles = svg.append("svg:g")
    .attr("id", "circles");

var cells = svg.append("svg:g")
    .attr("id", "cells");

d3.json("data/us-states.json", function(collection) {
  states.selectAll("path")
      .data(collection.features)
    .enter().append("svg:path")
      .attr("d", path);
});

function sumArrayCSV(array,prop,condition) {
  var sum = 0;
  for (i=0; i<array.length; i++) {
    sum += parseFloat(array[i][prop]);
  }
  return sum;
}

d3.csv("data/passengers-summarized.csv", function(fare) {
  var sum = sumArrayCSV(fare,'Passengers');

  // var radius = d3.scale.sqrt()
  //             .domain([0,])
});

// d3.csv("flights-airport.csv", function(flights) {
//   var linksByOrigin = {},
//       countByAirport = {},
//       locationByAirport = {},
//       positions = [];
//
//   var arc = d3.geo.greatArc()
//       .source(function(d) { return locationByAirport[d.source]; })
//       .target(function(d) { return locationByAirport[d.target]; });
//
//   flights.forEach(function(flight) {
//     var origin = flight.origin,
//         destination = flight.destination,
//         links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
//     links.push({source: origin, target: destination});
//     countByAirport[origin] = (countByAirport[origin] || 0) + 1;
//     countByAirport[destination] = (countByAirport[destination] || 0) + 1;
//   });
//
//   d3.csv("airports.csv", function(airports) {
//
//     // Only consider airports with at least one flight.
//     airports = airports.filter(function(airport) {
//       if (countByAirport[airport.iata]) {
//         var location = [+airport.longitude, +airport.latitude];
//         locationByAirport[airport.iata] = location;
//         positions.push(projection(location));
//         return true;
//       }
//     });
//
//
//     var g = cells.selectAll("g")
//         .data(airports)
//       .enter().append("svg:g");
//
//     g.append("svg:path")
//         .attr("class", "cell")
//         .attr("d", function(d, i) { return "M" + polygons[i].join("L") + "Z"; })
//         .on("mouseover", function(d, i) { d3.select("h2 span").text(d.name); });
//
//     g.selectAll("path.arc")
//         .data(function(d) { return linksByOrigin[d.iata] || []; })
//       .enter().append("svg:path")
//         .attr("class", "arc")
//         .attr("d", function(d) { return path(arc(d)); });
//
//     circles.selectAll("circle")
//         .data(airports)
//       .enter().append("svg:circle")
//         .attr("cx", function(d, i) { return positions[i][0]; })
//         .attr("cy", function(d, i) { return positions[i][1]; })
//         .attr("r", function(d, i) { return Math.sqrt(countByAirport[d.iata]); })
//         .sort(function(a, b) { return countByAirport[b.iata] - countByAirport[a.iata]; });
//   });
// });

@@ -207,24 +207,53 @@
 ight%22, h);%0A%0A
+var centroidPositions = %5B%5D;%0A%0A
 var states =
@@ -265,32 +265,32 @@
 append(%22svg:g%22)%0A
-
     .attr(%22id%22, 
@@ -633,18 +633,8 @@
 prop
-,condition
 ) %7B%0A
@@ -880,16 +880,16 @@
 .sqrt()%0A
-
   //    
@@ -916,16 +916,980 @@
 %5D)%0A%7D);%0A%0A
+d3.json(%22data/us-states-centroids.json%22, function(json) %7B%0A%0A  var centroids = json.features.filter(function(centroid) %7B%0A    var location = %5B+centroid.geometry.coordinates%5B0%5D, +centroid.geometry.coordinates%5B1%5D%5D;%0A    var projectedLocation = projection(location);%0A    if (projectedLocation !== null) %7B%0A      centroidPositions.push(projectedLocation);%0A    %7D%0A  %7D);%0A%0A  circles.selectAll(%22circle%22)%0A    .data(json.features)%0A    .enter()%0A    .append(%22circle%22)%0A    .attr(%22r%22,2)%0A    .attr(%22cx%22, function(d, i) %7B%0A      // return centroidPositions%5Bi%5D%5B0%5D;%0A      var location = projection(%5Bjson.features%5Bi%5D.geometry.coordinates%5B0%5D,json.features%5Bi%5D.geometry.coordinates%5B1%5D%5D);%0A      if (location !== null) %7B%0A        return location%5B0%5D;%0A      %7D%0A    %7D)%0A    .attr(%22cy%22, function(d, i) %7B%0A      var location = projection(%5Bjson.features%5Bi%5D.geometry.coordinates%5B0%5D,json.features%5Bi%5D.geometry.coordinates%5B1%5D%5D);%0A      if (location !== null) %7B%0A        return location%5B1%5D;%0A      %7D%0A    %7D);%0A%7D);%0A%0A
 // d3.cs

add point in states