Index: /extensions/rv_autocomplete/context_suggest.php
===================================================================
--- /extensions/rv_autocomplete/context_suggest.php	(revision 22246)
+++ /extensions/rv_autocomplete/context_suggest.php	(revision 22246)
@@ -0,0 +1,101 @@
+<?php
+
+define('PHPWG_ROOT_PATH','../../');
+include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
+
+if (!defined('RVAC_ID')) {
+  set_status_header(501);
+  exit;
+}
+
+include_once( 'functions.inc.php' );
+
+$page['root_path'] = get_absolute_root_url(false);
+
+$context = @$_REQUEST['context'];
+
+if (empty($context) || strncmp($context, '$t/',3))
+	bad_request("context");
+
+$tag_ids = array();
+$tag_url_names = array();
+foreach( explode('/', $context) as $tag_token)
+{
+	if ( $conf['tag_url_style'] != 'tag' and preg_match('/^(\d+)(?:-(.*)|)$/', $tag_token, $matches) )
+	{
+		$tag_ids[] = $matches[1];
+	}
+	else
+	{
+		$tag_url_names[] =  $tag_token;
+	}
+}
+
+$context_tags = find_tags($tag_ids, $tag_url_names);
+if ( empty($context_tags) )
+{
+	bad_request('no context content');
+}
+
+$context_tag_ids = array_map( function($tag) { return $tag['id']; }, $context_tags);
+$context_image_ids = get_image_ids_for_tags($context_tag_ids);
+
+$tags = get_common_tags($context_image_ids, -1, $context_tag_ids);
+usort($tags, function($a,$b) {
+	$d = -$a['counter'] + $b['counter'];
+	if (!$d) $d = tag_alpha_compare($a,$b);
+	return $d;
+});
+
+$suggestions = array();
+foreach($tags as $tag)
+{
+	if ($tag['counter']==count($context_image_ids))
+		continue;
+	$label = $tag['name'];
+
+	$url = $context.'/';
+	switch ( $conf['tag_url_style'] )
+	{
+		case 'id':
+			$url.= $tag['id'];
+			break;
+		case 'tag':
+			if (!is_numeric($tag['url_name']) )
+			{
+				$url.= $tag['url_name'];
+				break;
+			}
+		default:
+			$url.= $tag['id'].'-'.$tag['url_name'];
+	}
+
+	$suggestion = array(
+    'label' => $label,
+    'value' => $url,
+		'w' => intval($tag['counter'])
+	);
+	
+	$q = rvac_normalize($label);
+  if ($q != strtolower($label))
+    $suggestion['q'] = $q;
+
+	$suggestions[] = $suggestion;
+}
+
+
+
+$ds = str_replace(
+  array('\\/', '"q":', '"label":', '"value":', '"w":'),
+  array('/', 'q:', 'label:', 'value:', 'w:'),
+  json_encode($suggestions));
+
+$js = 'RVAC.mergeData({dataSource:'.$ds.',
+ctxq:'.json_encode((string)@$_REQUEST['ctxq']).',
+ctxl:'.json_encode((string)@$_REQUEST['ctxl']).',
+ctxw:'.count($context_image_ids).'
+})';
+
+echo $js;
+
+?>
Index: /extensions/rv_autocomplete/functions.inc.php
===================================================================
--- /extensions/rv_autocomplete/functions.inc.php	(revision 22245)
+++ /extensions/rv_autocomplete/functions.inc.php	(revision 22246)
@@ -57,4 +57,6 @@
   if ($q != strtolower($l))
     $ret['q'] = $q;
+	if ($item['counter'] > 0)
+		$ret['w'] = $item['counter'];
   return $ret;
 }
Index: /extensions/rv_autocomplete/main.inc.php
===================================================================
--- /extensions/rv_autocomplete/main.inc.php	(revision 22245)
+++ /extensions/rv_autocomplete/main.inc.php	(revision 22246)
@@ -45,5 +45,6 @@
   global $template;
 
-  $core_src = get_root_url().'plugins/'.RVAC_ID.'/res/suggest-core.js';
+	$plug_root = get_root_url().'plugins/'.RVAC_ID.'/';
+  $core_src = $plug_root.'res/suggest-core.js';
 
   $data_src = rvac_get_data_file();
@@ -51,7 +52,7 @@
     $data_src = get_root_url().$data_src;
   else
-    $data_src = get_root_url().'plugins/'.RVAC_ID.'/suggestions.php';
+    $data_src = $plug_root.'suggestions.php';
 
-  $fs = 'var RVAC={};
+  $fs = 'var RVAC={root:"'.$plug_root.'"};
 $("#qsearchInput").one("focus", function() {
 var s;
Index: /extensions/rv_autocomplete/res/suggest-core.js
===================================================================
--- /extensions/rv_autocomplete/res/suggest-core.js	(revision 22245)
+++ /extensions/rv_autocomplete/res/suggest-core.js	(revision 22246)
@@ -93,4 +93,37 @@
 }
 
+RVAC.requestedContexts = {};
+RVAC.requestingContexts = 0;
+
+RVAC.mergeData = function(data) {
+	RVAC.requestingContexts--;
+	var ctxq = data.ctxq + " "
+		, ctxl = data.ctxl + " ";
+	data = data.dataSource;
+	$.each(data, function(i,item) {
+		if (item.q)
+			item.q = ctxq + item.q;
+		else
+			item.q = ctxq + item.label.toLowerCase();
+		item.label = ctxl + item.label;
+		item.ctx = 1;
+	});
+
+	var ids=0, inew=0;
+
+	while (inew < data.length) {
+		while (RVAC.dataSource[ids].w > data[inew].w)
+			ids++;
+		RVAC.dataSource.splice(ids, 0, data[inew]);
+		RVAC.dataSourceAltLangIndex++;
+		inew++;
+	}
+	
+	var inp = $("#qsearchInput");
+	if (data.length && inp.is(":focus")) {
+		inp.autocomplete("search");
+	}
+};
+
 RVAC.start = function() {
 var $input = $("#qsearchInput");
@@ -124,5 +157,5 @@
 			window.location = (url + (/\?/.test(url) ?  "&" : "?") + "acs=" + term);
 		}
-			
+
 		return false;
 	},
@@ -145,4 +178,6 @@
 			var item = RVAC.dataSource[i];
 			if ( (item._matchIdx = item.q.search(matcher)) > -1 ) {
+				if (item.ctx && item._matchIdx>0)
+					continue; // context searches only at beginning
 				if (item.value !== -1) {
 					if (found.hasOwnProperty(item.value))
@@ -157,5 +192,5 @@
 
 		if (res.length > RVAC.suggestions) {//try filter
-			// compute number of matched "words" or "expression"
+			// compute number of matched "words" or "expressions"
 			found={};
 			var foundWords=0, word;
@@ -202,4 +237,29 @@
 		}
 
+		var that = this;
+		if (!RVAC.requestingContexts && this.options.rv.q.length > 1) {
+			var eligible = $.grep( res, function(item) {
+				if (item._matchIdx > 0
+					|| item.w <= 25
+					|| item.value === -1
+					|| item.value[0]!="$" || item.value[1]!="t")
+					return 0;
+				if (RVAC.requestedContexts.hasOwnProperty(item.value))
+					return 0;
+				if (item.q.length - that.options.rv.q.length > 3)
+					return 0;
+				return 1;
+			} );
+
+			if (eligible.length) {
+				item = eligible[0];
+				RVAC.requestedContexts[item.value] = 1;
+				RVAC.requestingContexts++;
+				var s=document.createElement("script");s.type="text/javascript";s.async=true;
+				s.src=RVAC.root+"context_suggest.php?context="+encodeURIComponent(item.value)+"&ctxq="+encodeURIComponent(item.q)+"&ctxl="+encodeURIComponent(item.label);
+				document.body.appendChild(s);
+			}
+		}
+
 		response( res );
 	}
Index: /extensions/rv_autocomplete/suggestions.php
===================================================================
--- /extensions/rv_autocomplete/suggestions.php	(revision 22245)
+++ /extensions/rv_autocomplete/suggestions.php	(revision 22246)
@@ -23,6 +23,6 @@
 
 $ds = str_replace(
-  array('\\/', '"q":', '"label":', '"value":'),
-  array('/', 'q:', 'label:', 'value:'),
+  array('\\/', '"q":', '"label":', '"value":', '"w":'),
+  array('/', 'q:', 'label:', 'value:', 'w:'),
   json_encode($data['src']));
 $js = '$.extend(RVAC, {
