Index: Dev/branches/rest-dojo-ui/client/config/db.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/config/db.js	(revision 361)
+++ Dev/branches/rest-dojo-ui/client/config/db.js	(revision 362)
@@ -20,5 +20,5 @@
     parser.parse();
 
-    query("#log").forEach(function(n){ logNode = n; });
+    query("#log").forEach(function(n){logNode = n;});
     usernameInput = registry.byId('username');
     passwordInput = registry.byId('password');
@@ -31,7 +31,8 @@
 
     log("Give CouchDB admin username & password and click 'Configure' to start.\nIf the database already exists, rft_admin password will suffice.",true);
-
     
     function configure(){
+        var docs;
+
         log("Configuring CouchDB for RFT:",true);
 
@@ -40,5 +41,13 @@
         var reset = resetInput.get('value');
 
-        var docs = json.fromJson(docsJson);
+        log("Downloading most recent configuration.");
+        xhr('GET',{
+            url: 'docs.json',
+            handleAs: 'json',
+            sync: true
+        },true)
+        .then(function(result){
+            docs = result;
+        });
 
         function req(method,url,body) {
@@ -47,8 +56,5 @@
                 contentType: 'application/json',
                 handleAs: 'json',
-                sync: true,
-                error: function(err) {
-                    log("ERROR: "+err);
-                }
+                sync: true
             };
             if ( !body || lang.isObject(body) ) {
Index: Dev/branches/rest-dojo-ui/client/config/docs.json
===================================================================
--- Dev/branches/rest-dojo-ui/client/config/docs.json	(revision 362)
+++ Dev/branches/rest-dojo-ui/client/config/docs.json	(revision 362)
@@ -0,0 +1,81 @@
+{
+    "_users/rft_admin": {
+        "__configAction": "ignore",
+        "_id": "org.couchdb.user:rft_admin",
+        "name": "rft_admin",
+        "password": "Welkom01",
+        "roles": [ "rft_admin" ],
+        "type": "user"
+    },
+    "rft/_security": {
+        "__configAction": "ignore",
+        "admins" : {
+            "names" : [],
+            "roles" : ["rft_admin"]
+        },
+        "readers" : {
+            "names" : [],
+            "roles" : ["rft_user"]
+        }
+    },
+    "rft/q1": {
+        "__configAction": "update",
+        "_id": "q1",
+        "title": "How long have you worked here?",
+        "categories": [
+            "Professional background"
+        ],
+        "topic": "Work experience",
+        "type": "Question"
+    },
+    "rft/q2": {
+        "__configAction": "update",
+        "_id": "q2",
+        "title": "How many years have you been employed here?",
+        "categories": [
+            "Respondent personals",
+            "Professional background"
+        ],
+        "topic": "Work experience",
+        "type": "Question"
+    },
+    "rft/q3": {
+        "__configAction": "update",
+        "_id": "q3",
+        "title": "Have you worked here longer than 10 years?",
+        "categories": [
+            "Respondent personals"
+        ],
+        "topic": "Work experience",
+        "type": "Question"
+    },
+    "rft/q4": {
+        "__configAction": "update",
+        "_id": "q4",
+        "title": "Rate your experience at your current employer from 1 to 10.",
+        "categories": [
+            "Respondent personals",
+            "Professional background"
+        ],
+        "topic": "Work enjoyment",
+        "type": "Question"
+    },
+    "rft/_design/default": {
+        "__configAction": "replace",
+        "_id": "_design/default",
+        "language": "javascript",
+        "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { if (oldDoc && oldDoc.published) { throw({forbidden: 'Published documents cannot be modified.'}); } }",
+        "views": {
+            "by_type": {
+                "map": "function(doc){ emit(doc.type, doc); }"
+            },
+            "unpublished": {
+                "map": "function(doc){ if ( doc.type === 'Survey' && !doc.published ) { emit(doc._id, doc); } }"
+            },
+            "questions": {
+                "map": "function(doc){ if ( doc.type === 'Question' ) { for (var i = 0; i < doc.categories.length; i++) { emit([doc.categories[i],doc.topic],1); } } }",
+                "reduce": "function(keys,values) { return sum(values); }"
+            }
+        }
+    }
+}
Index: Dev/branches/rest-dojo-ui/client/rft/pages/survey.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/pages/survey.js	(revision 361)
+++ Dev/branches/rest-dojo-ui/client/rft/pages/survey.js	(revision 362)
@@ -3,4 +3,5 @@
     'dojo/_base/event',
     'dojo/_base/Deferred',
+    'dojo/on',
     'rft/ui/Selector',
     'rft/ui/SurveyListView',
@@ -11,5 +12,5 @@
     'dojo/store/Cache',
     'dojo/store/Memory' ],
-    function(declare,lang,event,Deferred,Selector,SurveyListView,ContentPane,store,_Page,content,Cache,Memory){
+    function(declare,lang,event,Deferred,on,Selector,SurveyListView,ContentPane,store,_Page,content,Cache,Memory){
         return declare('rft.pages.survey',[_Page],{
             object: null,
@@ -26,7 +27,7 @@
                         return Deferred.when( obj.creator && store.dereference(obj.creator) );
                     }));
-                    this._createListView();
-                    this._createQuestionBrowser();
                     this._setupButtons();
+                    this._setupQuestionBrowser();
+                    this._setupListView();
                 } else {
                     throw "No valid uid or survey passed!";
@@ -40,52 +41,71 @@
                 this.btnPreview.on("click", lang.hitch(this, this._goToPreview));
             },
-            _createQuestionBrowser: function() {
-                store.query('_design/default/_view/by_type', {key:'Question'})
-                .forEach(function(question){
-                    this._insertQuestion(question);
-                },this);
+            _setupQuestionBrowser: function() {
+                this.tabList.watch("selectedChildWidget",lang.hitch(this,function(name,oldTab,newTab){
+                    this._fillCategoryTab(newTab.__category);
+                }));
+                store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:1})
+                .forEach(lang.hitch(this,function(value,key){
+                    this._createCategoryTab(key[0],value);
+                }));
             },
-            _insertQuestion: function(question) {
-                for (var c in question.categories) {
-                    var cat = question.categories[c];
-                    if (this._dataMap[cat] === undefined) {
-                        this._dataMap[cat] = {
-                            widget: this._createCategoryTab(cat),
-                            topics: {}
-                        }
-                    }
-                    this._insertIntoCategory(question,this._dataMap[cat]);
+            _createCategoryTab: function(category,count) {
+                if (this._dataMap[category] === undefined) {
+                    var categoryTab = new ContentPane({
+                        __category: category,
+                        title: category+" ("+count+")"
+                    });
+                    categoryTab.startup();
+                    this._dataMap[category] = {
+                        _widget: categoryTab
+                    };
+                    this.tabList.addChild(categoryTab);
                 }
             },
-            _createCategoryTab: function(category) {
-                var categoryTab = new ContentPane({
-                    title: category
-                });
-                categoryTab.startup();
-                this.tabList.addChild(categoryTab);
-                return categoryTab;
+            _fillCategoryTab: function(category) {
+                var categoryMap = this._dataMap[category];
+                if (!categoryMap._filled) {
+                    categoryMap._filled = true;
+                    store.query('_design/default/_view/questions', {reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]})
+                    .forEach(lang.hitch(this,function(value,key){
+                        this._createTopicSelector(key[1],category,value);
+                    }));
+                }
             },
-            _insertIntoCategory: function(question, categoryMap) {
-                if (categoryMap[question.topic] === undefined) {
+            _createTopicSelector: function(topic,category,count){
+                var categoryMap = this._dataMap[category];
+                if (categoryMap[topic] === undefined) {
                     var w = new Selector({
-                        title: question.topic
-                    }).placeAt(categoryMap.widget.containerNode);
+                        __category: category,
+                        __topic: topic,
+                        title: topic+" ("+count+")"
+                    }).placeAt(categoryMap._widget.containerNode);
                     w.startup();
+                    categoryMap[topic] = {
+                        _widget: w
+                    };
+                    this._fillTopicSelector(topic,category);
                     w.on('include',lang.hitch(this,this.includeQuestion));
-                    categoryMap[question.topic] = {
-                        widget: w
-                    };
                 }
-                categoryMap[question.topic].widget.addItem(question);
+            },
+            _fillTopicSelector: function(topic,category) {
+                var categoryMap = this._dataMap[category];
+                var topicMap = categoryMap[topic];
+                if (!topicMap._filled) {
+                    store.query('_design/default/_view/questions', {reduce:false,include_docs:true,key:[category,topic]})
+                    .forEach(lang.hitch(this,function(value,key){
+                        topicMap._widget.addItem(value);
+                    }));
+                }
             },
             /* ListView code */
-            _createListView: function() {
+            includeQuestion: function(question) {
+                this.listView.insertItem(question);
+            },
+            _setupListView: function() {
                 this.listView = new SurveyListView({
                     controller: this
                 }).placeAt(this.surveyListViewNode);
                 this.listView.startup();
-            },
-            includeQuestion: function(question) {
-                this.listView.insertItem(question);
             }
         });
Index: Dev/branches/rest-dojo-ui/client/rft/store.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/store.js	(revision 361)
+++ Dev/branches/rest-dojo-ui/client/rft/store.js	(revision 362)
@@ -97,5 +97,4 @@
                 if ( query === undefined ) {
                     query = '_all_docs';
-                    queryOpts.include_docs = true;
                 }
 
@@ -126,11 +125,23 @@
 
                 // Custom options
-                if (options.key) {
+                if (options.key !== undefined) {
                     queryOpts.key = options.key;
-                } else if (options.keys) {
+                } else if (options.keys !== undefined) {
                     queryOpts.keys = options.keys;
-                } else if (options.startkey || options.endkey) {
+                } else if (options.startkey !== undefined || options.endkey !== undefined) {
                     queryOpts.startkey = options.startkey;
                     queryOpts.endkey = options.endkey;
+                }
+                if (options.include_docs !== undefined) {
+                    queryOpts.include_docs = options.include_docs;
+                }
+                if (options.reduce !== undefined) {
+                    queryOpts.reduce = options.reduce;
+                }
+                if (options.group !== undefined) {
+                    queryOpts.group = options.group;
+                    if (options.group_level !== undefined) {
+                        queryOpts.group_level = options.group_level;
+                    }
                 }
 
@@ -152,13 +163,37 @@
                         dfd.resolve(
                             array.map(result.rows,
-                                      function(result){ return result.value; }));
+                                      function(result){
+                                          return [result.key, options.include_docs === true ? result.doc : result.value];
+                                      }));
                     }
                 },function(err){
                     dfd.reject(err);
                 });
-		        return QueryResults(dfd.promise);
+		        return CouchResults(dfd.promise);
             }
         });
 
+        function CouchResults(results) {
+            if (!results) {
+                return results;
+            }
+
+	        if(results.then){
+		        results = lang.delegate(results);
+	        }
+
+            if (!results.forEach) {
+                results.forEach = function(callback) {
+                    return Deferred.when(results, function(results) {
+                        array.forEach(results, function(result) {
+                            callback(result[1],result[0]);
+                        });
+                    });
+                }
+            }
+
+            return results;
+        }
+
         return new CouchStore({target: 'data/rft/'});
 
Index: Dev/branches/rest-dojo-ui/client/rft/ui/Selector.js
===================================================================
--- Dev/branches/rest-dojo-ui/client/rft/ui/Selector.js	(revision 361)
+++ Dev/branches/rest-dojo-ui/client/rft/ui/Selector.js	(revision 362)
@@ -64,5 +64,5 @@
                     actions: {
                         "Toggle dropdown" : {
-                            callback: lang.hitch(this, this._onToggleDropdown),
+                            callback: lang.hitch(this, this.onToggle),
                             properties: {
                                 blockButton: true,
@@ -75,4 +75,5 @@
                 },this.selectedItemNode);
                 this._selectorLine.startup();
+                this._selectorLine.on('click',lang.hitch(this, this.onToggle));
 
                 fx.wipeOut({
@@ -82,5 +83,5 @@
             _onSelect: function(item, widget) {
                 this._selectedItem = item;
-                this._onToggleDropdown();
+                this.onToggle();
                 this._selectorLine.set("title", item.title);
                 baseArray.forEach(this.optionsNode.childNodes, function(node){
@@ -101,5 +102,5 @@
                 }
             },
-            _onToggleDropdown: function(evt) {
+            onToggle: function(evt) {
                 if (this._folded) {
                     var downArrowIcon = dojo.query(".rftBlockButton .rftIconHalfArrowDown", this._selectorLine.buttonsNode)[0];
