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

    .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,
        list = parent_options.doc.links,
        index_name = parent_options.doc.title;

        return gadget.getSetting("build_dict", {})
        .push(function(build_dict){
            build_dict[parent_options.action_options.jio_key] = {
              links: list,
              index_name: index_name,
              jio_key: parent_options.action_options.jio_key,
              id: Date.now()
            };
            return gadget.setSetting("build_dict", build_dict);
        })
        .push(function(){
            return_submit_dict = {
                notify: {
                    message: "Index " + index_name  + " will start building...",
                    status: "success"
                },
                redirect: {
                    command: "display",
                    options: {page : "ojs_local_controller"}
                }
            };
            return return_submit_dict;
        });
    });

}(window, rJS, RSVP));