 #= require mappings
 
-width = 800
+width  = 800
 height = 800
 vis    = null
+places = null
+neighborhoods = null
 
+fill = d3.scale.log().range ['#6cd7ff', "#006bbb"]
 projection = d3.geo.albers().rotate([120])
 path = d3.geo.path().projection(projection)
 

          row[key] = parseFloat val if key isnt 'NEIGHBORHOOD'
        row
      out[years[index]] = data
      callback(out) if index == 1
    .attr('d', path)

$ ->
  vis = d3.select('.js-map').append('svg')
    .attr('width', width)
    .attr('height', height)

  menu = d3.select('.js-menu').on 'change', (d) ->
    key = d3.event.target.value
    highlight key

  for key of __mappings
    menu.append('option')
      .attr('value', key)
      .text(key)

highlight = (key) ->
  [min, max] = d3.extent places, (d) -> d.value[key][1]
  fill.domain [min, max]
  vis.selectAll('.neighborhood:not(.shared):not(.unclaimed)')
    .style('fill', (d) ->
      name = d.properties.NAME
      place = places.filter((p) -> p.key == name)[0]
      console.log place, name
      fill(place.value[key][0])
    )

mapDataToNeighborhoods = (data) ->
  nhoods = {}
  for hood in neighborhoods.features
    name = hood.properties.NAME
    shared = hood.properties.SHARED

    if !shared?
      current = nhoods[name] = {}
      current[2010] = data[2010].filter((d) -> d.NEIGHBORHOOD == name)[0]
      current[2000] = data[2000].filter((d) -> d.NEIGHBORHOOD == name)[0]

      for key, ids of window.__mappings
        try
          from = d3.sum(ids[0].map (id) -> current[2000][id])
          to   = d3.sum(ids[1].map (id) -> current[2010][id])
          change = to - from
          growth = parseFloat ((change / from) * 100).toFixed(2)
          current[key] = [from, to, change, growth]
        catch e
          console.log "ERROR!!!! #{e} #{name} #{key}"

      delete current[2000]
      delete current[2010]

  places = d3.entries(nhoods).filter (nh) -> !shouldExcludeNeighborhood(nh.key)

loadCensusData = (callback) ->
  out  = {}
