/*globals window, document, RSVP, rJS, Handlebars, console*/
/*jslint indent: 2, maxlen: 80*/
(function (window, document, RSVP, rJS, Handlebars, console) {
  "use strict";

  function callCribSWGadget(gadget, method, param_list) {
    var called = false;
    return new RSVP.Queue()
      .push(function () {
      return gadget.getDeclaredGadget("crib_sw_gadget");
    })
      .push(function (crib_sw_gadget) {
      return crib_sw_gadget[method].apply(crib_sw_gadget, param_list);
    })
      .push(function (result) {
      return  result;
    }, function (error) {
      throw error;
    });
  }


  rJS(window)
    .ready(function (g) {
    g.props = {};
    return g.getElement()
      .push(function (element) {
      g.props.element = element;
    });
  })
    .allowPublicAcquisition("crib_sw_allDocs", function (param_list) {
    return callCribSWGadget(this, "allDocs", param_list);
  })
    .allowPublicAcquisition("crib_sw_remove", function (param_list) {
    return callCribSWGadget(this, "remove", param_list);
  })
    .allowPublicAcquisition("crib_sw_put", function (param_list) {
    return callCribSWGadget(this, "put", param_list);
  })
    .allowPublicAcquisition("crib_sw_get", function (param_list) {
    return callCribSWGadget(this, "get", param_list);
  })
    .declareMethod('render', function (options) {
    var promise_list = [],
        gadget = this;
    gadget.props.options = options;
    // Add promise for list Cache content
    // promise to use dav storage for jIO
    return RSVP.Queue()
      .push(function () {
      return gadget.getDeclaredGadget('crib_ide_gadget');
    })
      .push(function (crib_ide_gadget) {
      return crib_ide_gadget.render();
    });
  })


}(window, document, RSVP, rJS, console));