function AjaxError(jqXHR, textStatus, errorThrown) {
  this.name = 'AjaxError';
  this.message = textStatus;
  this.jqXHR = jqXHR;
  this.errorThrown = errorThrown;
}
AjaxError.prototype = new Error();
AjaxError.prototype.constructor = AjaxError;

(function($) {
  function decorateAsJQuery(promise) {
    promise.done = function(fn) {
      return decorateAsJQuery(promise.then(fn));
    };
    promise.fail = function(fn) {
      return decorateAsJQuery(promise.then(null, fn));
    };
    promise.complete = function(fn) {
      return decorateAsJQuery(promise.then(fn, fn));
    };
    return promise;
  }
  var jqAjax = $.ajax;
  $.ajax = function ajax() {
    var args = Array.prototype.slice.call(arguments);
    var jqPromise = jqAjax.apply(this, args);
    var promise = new Promise(function(resolve, reject) {
      jqPromise.then(
        function(data, textStatus, jqXHR) {
          resolve(data);
        },
        function(jqXHR, textStatus, errorThrown) {
          reject(new AjaxError(jqXHR, textStatus, errorThrown));
        }
      );
    });
    return decorateAsJQuery(promise);
  };
})(jQuery);

var define = function(require, module) {
  if (module)
    return function(_, __, m) {
      m.exports = require(module);
    };
  else
    return function(_, __, m) {
      m.exports = require;
    };
};

window.soundManager = require('soundmanager2/script/soundmanager2-nodebug').soundManager;

window.require.define({
  'editor/listeners/embed-listener': define(
    require('../editor/listeners/embed-listener')
  ),
});

function defineAll(r, prefix) {
  var nameRegex = /^\.\/(.+)\.(pug|coffee)$/;
  r.keys().forEach(function(key) {
    var moduleName = nameRegex.exec(key)[1];
    if (prefix) moduleName = prefix + moduleName;
    window.require.define(moduleName, define(r, key));
  });
}
defineAll(require.context('../', true, /\.pug$/));
defineAll(
  require.context('../core/controls', true, /\.coffee$/),
  'core/controls/'
);
defineAll(
  require.context('../editor/content/service', true, /\.coffee$/),
  'editor/content/service/'
);
defineAll(
  require.context('../browser/controller', true, /\.coffee$/),
  'browser/controller/'
);
