 
       $(this).toggleClass('selected')
 
-
-
   @go: ->
-    $('#js-datastore').data('stats', {})
     @loadFile()
       .then(@parseHtml)
       .then(@createMenu)

      title = $(this).text().replace(titleRe, '$1')
      categoryAndRoom = $(this).text().replace(titleRe, '$2')

      if source of Stat.get_stats() # CoffeeScript 'of' equals JS 'in'
        data = Stat.get_stats()[source]
        data.urls.push {url, year, month}
        $('#js-datastore').data('stats', obj)
      else
        obj = Stat.get_stats()
        obj[source] = {title, categoryAndRoom, urls: [{url, year, month}]}
        $('#js-datastore').data('stats', obj)

  @createMenu: ->
    $('#menu-container').append('<ul></ul>')
    for k, v of Stat.get_stats()
      $('#menu-container>ul').append("<li id=#{k}>#{v.title}<ul></ul></li>")
      for u in v.urls
        divid = u.url.replace(/\./g, '_') # '.' in id breaks jQuery!
        $("#menu-container>ul>li[id=#{k}]>ul")
          .append("<li><a title='show/hide graph for #{u.year} #{u.month}'
            href='javascript:void(0)'
            data-div-id=#{divid}
            data-url=#{u.url}
            data-title=\"#{v.title}: #{u.year}-#{u.month}\">#{u.year}-#{u.month}</a></li>")
    $('#menu-container a').click ->
      # TODO move remaining code into Graph method
      anchor = this
      divid = $(this).attr('data-div-id')
      if $(this).hasClass('selected')
        #$(this).parent().remove()
        $("##{divid}").parent().remove()
      else
        $('#graph-container').append("<div class=dygraph-wrapper>
          <a title=close data-div-id=#{divid} class=close-graph href='javascript:void(0)'>x</a>
          <div class=dygraph id=#{divid}><span class=loading>loading</span></div>
          </div")
        g = new Graph
          name: $(this).attr('data-title')
          url: $(this).attr('data-url')
          div: divid
        .create()

      $('.close-graph').click (event) ->

    $("##{@div}").width($(window).width() - $("##{@div}").css('margin').replace(/[^-\d\.]/g, '')*4)

class Stat
  @loadFile: -> $.ajax { url: '/stats', dataType: 'html' }

  @parseHtml: (htmlDoc) =>
    stats = {}
    $(htmlDoc).find('li>a').map ->
      url = $(this).attr('href')
      urlRe = /([0-9a-f-]{35})\.([0-9]{6})\.xml/
      sourceId = url.replace(urlRe, '$1')
      period = url.replace(urlRe, '$2')
      year = period.slice 0,4
      month = period.slice -2
