(function (window, rJS, RSVP) {
    "use strict";
    rJS(window)
    .declareAcquiredMethod("notifySubmitting", "notifySubmitting")
    .declareAcquiredMethod("notifySubmitted", "notifySubmitted")
    .declareAcquiredMethod("setSetting", "setSetting")
    .declareAcquiredMethod("jio_get", "jio_get")
    .declareAcquiredMethod("jio_post", "jio_post")

    .ready(function () {
      var gadget = this;
      return this.getDeclaredGadget("torrent")
      .push(function (torrent) {
        return gadget.changeState({
          torrent_gadget: torrent
        });
      });
    })

    .declareMethod("preRenderDocument", function (parent_options) {
        var gadget = this;
        /*return gadget.jio_get(parent_options.jio_key)
          .push(function (parent_document) {
              return parent_document;
          });*/
    })

    .declareMethod("handleSubmit", function (content_dict, parent_options) {
        var return_submit_dict,
        gadget = this;

        if (!content_dict.magnet_uri) {
          return;
        }
        return new RSVP.Queue()
        .push(function(){
            return_submit_dict = {
                notify: {
                    message: "Download index",
                    status: "success"
                },
                redirect: {
                    command: "change",
                    options: {page : "mynij_torrent",
                              magnet_uri: content_dict.magnet_uri
                             }
                }
            };
            return return_submit_dict;
        });
    });

}(window, rJS, RSVP));