$ ->
  localAddr = 'http://127.0.0.1:8080'
  local = ['/1', '/2', '/3', '/4', '/5', '/6', '/7', '/8', '/9', '/10']

  remote = [
    'http://stackoverflow.com/questions/101268/hidden-features-of-python',
    'http://stackoverflow.com/questions/81584/what-ide-to-use-for-python',
    'http://stackoverflow.com/questions/228181/the-zen-of-python',
    'http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained',
    'http://stackoverflow.com/questions/2146383/https-connection-python',
    'http://stackoverflow.com/questions/68645/static-class-variables-in-python',
    'http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python',
    'http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python'
  ]

  urlArea = $ '<textarea rows="11" cols="90"></textarea>'
  titleArea = $ '<textarea rows="11" cols="90"></textarea>'

  slide = $ '.slide.msg-demo'
  slide.append(urlArea)
       .append('<div><button id="local">Local</button><button id="remote">Remote</button><div>')
       .append('<div><button id="fetch">Fetch</button><div>')
       .append($('<div><div>').append(titleArea))

  socket = null

  connect = (f) ->
    console.log 'Connecting...'

    socket = io.connect(location.origin,
      'force new connection': true
    )
    socket.on "connect", ->
      f(socket)

    socket.on "message", (data) ->
      log data

    socket.on 'disconnect', ->
      console.log('Disconnected')

  log = (msg) ->
    titleArea.val(titleArea.val() + msg + '\n')

  $('#local').click ->
    urlArea.val((localAddr + l for l in local).join('\n'))

  $('#remote').click ->
    urlArea.val(remote.join('\n'))

  $('#fetch').click ->
    if socket and socket.socket.connected
      console.log 'Disconnecting...'
      socket.disconnect()
    connect () ->
      console.log 'Fetching...'

      titleArea.val('');

      urls = (u for u in urlArea.val().split('\n') when u)

      console.log(urls)

      for u in urls
        socket.send u

