$(function () {
  var compiler = require('squiffy/compiler.js');
  var shell = require('shell');

  var compile = function (input) {
    if (input.zip) {
      // TODO: Generate zip files using local compiler.js

      var url = 'http://squiffy.textadventures.co.uk/zip';
      // Using XMLHttpRequest here as jQuery doesn't support blob downloads
      var xhr = new XMLHttpRequest();
      xhr.open('POST', url, true);
      xhr.responseType = 'blob';
       
      xhr.onload = function(e) {
        if (this.status == 200) {
          input.success(this.response);
        }
        else {
          input.fail(this.response);
        }
      };
       
      xhr.send(input.data);
      return;
    }

    var js = compiler.getJs(input.data);
    if (js.indexOf('Failed') === 0) {
        input.fail(js);
        return;
    }
    input.success(js);
  };

  $('#inputfile').on('change', function () {
      var objectUrl = window.URL.createObjectURL(this.files[0]);
      if (!objectUrl) return;
      $.get(objectUrl, function (data) {
          $('#squiffy-editor').squiffyEditor('load', data);
      });
  });

  var init = function (data) {
    $('#squiffy-editor').squiffyEditor({
      data: data,
      compile: compile,
      open: function () {
        $('#inputfile').click();
      },
      save: function (title) {
        bootbox.alert('Save not implemented in demo');
        $('#squiffy-editor').squiffyEditor('setInfo', 'Not saved');
      },
      autoSave: function (title) {
        console.log('Auto save');
      },
      preview: function () {
        bootbox.alert('Preview not implemented in demo');
      },
      publish: function () {
        bootbox.alert('Publish not implemented in demo');
      },
      storageKey: 'squiffy',
      updateTitle: function (title) {
        if (process.platform === 'darwin') {
          document.title = title;
        }
        else{
          document.title = title + ' - Squiffy';
        }
      },
    });
  };

  var saved = localStorage['squiffy'];
  if (saved) {
    init(localStorage['squiffy']);
  }
  else {
    $.get('example.squiffy', init);
  }

  $('#squiffy-editor').on('click', 'a.external-link, #output-container a[href]', function (e) {
    shell.openExternal($(this).attr('href'));
    e.preventDefault();
  });
});
@@ -89,16 +89,69 @@
 shell');
+%0A  var path = require('path');%0A  var filename = null;
 %0A%0A  var 
@@ -907,16 +907,182 @@
 ;%0A  %7D;%0A%0A
+  var updateTitle = function () %7B%0A    if (!filename) %7B%0A      document.title = 'New file';%0A    %7D%0A    else%7B%0A      document.title = path.basename(filename);%0A    %7D%0A  %7D;%0A%0A
   $('#in
@@ -1114,24 +1114,61 @@
 nction () %7B%0A
+      filename = this.files%5B0%5D.path;%0A
       var ob
@@ -1287,24 +1287,49 @@
 on (data) %7B%0A
+          updateTitle();%0A
           $(
@@ -2070,185 +2070,11 @@
 on (
-title) %7B%0A        if (process.platform === 'darwin') %7B%0A          document.title = title;%0A        %7D%0A        else%7B%0A          document.title = title + ' - Squiffy';%0A        %7D%0A      
+) %7B
 %7D,%0A 

Window title is filename