Fixed mirroring - made collections work
diff --git a/Changes b/Changes
index b0efd68..2f8972c 100755
--- a/Changes
+++ b/Changes
@@ -1,5 +1,7 @@
 0.16 2015-06-18
         - Update to new KoralQuery API.
+	  WARNING! This version is no longer compatible
+	  with old responses of Krill.
 	- Introduced new notifications engine.
 
 0.15 2015-05-21
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 4e37a9b..0e49091 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -5,6 +5,7 @@
   'tutorial',
   'lib/domReady',
   'hint/array',
+  'vc/array',
   'lib/alertify',
   'api',
   'mailToChiffre',
@@ -16,6 +17,7 @@
 	     tutClass,
 	     domReady,
 	     hintArray,
+	     vcArray,
 	     alertifyClass) {
 
   // Set hint array for hint helper
@@ -62,12 +64,17 @@
       vcname = document.createElement('span');
       vcname.setAttribute('id', 'vc-choose');
 
+      var currentVC = loc.VC_allCorpora;
+      if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
+	currentVC = loc.VC_oneCollection;
+      };
+
       vcname.appendChild(
 	document.createTextNode(
-	  document.getElementById('vc-name').value ||
-	  (KorAP.currentVC !== undefined) ? loc.VC_oneCollection : loc.VC_allCorpora
+	  document.getElementById('vc-name').value || currentVC
 	)
       );
+
       input.parentNode.insertBefore(vcname, input);
     };
 
@@ -133,7 +140,7 @@
 	// The vc is not visible
 	else {
 	  if (vc === undefined)
-	    vc = _getCurrentVC(vcClass);
+	    vc = _getCurrentVC(vcClass, vcArray);
 	  view.appendChild(vc.element());
 	  this.classList.add('active');
 	};
@@ -164,18 +171,20 @@
     obj.tutorial.initDocLinks(document);
 
     // There is a currentQuery
-    if (KorAP.currentQuery !== undefined) {
+    /*
+    if (KorAP.koralQuery !== undefined) {
       var kq = document.createElement('div');
       kq.setAttribute('id', 'koralquery');
 
       var kqInner = document.createElement('div');
       kq.appendChild(kqInner);
-      kqInner.innerHTML = JSON.stringify(KorAP.currentQuery, null, '  ');
+      kqInner.innerHTML = JSON.stringify(KorAP.koralQuery, null, '  ');
       hljs.highlightBlock(kqInner);
 
       var sb = document.getElementById('search');
       sb.insertBefore(kq, sb.firstChild);
     };
+    */
 
     /**
      * Add VC creation on submission.
@@ -184,7 +193,7 @@
     if (form !== undefined) {
       form.addEventListener('submit', function (e) {
 	if (vc === undefined)
-	  vc = _getCurrentVC(vcClass);
+	  vc = _getCurrentVC(vcClass, vcArray);
 
 	if (vc !== undefined)
 	  input.value = vc.toQuery();
@@ -205,18 +214,11 @@
 });
 
 // Render Virtual collection
-// TODO:: Use currentQuery!!!
-function _getCurrentVC (vcClass) {
-  var vc = vcClass.create([
-    ['title', 'string'],
-    ['subTitle', 'string'],
-    ['pubDate', 'date'],
-    ['author', 'string'],
-    ['corpusID', 'string']
-  ]);
-  if (KorAP.currentVC !== undefined)
-    vc.fromJson(KorAP.currentVC);
-
+function _getCurrentVC (vcClass, vcArray) {
+  var vc = vcClass.create(vcArray);
+  if (KorAP.koralQuery !== undefined && KorAP.koralQuery["collection"]) {
+    vc.fromJson(KorAP.koralQuery["collection"]);
+  };
   return vc;
 };
 
diff --git a/dev/js/src/loc/de.js b/dev/js/src/loc/de.js
index a9eb389..fce90a1 100644
--- a/dev/js/src/loc/de.js
+++ b/dev/js/src/loc/de.js
@@ -5,10 +5,13 @@
   // EMPTY, DELETE
 
   // Virtual collection:
+  /*
   loc.VC_subTitle = 'Untertitel';
   loc.VC_title = 'Titel';
   loc.VC_pubDate = 'Veröffentlichungsdatum';
   loc.VC_pubPlace = 'Veröffentlichungsort';
+  */
+
   loc.VC_allCorpora = 'allen Korpora';
   loc.VC_oneCollection = 'einer Kollektion';
 
diff --git a/dev/js/src/vc/array.js b/dev/js/src/vc/array.js
new file mode 100644
index 0000000..6473a48
--- /dev/null
+++ b/dev/js/src/vc/array.js
@@ -0,0 +1,36 @@
+define(function () {
+  return [
+    ['author', 'string'],
+    ['biblEditionStatement', 'string'],
+    ['corpusAuthor', 'string'],
+    ['corpusEditor', 'string'],
+    ['corpusSigle', 'string'],
+    ['corpusSubTitle', 'string'],
+    ['corpusTitle', 'string'],
+    ['creationDate', 'date'],
+    ['docAuthor', 'string'],
+    ['docEditor', 'string'],
+    ['docSigle', 'string'],
+    ['docSubTitle', 'string'],
+    ['docTitle', 'string'],
+    ['editor', 'string'],
+    ['fileEditionStatement', 'string'],
+    ['keywords', 'string'],
+    ['language', 'string'],
+    ['license', 'string'],
+    ['pages', 'string'],
+    ['pubDate', 'date'],
+    ['publisher', 'string'],
+    ['pubPlace', 'date'],
+    ['reference', 'string'],
+    ['subTitle', 'string'],
+    ['textClass', 'string'],
+    ['textColumn', 'string'],
+    ['textDomain', 'string'],
+    ['textSigle', 'string'],
+    ['textType', 'string'],
+    ['textTypeArt', 'string'],
+    ['textTypeRef', 'string'],
+    ['title', 'string']
+  ]
+});
diff --git a/dev/scss/main/koralquery.scss b/dev/scss/main/koralquery.scss
index d569d77..1aec746 100644
--- a/dev/scss/main/koralquery.scss
+++ b/dev/scss/main/koralquery.scss
@@ -8,6 +8,8 @@
   white-space: pre;
   background-color: $dark-orange;
   > div {
+    overflow-x: auto;
+    font-size: 85%;
     background-color: $nearly-white;
     margin: {
       top: 2pt;
diff --git a/kalamar.conf b/kalamar.conf
index 18c7d0a..1bdf667 100644
--- a/kalamar.conf
+++ b/kalamar.conf
@@ -5,8 +5,8 @@
   },
   Search => {
     engine => 'Kalamar::API',
-    api => 'http://10.0.10.13:7070/api/v0.1/'
-#    api => 'http://localhost:9999/api/v0.1/'
+#    api => 'http://10.0.10.13:7070/api/v0.1/'
+    api => 'http://localhost:9999/api/v0.1/'
   },
   CHI => {
     default => {
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index 7369598..0fd7d3d 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -4,7 +4,7 @@
 use Mojo::JSON 'decode_json';
 
 # Minor version - may be patched from package.json
-our $VERSION = '0.15';
+our $VERSION = '0.16';
 
 # TODO: The FAQ-Page has a contact form for new questions
 # TODO: Embed query serialization
diff --git a/lib/Kalamar/API.pm b/lib/Kalamar/API.pm
index 4ae79e3..74dc1c4 100644
--- a/lib/Kalamar/API.pm
+++ b/lib/Kalamar/API.pm
@@ -28,6 +28,7 @@
 			 api_response
 			 benchmark
 			 query_jsonld
+			 collection
 			 collection_jsonld/]);
   $index_class->attr(no_cache => 0);
 };
@@ -55,6 +56,8 @@
 
   # Cache based on URL
   $index->_api_cache('total-' . $url->to_string);
+  # TODO: Make this user dependent for collections!!!!
+
   my %param = @_;
 
   # Set context based on parameter
@@ -290,9 +293,6 @@
   # Response was fine
   if (my $res = $tx->success) {
 
-    # Set api response for debugging
-    $index->api_response($res->body); # if $c->kalamar_test_port;
-
     # Json failure
     my $json;
     unless ($json = $res->json) {
@@ -300,6 +300,9 @@
       return;
     };
 
+    # Set api response as jsonld
+    $index->api_response($json);
+
     # expected response for matches
     if ($type eq 'matches') {
       $self->_process_response_matches($index, $json);
@@ -332,8 +335,11 @@
 sub _process_response_matches {
   my ($self, $index, $json) = @_;
 
+  # Process meta
+  my $meta = $json->{meta};
+
   # Reformat benchmark counter
-  my $benchmark = $json->{benchmark};
+  my $benchmark = $meta->{benchmark};
   if ($benchmark && $benchmark =~ s/\s+(m)?s$//) {
     $benchmark = sprintf("%.2f", $benchmark) . ($1 ? $1 : '') . 's';
   };
@@ -342,77 +348,23 @@
   $index->benchmark($benchmark);
 
   # Set time exceeded
-  if ($json->{timeExceeded} && $json->{timeExceeded} eq Mojo::JSON::true) {
+  if ($meta->{timeExceeded} && $meta->{timeExceeded} eq Mojo::JSON::true) {
     $index->time_exceeded(1);
   };
 
   # Set result values
-  $index->items_per_page($json->{itemsPerPage});
+  $index->items_per_page($meta->{itemsPerPage});
+
 
   # Bouncing query
-  if ($json->{query}) {
-    $index->query_jsonld($json->{query});
-  }
+#  if ($json->{query}) {
+#    $index->query_jsonld($json->{query});
+#  };
+
   # Legacy
-  elsif ($json->{request}->{query}) {
-    $index->query_jsonld($json->{request}->{query});
-  };
-
-  # Temporary:
-  my $collection_query = {
-    '@type' => "koral:docGroup",
-    "operation" => "operation:or",
-    "operands" => [
-      {
-	'@type' => "koral:docGroup",
-	"operation" => "operation:and",
-	"operands" => [
-	  {
-	    '@type' => "koral:doc",
-	    "key" => "title",
-	    "match" => "match:eq",
-	    "value" => "Der Birnbaum",
-	    "type" => "type:string"
-	  },
-	  {
-	    '@type' => "koral:doc",
-	    "key" => "pubPlace",
-	    "match" => "match:eq",
-	    "value" => "Mannheim",
-	    "type" => "type:string"
-	  },
-	  {
-	    '@type' => "koral:docGroup",
-	    "operation" => "operation:or",
-	    "operands" => [
-	      {
-		'@type' => "koral:doc",
-		"key" => "subTitle",
-		"match" => "match:eq",
-		"value" => "Aufzucht oder Pflege",
-		"type" => "type:string"
-	      },
-	      {
-		'@type' => "koral:doc",
-		"key" => "subTitle",
-		"match" => "match:eq",
-		"value" => "Gedichte",
-		"type" => "type:string"
-	      }
-	    ]
-	  }
-	]
-      },
-      {
-	'@type' => "koral:doc",
-	"key" => "pubDate",
-	"match" => "match:geq",
-	"value" => "2015-03-05",
-	"type" => "type:date"
-      }
-    ]
-  };
-
+  # elsif ($json->{request}->{query}) {
+  #   $index->query_jsonld($json->{request}->{query});
+  # };
 
   # Bouncing collection query
   if ($json->{collection}) {
@@ -420,24 +372,21 @@
   }
 
   # Legacy
-  elsif ($json->{request}->{collection}) {
-    $index->collection_jsonld($json->{request}->{collection});
-  };
-
-  # Temp
-  $index->collection_jsonld($collection_query);
+  # elsif ($json->{request}->{collection}) {
+  #  $index->collection_jsonld($json->{request}->{collection});
+  # };
 
   $index->results(_map_matches($json->{matches}));
 
   # Total results not set by stash
   if ($index->total_results == -1) {
 
-    if ($json->{totalResults} && $json->{totalResults} > -1) {
+    if ($meta->{totalResults} && $meta->{totalResults} > -1) {
       my $c = $index->controller;
 
       $c->app->log->debug('Cache total result');
-      $c->chi->set($index->_api_cache => $json->{totalResults}, '120min');
-      $index->total_results($json->{totalResults});
+      $c->chi->set($index->_api_cache => $meta->{totalResults}, '120min');
+      $index->total_results($meta->{totalResults});
     };
   };
 };
@@ -483,7 +432,10 @@
 
   # Check json response error message
   if ($json) {
+
+    # Legacy, but still in use by Kustvakt
     if ($json->{error}) {
+
       # Temp
       $json->{error} =~ s/;\s+null$//;
       $c->notify(error => $json->{error});
@@ -581,6 +533,9 @@
   # Set cutoff from param
   $index->cutoff(delete $param{cutoff});
 
+  # Set collection from param
+  $index->collection(delete $param{collection});
+
   # Set query language
   $index->query_language(delete $param{query_language} // 'poliqarp');
 
@@ -593,9 +548,9 @@
   $query{ql}     = $index->query_language;
   $query{page}   = $index->start_page if $index->start_page;
   $query{count}  = $index->items_per_page if $index->items_per_page;
+  $query{cq}     = $index->collection if $index->collection;
   $query{cutoff} = 'true' if $index->cutoff;
 
-  # Todo: support corpus and collection
   # Create query url
   my $url = Mojo::URL->new($index->api);
   $url->query(\%query);
@@ -726,3 +681,58 @@
 L<BSD-2 License|https://raw.githubusercontent.com/KorAP/Kalamar/master/LICENSE>.
 
 =cut
+
+  # Temporary:
+  my $collection_query = {
+    '@type' => "koral:docGroup",
+    "operation" => "operation:or",
+    "operands" => [
+      {
+	'@type' => "koral:docGroup",
+	"operation" => "operation:and",
+	"operands" => [
+	  {
+	    '@type' => "koral:doc",
+	    "key" => "title",
+	    "match" => "match:eq",
+	    "value" => "Der Birnbaum",
+	    "type" => "type:string"
+	  },
+	  {
+	    '@type' => "koral:doc",
+	    "key" => "pubPlace",
+	    "match" => "match:eq",
+	    "value" => "Mannheim",
+	    "type" => "type:string"
+	  },
+	  {
+	    '@type' => "koral:docGroup",
+	    "operation" => "operation:or",
+	    "operands" => [
+	      {
+		'@type' => "koral:doc",
+		"key" => "subTitle",
+		"match" => "match:eq",
+		"value" => "Aufzucht oder Pflege",
+		"type" => "type:string"
+	      },
+	      {
+		'@type' => "koral:doc",
+		"key" => "subTitle",
+		"match" => "match:eq",
+		"value" => "Gedichte",
+		"type" => "type:string"
+	      }
+	    ]
+	  }
+	]
+      },
+      {
+	'@type' => "koral:doc",
+	"key" => "pubDate",
+	"match" => "match:geq",
+	"value" => "2015-03-05",
+	"type" => "type:date"
+      }
+    ]
+  };
diff --git a/lib/Kalamar/Controller/Search.pm b/lib/Kalamar/Controller/Search.pm
index 8445c9f..a29d9d3 100644
--- a/lib/Kalamar/Controller/Search.pm
+++ b/lib/Kalamar/Controller/Search.pm
@@ -17,6 +17,7 @@
   my %param = (
     query_language => scalar $c->param('ql'),
     query => $query,
+    collection => scalar $c->param('collection')
   );
 
   # May be not relevant
diff --git a/templates/layouts/main.html.ep b/templates/layouts/main.html.ep
index 9050f77..7f6f811 100644
--- a/templates/layouts/main.html.ep
+++ b/templates/layouts/main.html.ep
@@ -20,13 +20,13 @@
     <div id="kalamar-bg"></div>
 
     % unless ($embedded) {
-    %= include 'partial/header'
+      %= include 'partial/header'
     % }
 
     % my $aside = content_for('sidebar');
     <aside tabindex="0" class="<% unless (length($aside) > 0) { %>off<% } elsif (stash('sidebar_active')) { %>active<% } %>">
       <div>
-    %= $aside
+      %= $aside
       </div>
     </aside>
 
@@ -40,6 +40,6 @@
       <%= doc_link_to 'V '. $Kalamar::VERSION, 'korap', 'kalamar' %>
     </footer>
     % };
-%= notifications 'Kalamar::Plugin::Notifications'
+    %= notifications 'Kalamar::Plugin::Notifications'
   </body>
 </html>
diff --git a/templates/query.html.ep b/templates/query.html.ep
index a25a5e5..b3e52fd 100644
--- a/templates/query.html.ep
+++ b/templates/query.html.ep
@@ -1,9 +1,9 @@
-% use Mojo::JSON;
+% use Mojo::JSON 'encode_json';
 
-%= javascript begin
- KorAP.currentQuery = <%== Mojo::JSON::encode_json(search->query_jsonld) %>;
-% end
-
-<pre>
-%== search->api_response
-</pre>
+% if (search->api_response) {
+%=  javascript begin
+%   my $kq_hash = search->api_response;
+%   $kq_hash->{matches} = ["..."];
+  KorAP.koralQuery = <%= b(encode_json($kq_hash))->decode %>;
+%   end
+% };
diff --git a/templates/search.html.ep b/templates/search.html.ep
index 47ec86a..f34f00f 100644
--- a/templates/search.html.ep
+++ b/templates/search.html.ep
@@ -1,5 +1,4 @@
 % layout 'main', title => loc('searchtitle', q => search->query, ql => search->query_language);
-% use Mojo::JSON 'encode_json';
 
 <div class="resultinfo">
   <div id="pagination"><%= pagination(search->start_page, search->total_pages, url_with->query(['p' => '{page}'])) =%></div>
@@ -17,12 +16,7 @@
 % };
 </div>
 
-% if (search->collection_jsonld) {
-%=  javascript begin
-KorAP.currentVC = <%== encode_json search->collection_jsonld %>;
-%   end
-% };
-
+%= include 'query'
 
 <div id="search">
 % if (search->total_results != 0 && search->results->size) {
@@ -35,5 +29,3 @@
 <p id="no-results"><%== loc 'noMatches', q => search->query %></p>
 % }
 </div>
-
-%= include 'query'