 FileDialog = require("./file-dialog.coffee")
 
 fileLoadedBus = new Bacon.Bus()
-renameBus = new Bacon.Bus()
-
 initialApplication = if localStorage.application
     JSON.parse(localStorage.application)
   else

$ = window.$ = require("jquery")
Bacon = window.Bacon = require("baconjs")
_ = require("lodash")
$.fn.asEventStream = Bacon.$.asEventStream
examples = require("./examples.coffee")

fileLoadedBus.plug(fileDialog.fileLoadedE)
fileLoadedBus.plug(menubar.itemClickE("file-new").map(examples.empty))


nameModE = fileLoadedBus.map((app) -> (-> {name: app.name}))
  .merge(menubar.itemClickE("file-rename").map(-> ({name}, newName) ->
     promptNewName(name).map((newName) -> {name: newName, rename: true, oldName: name})
   ))

nameChangesP = nameModE.flatScan(initialApplication, (oldName, f) -> f(oldName))
nameP = nameChangesP.map(".name")

renameE = nameChangesP.changes().filter(".rename").map(({name, oldName}) -> {newName: name, oldName})

nameP.onValue (name) ->
  $("#menu-file > .title").text('Project "' + name + '"')
