       position:
         from: 'bottom'
         align: 'right'
-
   else
     $.growl
       title: 'Whoops!'

      position:
        from: 'bottom'
        align: 'right'

  return


# load history

files = History.get()

currentPage = ->
    return parseInt($('#cont-pagination').attr('data-current')) | 0

pageCount = ->
    return parseInt((History.get().length + 9) / 10)

pickPagination = (page) ->
    $('#cont-pagination').attr('data-current', page)
    $('#cont-pagination button').each ->
        found = false
        if not isNaN($(this).attr('data-id'))
            if parseInt($(this).attr('data-id')) is page
                found = true

        $(this).prop('disabled', found)

pickFilePage = (page) ->
    if page is "next"
        page = Math.min(currentPage() + 1, pageCount()-1)

    if page is "prev"
        page = Math.max(0, currentPage() - 1)

    page = parseInt(page)

    $('#cont-file-list').empty()
    for file in History.get()[page * 10...(page + 1) * 10]
        addFile(file)

    pickPagination(page)

initFilePages = ->
    $cont = $('#cont-pagination')
    $cont.empty()

    $cont.append('<button data-id="prev" type="button" class="btn btn-default"><i class="fa fa-arrow-circle-left"></i></button>')

    for i in [0...pageCount()]
        $cont.append('<button data-id="' + i + '" type="button" class="btn btn-default">' + (i + 1) + '</button>')

    $cont.append('<button data-id="next" type="button" class="btn btn-default"><i class="fa fa-arrow-circle-right"></i></button>')

    $cont.find('button').on 'click', () ->
        pickFilePage($(this).attr('data-id'))
    pickFilePage(0)

initFilePages()


api = (resource) ->
  'http://node2.storj.io/api/' + resource

GIGABYTE = 1024 * 1024 * 1024

Cookies = {
  set: ((k, v, days) ->
    if days
      date = new Date()
      secs = days * 24 * 60 * 60 * 1000
      date.setTime(date.getTime() + secs)
      expires = '; expires=' + date.toGMTString() + '; max-age=' + secs
    else
      expires = ''

    document.cookie = k + '=' + v + expires + '; path=/'
    v
  ),

  get: ((k) ->
    cookies = document.cookie.split(';')
    for cookie in cookies
      cookie = cookie.trim()
      index = cookie.indexOf('=')
      [name, v] = [cookie.substring(0, index), cookie.substring(index + 1)]

      if name is k
        return v
    null
  ),

  kill: ((k) ->
    this.set(k, '', -11)
  )
}

LocalStorage = {
  set: ((k, v) ->
    localStorage.setItem(k, v)
    v
  ),

  get:  ((k) ->
    localStorage.getItem(k)
  ),

  kill: ((k) ->
    localStorage.removeItem(k)
  )
}

KeyValueStore = LocalStorage


History = {
  add: ((file) ->
    stuff = this.get()
    stuff.unshift(file)
    KeyValueStore.set('history', JSON.stringify(stuff))
  ),
  get: (-> JSON.parse(KeyValueStore.get('history')) or []),
  kill: (-> KeyValueStore.kill('history'))
}

AccessToken = {
