YUI.add("presenters:machines", function (Y) {

  Y.MachinesPresenter = Y.Base.create("MachinesPresenter", Y.Presenter, [], {

    initializer: function (options) {
      this.N = options.containerNode;

      this.N.all(".machine .edit").on("click", this.showEdit, this);

      var machineImages = this.N.all(".machine_images");
      if (machineImages.size() > 0) {
        machineImages.each(function (node) {
          new Y.PhotoViewer({containerNode: node});
        });
      }
    },

    showEdit: function (ev) {
      var machine = new Y.Machine({});
      this._modal = new Y.ModalPresenter({tmpl: 'machines/edit', 
                                          data: {machine: machine}});
      this._modal.show();
    },

    showNew: function (ev) {
      var machine = new Y.Machine(m);
      this._modal = new Y.ModalPresenter({tmpl: 'machines/new',
                                          data: {machine: machine}});
      this._modal.show();
      this._modal.on("show", function () {
        Y.one("#save_machine").on("click", P.handleSave);
      });

      machine.on("saved", P.handleSaved);
    },

    handleSave: function (ev, machine) {
      machine.save();
    },

    handleSaved: function () {
      if (this._modal) {
        this._modal.hide();
        this._modal.on("hidden", function () {
          this._modal = undefined;
        });
      }
    }

  }, {ATTRS: {}});

}, "1.0.0", {requires: ["node", "base", "presenters:presenter", "presenters:modal", "models:machine"]});

