--- functions/imap_mailbox.php.orig	2008-07-08 15:05:45.000000000 -0700
+++ functions/imap_mailbox.php	2008-07-08 15:10:21.000000000 -0700
@@ -572,7 +572,7 @@
         global $data_dir, $username, $list_special_folders_first,
                $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
                $move_to_trash, $move_to_sent, $save_as_draft,
-               $delimiter, $noselect_fix_enable;
+               $delimiter, $noselect_fix_enable, $default_sub_of_inbox;
         $inbox_in_list = false;
         $inbox_subscribed = false;
 
@@ -684,6 +684,21 @@
             }
         }
 
+        /**
+         * If folders are not subfolders of INBOX, inbox subfolders must be added
+         * before special folders. IMAP servers: Dovecot, hMailServer
+         */
+        if (! $default_sub_of_inbox) {
+            /* Find INBOX's children */
+            for($k = 0; $k < $cnt; ++$k) {
+                if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') && 
+                    strtolower($boxesall[$k]['unformatted']) != 'inbox') {
+                    $boxesnew[] = $boxesall[$k];
+                    $used[$k] = true;
+                }   
+            }
+        }
+
         /* List special folders and their subfolders, if requested. */
         if ($list_special_folders_first) {
             for($k = 0; $k < $cnt; ++$k) {
@@ -694,17 +709,20 @@
             }
         }
 
-
-        /* Find INBOX's children */
-        for($k = 0; $k < $cnt; ++$k) {
-            if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') &&
-                strtolower($boxesall[$k]['unformatted']) != 'inbox') {
-                $boxesnew[] = $boxesall[$k];
-                $used[$k] = true;
+        /**
+         * If folders are only under INBOX, 
+         */
+        if ($default_sub_of_inbox) {
+            /* Find INBOX's children */
+            for($k = 0; $k < $cnt; ++$k) {
+                if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') && 
+                    strtolower($boxesall[$k]['unformatted']) != 'inbox') {
+                    $boxesnew[] = $boxesall[$k];
+                    $used[$k] = true;
+                }   
             }
         }
 
-
         /* Rest of the folders */
         for($k = 0; $k < $cnt; $k++) {
             if (!$used[$k]) {
