function Regulator(name, data) {
    var self = this;

    self.name = name;
    self.summary = data.summary;
    self.description = data.description;
    self.methods = data.methods;
    self.references = data.references;
}

function DoRiNAViewModel() {
    var self = this;
    self.clades = ko.observableArray([]);
    self.genomes = ko.observableArray([]);
    self.assemblies = ko.observableArray([]);

    self.chosenClade = ko.observable();
    self.chosenGenome = ko.observable();
    self.chosenAssembly = ko.observable();

    self.rbps = ko.observableArray([]);
    self.selected_rbps = ko.observableArray([]);

    self.mirnas = ko.observableArray([]);
    self.selected_mirnas = ko.observableArray([]);

    self.results = ko.observableArray([]);

    self.get_clades = function() {
        $.getJSON("clades", function(data) {
            self.clades.removeAll();
            for (var i in data.clades) {
                self.clades.push(data.clades[i]);
            }
        });
    };

    self.get_genomes = function(clade) {
        $.getJSON("genomes/" + clade, function(data) {
            self.genomes.removeAll();
            for (var i in data.genomes) {
                self.genomes.push(data.genomes[i]);
            }
        });
    };

    self.get_assemblies = function(clade, genome) {
        $.getJSON("assemblies/" + clade + "/" + genome, function(data) {
            self.assemblies.removeAll();
            for (var i in data.assemblies) {
                self.assemblies.push(data.assemblies[i]);
            }
        });
    };

    self.show_simple_search = function() {
        var search_path = "regulators/";
        search_path += self.chosenClade() + "/";
        search_path += self.chosenGenome() + "/";
        search_path += self.chosenAssembly();

        $.getJSON(search_path, function(data) {
            self.rbps.removeAll();
            self.mirnas.removeAll();
            for (var i in data['RBP']) {
                self.rbps.push(new Regulator(i, data['RBP'][i]));
            }
            for (var i in data['miRNA']) {
                self.mirnas.push(new Regulator(i, data['miRNA'][i]));
            }
            $('#chooseDatabase').collapse('hide');
            $('#search').collapse('show');
        });
    };

    self.run_simple_search = function() {
        var regulators = self.selected_rbps() + self.selected_mirnas();
        var search_data = {
            regulators: regulators,
            assembly: self.chosenAssembly()
        };
        $('#search').collapse('hide');
        $('#results').collapse('show');
        $.post('search', search_data, function(data) {
            console.log(data.results);
            self.results.removeAll();
            for (var i in data.results) {
                self.results.push(data.results[i]);
            }
        });
    };

}

function SetViewModel(view_model) {
        $(document).data('view_model', view_model);
}

function GetViewModel() {
        return $(document).data('view_model');
}


@@ -746,32 +746,155 @@
 ableArray(%5B%5D);%0A%0A
+    self.more_results = ko.observable(false);%0A    self.offset = ko.observable(0);%0A    self.pending = ko.observable(true);%0A%0A
     self.get_cla
@@ -2409,23 +2409,16 @@
 elf.run_
-simple_
 search =
@@ -2419,32 +2419,41 @@
 arch = function(
+keep_data
 ) %7B%0A        var 
@@ -2469,30 +2469,73 @@
 s = 
-self.selected_rbps() +
+%5B%5D;%0A        var rbps = self.selected_rbps();%0A        var mirnas =
  sel
@@ -2551,32 +2551,33 @@
 d_mirnas();%0A
+%0A
         
 var search_d
@@ -2568,117 +2568,346 @@
     
-var search_data = %7B%0A            regulators: regulators,%0A            assembly: self.chosenAssembly()%0A        %7D
+for (var i in rbps) %7B%0A            regulators.push(rbps%5Bi%5D.name);%0A        %7D%0A        for (var i in mirnas) %7B%0A            regulators.push(mirnas%5Bi%5D.name);%0A        %7D%0A%0A        var search_data = %7B%0A            set_a: regulators,%0A            assembly: self.chosenAssembly(),%0A            offset: self.offset()%0A        %7D;%0A        self.pending(true)
 ;%0A  
@@ -2975,32 +2975,106 @@
 llapse('show');%0A
+        if (!keep_data) %7B%0A            self.results.removeAll();%0A        %7D%0A
         $.post('
@@ -3128,32 +3128,26 @@
     
-console.log(data.results
+self.pending(false
 );%0A 
@@ -3162,34 +3162,46 @@
    self.
+more_
 results
-.removeAll(
+(data.more_results
 );%0A     
@@ -3303,33 +3303,496 @@
       %7D%0A        
-%7D
+    if (data.next_offset) %7B%0A                self.offset(data.next_offset);%0A            %7D%0A        %7D);%0A    %7D;%0A%0A    self.run_simple_search = function() %7B%0A        self.run_search(false);%0A    %7D;%0A%0A    self.load_more_results = function() %7B%0A        self.run_search(true);%0A    %7D;%0A%0A    self.new_search = function() %7B%0A        $('#search').collapse('hide');%0A        $('#results').collapse('hide');%0A        $('#chooseDatabase').collapse('show');%0A        self.more_results(false
 );%0A    %7D;%0A%0A%7D%0A%0Afu

Make the UI work for searches