--- themeswitcher.module 2006-09-04 10:40:16.000000000 -0400
+++ themeswitcher.module	2006-09-04 12:14:59.000000000 -0400
@@ -86,20 +86,25 @@ function themeswitcher_cron() {
 }
 
 function themeswitcher_block($op = 'list', $delta = 0) {
-  if ($op == 'list') {
-    $blocks[0]['info'] = t('Random theme');
-    $blocks[1]['info'] = t('Theme browser');
-    return $blocks;
-  }
-  elseif ($delta == 0) {
-    $block['subject'] = t('Random theme');
-    $block['content'] = themeswitcher_display_random_block();
-    return $block;
-  }
-  elseif ($delta == 1) {
-    $block['subject'] = t('Browse themes');
-    $block['content'] = themeswitcher_display_nextprev_block();
-    return $block;
+  switch ($op) {
+    case 'list': 
+      $blocks[0]['info'] = t('Random theme');
+      $blocks[1]['info'] = t('Theme browser');
+      return $blocks;
+    break;
+    case 'view':
+      if ($delta == 0) {
+        $block['subject'] = t('Random theme');
+        $block['content'] = themeswitcher_display_random_block();
+      }
+      elseif ($delta == 1) {
+        $block['subject'] = t('Browse themes');
+        $block['content'] = themeswitcher_display_nextprev_block();
+      }
+      return $block;
+    break;
+    case 'default':
+    break;
   }
 }
 
@@ -116,43 +121,62 @@ function themeswitcher_display_random_bl
 }
 
 function themeswitcher_display_nextprev_block() {
-  global $custom_theme;
+  global $custom_theme, $user;
   $themes = list_themes();
   ksort($themes);
-
-  if (!$custom_theme) {
-    $next = current($themes);
-    if (file_exists($next->screenshot)) {
-      $output .= l("<img src=\"$next->screenshot\" alt=\"preview of $next->name\"  width=\"75px\"/>", $_GET['q'], NULL, 'theme='. $next->name, NULL, FALSE, TRUE);
-      $output .= '<br/>';
+  $i = 0;
+  foreach ($themes as $key => $theme) {
+    $themes[$key]->screenshot = dirname($theme->filename) . '/screenshot.png';
+    // I need both a numerical and an associative array
+    $sort[$i] = $themes[$key];
+    $themes[$key]->order = $i;
+    $i++;
+  }  
+  $count = count($themes) - 1; // offest for starting at zero
+  // assign the current theme as value of $custom_theme
+  if (!empty($custom_theme)) {
+    $current = $themes[$custom_theme];
+  }
+  else {
+    // if no theme, use the user default
+    if (empty($user->theme)) {
+      $conf = $GLOBALS['conf']['theme_default'];
     }
-    $output .= l($next->name . ' ›', $_GET['q'], NULL, 'theme='. $next->name);
-    return $output;
-  }
+    else {
+      $conf = $user->theme;
+    }  
+    $current = $themes[$conf];
+  }
+  
+  // derive the next and previous from the order of $current
+  $pos = $current->order;
+  $next = $pos + 1;
+  $prev = $pos - 1;
 
-  while (list($key, $theme) = each($themes)) {
-    if ($custom_theme == $theme->name) {
-      prev($themes);
-      $previous = prev($themes);
-      if ($previous) {
-        if (file_exists($previous->screenshot)) {
-          $output = l("<img src=\"$previous->screenshot\" alt=\"preview of $previous->name\"  width=\"75px\"/>", $_GET['q'], NULL, 'theme='. $previous->name, NULL, FALSE, TRUE);
-          $output .= '<br/>';
-        }
-        $output .= l('‹ '. $previous->name, $_GET['q'], NULL, 'theme='. $previous->name);
-      }
-      next($themes);
-      $next = next($themes);
-      if ($next) {
-        $output .= ' | ';
-        if (file_exists($next->screenshot)) {
-          $output .= l("<img src=\"$next->screenshot\" alt=\"preview of $next->name\"  width=\"75px\"/>", $_GET['q'], NULL, 'theme='. $next->name, NULL, FALSE, TRUE);
-          $output .= '<br/>';
-        }
-        $output .= l($next->name . ' ›', $_GET['q'], NULL, 'theme='. $next->name);
-      }
-      return $output;
-    }
+  if ($pos == $count) {
+    $next = 0;
+    $prev = $pos - 1;
+  }
+  elseif ($pos == 0) {
+    $next = $pos + 1;
+    $prev = $count;
+  }
+  
+  $next_theme = $sort[$next];
+  $prev_theme = $sort[$prev];
+
+  // add the themes to the output  
+  if (file_exists($prev_theme->screenshot)) {
+    $output = '<p>' . t('Previous theme') . '<br />';
+    $output .= l("<img src=\"$prev_theme->screenshot\" alt=\"preview of $prev_theme->name\" />", $_GET['q'], NULL, 'theme='. $prev_theme->name, NULL, FALSE, TRUE);
+  }
+  $output .= '<br />' . l('‹ '. $prev_theme->name, $_GET['q'], NULL, 'theme='. $prev_theme->name) . '</p>';    
+  $output .= '<p>' . t('Next theme') . '<br />';
+  if (file_exists($next_theme->screenshot)) {
+    $output .= l("<img src=\"$next_theme->screenshot\" alt=\"preview of $next_theme->name\"  />", $_GET['q'], NULL, 'theme='. $next_theme->name, NULL, FALSE, TRUE);
   }
+  $output .= '<br />' . l($next_theme->name . ' ›', $_GET['q'], NULL, 'theme='. $next_theme->name) . '</p>';        
+  return $output;
 }
-?>
+
+
