diff -Naur program/include/rcube_imap.inc ../webmail-dev.alternative.ch/program/include/rcube_imap.inc
--- program/include/rcube_imap.inc	Sun Feb 26 22:15:26 2006
+++ ../webmail-dev.alternative.ch/program/include/rcube_imap.inc	Sun Feb 26 22:19:11 2006
@@ -498,7 +498,7 @@
       $begin = 0;
       $end = $max;
       }
-    else if ($this->sort_order=='DESC')
+    else if (!$this->get_capability('sort') && $this->sort_order=='DESC')
       {
       $begin = $max - $this->page_size - $start_msg;
       $end =   $max - $start_msg;
@@ -533,24 +533,24 @@
       if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
         {
 //console("$mailbox: ".join(',', $msg_index));
-        
+
+        if ($this->sort_order == 'DESC')
+          $msg_index = array_reverse($msg_index);
+
         $msgs = $msg_index[$begin];
         for ($i=$begin+1; $i < $end; $i++)
           {
-          //if ($this->sort_order == 'DESC')
-          //  $msgs = $msg_index[$i].','.$msgs;
-          //else
             $msgs = $msgs.','.$msg_index[$i];
           }
 
-        $sorted = TRUE;
+        $headers_sorted = TRUE;
         }
       else
         {
         $msgs = sprintf("%d:%d", $begin+1, $end);
-        $sorted = FALSE;
+        $headers_sorted = FALSE;
         }
-
+        
 
       // cache is dirty, sync it
       if ($this->caching_enabled && $cache_status==-1 && !$recursive)
@@ -581,8 +581,13 @@
 
 
     // if not already sorted
-    if (!$headers_sorted)
-      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
+//    if (!$headers_sorted)
+//      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
+
+
+      if (!$headers_sorted && $this->sort_order == 'DESC')
+        $a_msg_headers = array_reverse($a_msg_headers);
+      
 
     return array_values($a_msg_headers);
     }
@@ -633,8 +638,7 @@
     return $deleted_count;
     }
     
-
-
+  
   // return sorted array of message UIDs
   function message_index($mbox='', $sort_field=NULL, $sort_order=NULL)
     {
@@ -664,17 +668,13 @@
 
     // fetch complete message index
     $msg_count = $this->_messagecount($mailbox);
-    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field)))
+    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, '', TRUE)))
       {
-      $a_uids = iil_C_FetchUIDs($this->conn, $mailbox);
-
       if ($this->sort_order == 'DESC')
         $a_index = array_reverse($a_index);
 
-      $i = 0;
-      $this->cache[$key] = array();
-      foreach ($a_index as $index => $value)
-        $this->cache[$key][$i++] = $a_uids[$value];
+      $this->cache[$key] = $a_index;
+
       }
     else
       {
@@ -1355,14 +1355,14 @@
       $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
       $cache_uid = array_pop($cache_index);
       
-      // uids of highes message matches -> cache seems OK
+      // uids of highest message matches -> cache seems OK
       if ($cache_uid == $header->uid)
         return 1;
 
       // cache is dirty
       return -1;
       }
-    // if cache count differs less that 10% report as dirty
+    // if cache count differs less than 10% report as dirty
     else if (abs($msg_count - $cache_count) < $msg_count/10)
       return -1;
     else
@@ -1707,6 +1707,15 @@
     return array_merge($a_defaults, $a_out);
     }
 
+  function get_id($uid) 
+    {
+      return $this->_uid2id($uid);
+    }
+  
+  function get_uid($id)
+    {
+      return $this->_id2uid($id);
+    }
 
   function _uid2id($uid, $mbox=NULL)
     {
@@ -1717,6 +1726,14 @@
       $this->uid_id_map[$mbox][$uid] = iil_C_UID2ID($this->conn, $mbox, $uid);
 
     return $this->uid_id_map[$mbox][$uid];
+    }
+
+  function _id2uid($id, $mbox=NULL)
+    {
+    if (!$mbox)
+      $mbox = $this->mailbox;
+      
+    return iil_C_ID2UID($this->conn, $mbox, $id);
     }
 
 
diff -Naur program/lib/imap.inc ../webmail-dev.alternative.ch/program/lib/imap.inc
--- program/lib/imap.inc	Sun Feb 19 19:34:34 2006
+++ ../webmail-dev.alternative.ch/program/lib/imap.inc	Sun Feb 26 21:33:28 2006
@@ -625,7 +625,7 @@
 	return $time2;
 }
 
-function iil_C_Sort(&$conn, $mailbox, $field, $add=''){
+function iil_C_Sort(&$conn, $mailbox, $field, $add='', $is_uid=FALSE, $encoding='US-ASCII'){
 	/*  Do "SELECT" command */
 	if (!iil_C_Select($conn, $mailbox)) return false;
 	
@@ -635,8 +635,10 @@
 	
 	if (!$fields[$field]) return false;
 	
+	$is_uid = $is_uid ? 'UID ' : '';
+
 	$fp = $conn->fp;
-	$command = 's SORT ('.$field.') US-ASCII ALL '."$add\r\n";
+	$command = 's '. $is_uid .'SORT ('.$field.') '.$encoding.' ALL '."$add\r\n";
 	$line = $data = '';
 	
 	if (!fputs($fp, $command)) return false;
@@ -654,7 +656,7 @@
 	return $out;
 }
 
-function iil_C_FetchHeaderIndex(&$conn, $mailbox, $message_set, $index_field,$normalize=true){
+function iil_C_FetchHeaderIndex(&$conn, $mailbox, $message_set, $index_field, $normalize=true){
 	global $IMAP_USE_INTERNAL_DATE;
 	
 	$c=0;
@@ -1556,6 +1558,25 @@
 		}
 	}
 	return false;
+}
+
+function iil_C_ID2UID(&$conn, $folder, $id){
+	$fp = $conn->fp;
+	$result=-1;
+	if ($id > 0) {
+		if (iil_C_Select($conn, $folder)){
+			$key = "FUID";
+			if (fputs($fp, "$key FETCH $id (UID)\r\n")){
+				do{
+					$line=chop(iil_ReadLine($fp, 1024));
+					if (eregi("^\* $id FETCH \(UID (.*)\)", $line, $r)){
+						$result = $r[1];
+					}
+				} while (!preg_match("/^$key/", $line));
+			}
+		}
+	}
+	return $result;
 }
 
 function iil_C_Search(&$conn, $folder, $criteria){
diff -Naur program/steps/mail/show.inc ../webmail-dev.alternative.ch/program/steps/mail/show.inc
--- program/steps/mail/show.inc	Thu Oct 27 00:12:36 2005
+++ ../webmail-dev.alternative.ch/program/steps/mail/show.inc	Sun Feb 26 21:58:43 2006
@@ -64,14 +64,28 @@
   $javascript = sprintf("%s.set_env('uid', '%s');\n", $JS_OBJECT_NAME, $_GET['_uid']);
   $javascript .= sprintf("%s.set_env('safemode', '%b');", $JS_OBJECT_NAME, $_GET['_safe']);
 
+  $next = $prev = -1;
   // get previous and next message UID
-  $a_msg_index = $IMAP->message_index(NULL, $_SESSION['sort_col'], $_SESSION['sort_order']);
-  $MESSAGE['index'] = array_search((string)$_GET['_uid'], $a_msg_index, TRUE);
+  if (!($_SESSION['sort_col'] == 'date' && $_SESSION['sort_order'] == 'DESC') && 
+      $IMAP->get_capability('sort')) {
+      // Only if we use custom sorting
+      $a_msg_index = $IMAP->message_index(NULL, $_SESSION['sort_col'], $_SESSION['sort_order']);
+ 
+      $MESSAGE['index'] = array_search((string)$_GET['_uid'], $a_msg_index, TRUE);
+      $prev = isset($a_msg_index[$MESSAGE['index']-1]) ? $a_msg_index[$MESSAGE['index']-1] : -1 ;
+      $next = isset($a_msg_index[$MESSAGE['index']+1]) ? $a_msg_index[$MESSAGE['index']+1] : -1 ;
+  } else {
+      // this assumes that we are sorted by date_DESC
+      $seq = $IMAP->get_id($_GET['_uid']);
+      $prev = $IMAP->get_uid($seq + 1);
+      $next = $IMAP->get_uid($seq - 1);
+      $MESSAGE['index'] = $IMAP->messagecount() - $seq;
+  }
   
-  if (isset($a_msg_index[$MESSAGE['index']-1]))
-    $javascript .= sprintf("\n%s.set_env('prev_uid', '%s');", $JS_OBJECT_NAME, $a_msg_index[$MESSAGE['index']-1]);
-  if (isset($a_msg_index[$MESSAGE['index']+1]))
-    $javascript .= sprintf("\n%s.set_env('next_uid', '%s');", $JS_OBJECT_NAME, $a_msg_index[$MESSAGE['index']+1]);
+  if ($prev > 0)
+    $javascript .= sprintf("\n%s.set_env('prev_uid', '%s');", $JS_OBJECT_NAME, $prev);
+  if ($next > 0)
+    $javascript .= sprintf("\n%s.set_env('next_uid', '%s');", $JS_OBJECT_NAME, $next);
 
   $OUTPUT->add_script($javascript);
   }
