 
     d3.select("#rank-history svg").datum(data).transition().duration(500).call chart
     nv.utils.windowResize chart.update
-    window.chart = chart
     chart
 
-window.getYear = ->
-  new Date().getYear() + 1900
-
-window.getYears = ->
-  years = []
-  year = 2003
-  while year <= getYear()
-    years[year - 2003] = year
-    year++
-  years
-
-window.filterHistoryData = (data, fromYear, toYear) ->
+filterHistoryData = (data, fromYear, toYear) ->
   fromTime = Date.parse(fromYear + "/1/1")
   toTime = Date.parse(parseInt(toYear) + 1 + "/1/1")
   $.map data, (item) ->
     time = Date.parse(item[0])
     [item]  if time >= fromTime and time < toTime
 
-window.initFromYear = ->
-  $("[name=fromYear]").html ""
-  year = 2003
+router.get '/rank-history/:players', (req) ->
+  players  = req.params.players.split(',')
+  fromYear = req.params.fromYear || FIRST_YEAR
+  toYear   = req.params.toYear   || THIS_YEAR
 
-  while year <= getYear()
-    $("[name=fromYear]").append "<option>" + year + "</option>"
-    year++
-
-window.initToYear = (fromYear) ->
-  $("[name=toYear]").html ""
-  year = fromYear
-
-  while year <= getYear()
-    $("[name=toYear]").append "<option>" + year + "</option>"
-    year++
-
-window.changeFromYear = ->
-  fromYear = $("[name = fromYear]").val()
-  toYear   = $("[name = toYear]").val()
-  $("[name=toYear]").html ""
-  year = parseInt(fromYear)
-
-  while year <= getYear()
-    $("[name=toYear]").append "<option>" + year + "</option>"
-    year++
-
-  $("[name=toYear]").val (if toYear then toYear else getYear())
-
-router.get '/rank-history/:players', (req) ->
-  T('rank-history').render inside: '.main'
+  T('rank-history', fromYear, toYear).render inside: '.main'
 
   loadData "rankings", (rankings) ->
     updateGenerationTime rankings.generated_at
 
-    $("[name=fromYear]").change changeFromYear
+    T.each_with_index('player-field', rankings.data).render inside: '#players'
 
-    $.each rankings.data, (i, r) ->
-      return  if i >= 50
-      name = r.first + " " + r.last
-      value = (r.first + "_" + r.last).toLowerCase().replace(/[ -]/g, "_")
-      $("#players").append "<div class=\"player\"><input type=\"checkbox\" name=\"player\" value=\"" + value + "\"><span>" + name + "</span></div>"
-      $('#players').append('<br/>') if i % 5 is 4
-
-
-    fromYear = 2003
-    fromYearParam = getQueryVar("fromYear")
-    fromYear = parseInt(fromYearParam)  if fromYearParam
-    initFromYear()
-    $("[name=fromYear]").val fromYear
-
-    toYear = getYear()
-    toYearParam = getQueryVar("toYear")
-    toYear = parseInt(toYearParam)  if toYearParam
-    initToYear fromYear
-    $("[name=toYear]").val toYear
-
-    players = req.params.players.split(',')
-    $.each players, (i, p) ->
-      $("[value=" + p + "]").click()
+    for player in players
+      $("[value=" + player + "]").attr('checked', 'checked')
 
     loadAndShowRankHistory players, fromYear, toYear

    ['#players']
  ]

window.loadAndShowRankHistory = (players, fromYear, toYear) ->
  if players.length is 0
    alert "No player is chosen!"
    return

FIRST_YEAR = 2003
THIS_YEAR  = new Date().getYear() + 1900
YEARS      = [FIRST_YEAR..THIS_YEAR]

T.def 'rank-history', (fromYear, toYear) ->
  [
    [ 'h2'
      'ATP rank history'
      [ 'span.generated-note'
        ' (generated at '
        [ 'span.generated-at' ]
        ' )'
      ]
    ]
    ['#rank-history', ['svg']]
    [ '#timespan'
      'Time span: '
      [ 'select'
        name: 'fromYear'
        #change: ->
        #  fromYear = $(this).val()
        #  toYear   = $("[name = toYear]").val()
        #  $("[name=toYear]").html ""
        #  year = parseInt(fromYear)

        #  while year <= THIS_YEAR
        #    $("[name=toYear]").append "<option>" + year + "</option>"
        #    year++

        #  $("[name=toYear]").val (if toYear then toYear else THIS_YEAR)

        renderComplete: (el) -> $(el).val(fromYear)

        for year in YEARS
          ['option', year]
      ]
      ' - '
      [ 'select'
        name: 'toYear'
        renderComplete: (el) -> $(el).val(toYear)

        for year in YEARS
          ['option', year]
      ]
      [ 'button.update', 
        click: ->
          players = []
          $("[name=player]:checked").each -> players.push(@value)

          if players.length is 0
            alert "No player is selected."
          else
            window.location.hash = "#/rank-history/#{players.join(',')}?fromYear=#{$("[name=fromYear]").val()}&toYear=#{$("[name=toYear]").val()}"

        'Update graph'
      ]
