 
   timeoutCallback = ->
     spinner.stop()
+    saveCodeToCache()
     reloadCode (err) -> if !err then executeCode()
 
   spinner.spin($("#north")[0]) 


  requestAnimationFrame(handleAnimation)

### Main ###

$(document).ready ->

  setupLayout()
  setupButtonHandlers()

  for id in ["modelEditor", "assetsEditor", "actionsEditor", "layoutEditor"]
    editor = setupEditor(id)
    editors[id] = editor

  loadedCode = false
  try 
    loadedCode = loadCodeFromCache()

  if not loadedCode
    for [id, url] in [["modelEditor", "optics/model.json"], ["assetsEditor", "optics/assets.json"], ["actionsEditor", "optics/actions.js"], ["layoutEditor", "optics/layout.json"]]
      loadIntoEditor(editors[id], url)

  for id in ["modelEditor", "assetsEditor", "actionsEditor", "layoutEditor"]
    editors[id].getSession().on "change", -> notifyCodeChange()

  # TODO: find another way to include global data
  globals.editors = editors

