;(function($, window, document, undefined) {

var Scenery = {
  
  _registry: {},
  _currentSceneId: null,
  
  create: function(id, base, obj) {
    var cls;
    if (!obj) {
      obj = base;
      base = this.Scene;
    }
    cls = $.Class(base, obj);
    this._registry[id] = new cls(id);
    return this;
  },
  
  get: function(id, base) {
    var scene = this._registry[id];
    if (scene)
      return scene;
    if (base)
      this.create(id, base, {});
    else
      this.create(id, {});
    return this._registry[id];
  },
  
  getCurrent: function() {
    return this._registry[this._currentSceneId];
  },
  
  swap: function(scene) {
    var currentScene = this._registry[this._currentSceneId];;
    if (currentScene && (currentScene.id === scene.id))
      return this;
    currentScene && currentScene.destroy();
    this._currentSceneId = scene.id;
    return this;
  }
};

Scenery.Scene = $.Class({
  
  id: null,
  
  __init__: function(id) {
    this.id = id;
    this._data = null;
  },
  
  setData: function(data) {
    this._data = data;
  },
  
  getData: function() {
    return this._data;
  },
  
  // for subclasses
  
  init: function() {},
  
  destroy: function() {},
  
  enter: function(yield) {
    if (yield)
      yield();
  },
  
  exit: function(yield) {
    if (yield)
      yield();
  }
});

$.Scenery = window.Scenery = Scenery;

})(jQuery, this, this.document);
