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

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

nameP = fileLoadedBus.map(".name")
  .merge(renameBus.map(".newName"))
  .scan(initialApplication.name, (oldName, newName) -> newName)

renameBus.plug(menubar.itemClickE("file-rename")
  .map nameP
  .map((oldName) ->
    newName = prompt("Enter new name", oldName) || oldName
    { oldName, newName })
  .filter((({oldName, newName}) -> oldName != newName))
)

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

$ = window.$ = require("jquery")
Bacon = window.Bacon = require("baconjs")
Bacon.Observable :: flatScan = (seed, f) ->
  acc = seed
  @flatMapLatest((newValue) ->
    acc = f(acc, newValue)
  )
  .toProperty(seed)
_ = require("lodash")
$.fn.asEventStream = Bacon.$.asEventStream
examples = require("./examples.coffee")
