define([
    'text!items.html!strip'
  , 'css!items'
  ],
  function (source) {
    var response = {};

    response.data = source;
    response.mockup = function (source, callback_mockup) {
      var markup, spec,
        gadget = RenderJs.getSelfGadget(),
        id = gadget.dom.attr('id').split("__").splice(-1)[0],
        config = gadget.state[0][id]._config,
        module = config.datasource.module,
        screen = window.App.settings.screen_format,
        size = screen === "small" ? "small" : "medium",
        shortcut = size.substring(0, 1);

      spec = {};

      spec.pointer = config.datasource.pointer;
      spec.method = spec.pointer ? undefined : config.datasource.method || "allDocs";
      spec.storage = spec.pointer ? undefined : config.datasource.storage || "items";
      spec.options = spec.pointer ? undefined : {
        "limit": config.datasource.limit || [0, 24],
        "sort_on": config.datasource.sort || [["_id", "descending"]],
        "select_list": config.datasource.fields || undefined
      };

      spec.callback = function(err, response) {
        var item, i, str = "", price;

        for (i = 0; i < response.total_rows; i += 1) {
          item = response.rows[i].value;

          if (item.price === item.original_price) {
            price = item.price + "€";
          } else {
            price = "<span class='rebate'>" + item.price + "€</span> (" +
              item.original_price + "€)";
          }

          str += "<li data-icon='false'>" +
            "<a href='details.html?product=" + item._id +
            "' title='" + item.brand + ", " + item.title + "'>" +
            "<span class='img_container_items'>" +
            "<img alt='" + item.title + "' src='" +
            item.media + item.dimensions[0].vars[0].media.img[0][shortcut] +
            size + "' /></span><span class='description_items'>" +
            item.brand + " " + item.title + " " + price + "</span></a></li>";
        }

        // add to data
        markup = source.replace(/<!-- items -->/g, str);
        // this is not response.callback(!!)
        callback_mockup(markup);
      }

      // query for items
      window.App[module].switchboard(spec);
    };

    response.callback = function (self) {
      if (window.App === undefined) {
        def = new $.Deferred;

        def.done(function(gadget) {
          // prefix instance-ids, pass JSON
          window.App.renderGadgets(gadget);
        });

        window.fallbackLoader.deferreds.push(def);
        window.fallbackLoader.args.push(self);
      } else {
        // prefix instance-ids, pass JSON
        window.App.renderGadgets(self);
      }
    };

  // return response object
  return response;
  }
);
