Index: /trunk/include/category_cats.inc.php
===================================================================
--- /trunk/include/category_cats.inc.php	(revision 18461)
+++ /trunk/include/category_cats.inc.php	(revision 18462)
@@ -28,11 +28,4 @@
  */
 
-$selection = array_slice(
-  $page['categories'],
-  $page['starta'],
-  $conf['nb_categories_page']
-  );
-
-$selection = trigger_event('loc_index_categories_selection', $selection);
 
 // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
@@ -49,6 +42,22 @@
     INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc 
     ON id = cat_id 
-    AND user_id = '.$user['id'].'
-  WHERE c.id IN('.implode(',', $selection).')';
+    AND user_id = '.$user['id'];
+  
+if ('recent_cats' == $page['section'])
+{
+  $query.= '
+  WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']);
+}
+else
+{
+  $query.= '
+  WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
+}
+
+$query.= '
+      '.get_sql_condition_FandF(
+        array('visible_categories' => 'id'),
+        'AND'
+        );
   
 if ('recent_cats' != $page['section'])
@@ -61,5 +70,16 @@
 ;';
 
-$result = pwg_query($query);
+$categories_sql = hash_from_query($query, 'id');
+
+$page['total_categories'] = count($categories_sql);
+
+$categories_sql = array_slice(
+  array_values($categories_sql),
+  $page['startcat'],
+  $conf['nb_categories_page']
+  );
+
+$categories_sql = trigger_event('loc_index_categories_selection', $categories_sql);
+
 $categories = array();
 $category_ids = array();
@@ -67,5 +87,5 @@
 $user_representative_updates_for = array();
 
-while ($row = pwg_db_fetch_assoc($result))
+foreach ($categories_sql as $row)
 {
   $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last'];
@@ -375,5 +395,21 @@
 
   $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails');
-}
+  
+  // navigation bar
+  $page['cats_navigation_bar'] = array();
+  if ($page['total_categories'] > $conf['nb_categories_page'])
+  {
+    $page['cats_navigation_bar'] = create_navigation_bar(
+      duplicate_index_url(array(), array('startcat')),
+      $page['total_categories'],
+      $page['startcat'],
+      $conf['nb_categories_page'],
+      true, 'startcat'
+      );
+  }
+
+  $template->assign('cats_navbar', $page['cats_navigation_bar'] );
+}
+
 pwg_debug('end include/category_cats.inc.php');
 ?>
Index: /trunk/include/functions_url.inc.php
===================================================================
--- /trunk/include/functions_url.inc.php	(revision 18461)
+++ /trunk/include/functions_url.inc.php	(revision 18462)
@@ -459,5 +459,5 @@
             and strpos($tokens[$current_token], 'posted-')!==0
             and strpos($tokens[$next_token], 'start-')!==0
-            and strpos($tokens[$next_token], 'starta-')!==0
+            and strpos($tokens[$next_token], 'startcat-')!==0
             and $tokens[$current_token] != 'flat')
         {
@@ -653,7 +653,7 @@
       $page['start'] = $matches[1];
     }
-    elseif (preg_match('/^starta-(\d+)/', $tokens[$i], $matches))
-    {
-      $page['starta'] = $matches[1];
+    elseif (preg_match('/^startcat-(\d+)/', $tokens[$i], $matches))
+    {
+      $page['startcat'] = $matches[1];
     }
     $i++;
Index: /trunk/include/section_init.inc.php
===================================================================
--- /trunk/include/section_init.inc.php	(revision 18461)
+++ /trunk/include/section_init.inc.php	(revision 18462)
@@ -42,9 +42,7 @@
 //   );
 
-// exemple of dynamic nb_categories_page (%2 for nice display)
-// $conf['nb_categories_page'] = 2*round($user['nb_image_page']/4);
-
-$page['items'] = $page['categories'] = array();
-$page['start'] = $page['starta'] = 0;
+
+$page['items'] = array();
+$page['start'] = $page['startcat'] = 0;
 
 // some ISPs set PATH_INFO to empty string or to SCRIPT_FILENAME while in the
@@ -255,5 +253,5 @@
   if
     (
-      $page['starta'] == 0 and
+      $page['startcat'] == 0 and
       (!isset($page['chronology_field'])) and
       (
@@ -318,43 +316,4 @@
     $page['items'] = array_from_query($query, 'image_id');
   } //otherwise the calendar will requery all subitems
-  
-  // GET CATEGORIES LIST
-  if ( script_basename()=='index'
-    and 0==$page['start']
-    and !isset($page['flat'])
-    and !isset($page['chronology_field'])
-    and ('recent_cats'==$page['section'] or 'categories'==$page['section'])
-    and (!isset($page['category']['count_categories']) or $page['category']['count_categories']>0 )
-  )
-  {
-    $query = '
-SELECT c.id
-  FROM '.CATEGORIES_TABLE.' c
-    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc 
-    ON id = cat_id 
-    AND user_id = '.$user['id'];
-
-    if ('recent_cats' == $page['section'])
-    {
-      $query.= '
-      WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']);
-    }
-    else
-    {
-      $query.= '
-      WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
-    }
-
-    $query.= '
-        '.get_sql_condition_FandF(
-          array('visible_categories' => 'id'),
-          'AND'
-          );
-
-    $query.= '
-    ;';
-
-    $page['categories'] = array_from_query($query, 'id');
-  }
 }
 // special sections
Index: /trunk/index.php
===================================================================
--- /trunk/index.php	(revision 18461)
+++ /trunk/index.php	(revision 18462)
@@ -84,18 +84,5 @@
 }
 
-$page['cats_navigation_bar'] = array();
-if (count($page['categories']) > $conf['nb_categories_page'])
-{
-  $page['cats_navigation_bar'] = create_navigation_bar(
-    duplicate_index_url(array(), array('starta')),
-    count($page['categories']),
-    $page['starta'],
-    $conf['nb_categories_page'],
-    true, 'starta'
-    );
-}
-
-$template->assign( 'thumb_navbar', $page['navigation_bar'] );
-$template->assign( 'cats_navbar', $page['cats_navigation_bar'] );
+$template->assign('thumb_navbar', $page['navigation_bar'] );
 
 // caddie filling :-)
@@ -299,8 +286,14 @@
 
 //------------------------------------------------------ main part : thumbnails
-if ( !empty($page['categories']) )
+if ( 0==$page['start']
+  and !isset($page['flat'])
+  and !isset($page['chronology_field'])
+  and ('recent_cats'==$page['section'] or 'categories'==$page['section'])
+  and (!isset($page['category']['count_categories']) or $page['category']['count_categories']>0 )
+)
 {
   include(PHPWG_ROOT_PATH.'include/category_cats.inc.php');
 }
+
 if ( !empty($page['items']) )
 {
