define([
    'underscore', 'backbone',
    'views/document', 'views/pagination',
    'models/document', 'collections/documents'
], function (_, Backbone,
             DocumentView, PaginationView,
             Document, Documents) {
    var parseQueryString;

    parseQueryString = function (queryString) {
        var params = {};
        if (queryString) {
            _.each(
                _.map(decodeURI(queryString).split(/&/g), function (el, i) {
                    var aux = el.split('='), o = {};
                    if (aux.length >= 1) {
                        var val = undefined;
                        if (aux.length == 2)
                            val = aux[1];
                        o[aux[0]] = val;
                    }
                    return o;
                }),
                function (o) {
                    _.extend(params, o);
                }
            );
        }
        return params;
    };

    var DocumentSearchView = Backbone.View.extend({
        tagName: "ul",
        className: "col-sm-offset-2 col-sm-8",
        id: "document-list",

        initialize: function (options) {
            options = options || {};
            // TODO: CHECK fetch url
            if (typeof options.queryString === 'undefined') {
                throw new Error('no query string defined');
            }
            this.queryString = options.queryString;
            this.collection = new Documents();
        },

        render: function () {
            var that = this;
            var fetchUrl = '/api/documents/?' + this.queryString;
            $.get(fetchUrl)
                .done(function (data) {
                    var params;

                    that.$el.append(data['total'] + ' results found.');
                    _(data['rows']).each(function (obj) {
                        var m = new Document(obj);
                        that.$el.append(
                            new DocumentView({model: m, id: "document-" + m.id}).render().el);
                        that.collection.add(m);
                    });

                    params = parseQueryString(that.queryString);
                    if (data['total'] > params['limit']) {
                        that.$el.append(new PaginationView({
                            total: data['total'],
                            offset: params['offset'],
                            limit: params['limit'],
                            searchString: params['search']
                        }).render().el);
                    }
                });
            return this;
        }
    });

    return DocumentSearchView;
});

@@ -1568,32 +1568,75 @@
 is.queryString;%0A
+            var start = performance.now();%0A
             $.ge
@@ -1647,16 +1647,16 @@
 tchUrl)%0A
-
         
@@ -1719,16 +1719,119 @@
 params;%0A
+                    var end =   performance.now();%0A                    var diff = (end - start) / 1000;
 %0A       
@@ -1889,14 +1889,40 @@
 lts 
-found.
+(' + diff.toFixed(3) + 'seconds)
 ');%0A

add timing for search