--- /data/switchtheme/switchtheme.module.orig	2008-11-02 05:32:56.000000000 -0800
+++ ./switchtheme.module	2009-10-18 22:45:08.000000000 -0700
@@ -67,13 +67,9 @@
  */
 function switchtheme_init() {
   global $custom_theme, $user;
+
   drupal_add_css(drupal_get_path('module', 'switchtheme') .'/switchtheme.css');
 
-  // If query parameter 'theme' is set, we always assign a new theme.
-  if (isset($_REQUEST['theme'])) {
-    $form = array('values' => array('theme' => $_REQUEST['theme']));
-    switchtheme_switch_form_submit('', $form);
-  }
   // Other modules may already have set $custom_theme, so we actually switch
   // the theme only if $custom_theme has not been set yet, or if we are on
   // administrative pages and admin_theme has been set to "System default"
@@ -81,8 +77,15 @@
   // object for authenticated users; that value is automatically used across
   // sessions by Drupal core if the "select different theme" permission has
   // been granted.
-  if (isset($_SESSION['custom_theme']) && (!isset($custom_theme) || $custom_theme === '0')) {
-    $custom_theme = $_SESSION['custom_theme'];
+
+  $new = false;
+  if (isset($_REQUEST['theme'])) {
+    $form = array('values' => array('theme' => $_REQUEST['theme']));
+    switchtheme_switch_form_submit('', $form);
+  } else {
+    if (!empty($_SESSION['custom_theme']) && empty($user->uid)) {
+      $new = $_SESSION['custom_theme'];
+    }
   }
 
   if (module_exists('browscap') && variable_get('switchtheme_browser_enabled', FALSE)) {
@@ -91,10 +94,22 @@
       $parent = trim($browser['parent']);
       $browser_theme = variable_get('switchtheme_browser_'. md5($parent), 'default');
       if ($browser_theme != 'default') {
-        $custom_theme = $browser_theme;
+        $new = $browser_theme;
       }
     }
   }
+    
+  if ($new && empty($custom_theme)) {
+    $_SESSION['custom_theme'] = $new;
+    $custom_theme = $new;
+    # We shouldn't need cache_clear_all() but we include it in case some more aggressive
+    # caching module that may get added makes it necessary. It is however required in 
+    # switchtheme_switch_form_submit() so if tempted to remove it, leave in it in the 
+    # other function.
+    if (empty($user->uid)) { 
+      cache_clear_all($base_root . request_uri(), 'cache_page');
+    }
+  }
 }
 
 /**
@@ -145,7 +160,7 @@
 
 function switchtheme_switch_form() {
   global $user, $custom_theme;
-
+    
   $form = array();
   $form['theme'] = array(
     '#type' => 'select',
@@ -171,8 +186,9 @@
  * already.
  */
 function switchtheme_switch_form_submit($form, &$form_state) {
-  global $user;
+  global $custom_theme, $user, $base_root;
 
+  $new=false;
   if ($user->uid > 0) {
     // Save the setting for authenticated users, if the "select different theme"
     // permission has been granted.
@@ -181,12 +197,22 @@
     }
     // Otherwise save the setting in the session, just like for anonymous users.
     else {
-      $_SESSION['custom_theme'] = $form_state['values']['theme'];
+      $new = $form_state['values']['theme'];
     }
   }
   elseif (user_access('switch theme')) {
     // Save the setting in the session for anonymous users.
-    $_SESSION['custom_theme'] = $form_state['values']['theme'];
+    $new = $form_state['values']['theme'];
+  }
+
+  if ($new != $_SESSION['custom_theme']) {
+    $_SESSION['custom_theme'] = $new;
+    if (empty($custom_theme)) {
+      $custom_theme = $new;
+      if (empty($user->uid)) { 
+        cache_clear_all($base_root . request_uri(), 'cache_page');
+      }
+    }
   }
 }
 
@@ -219,4 +245,3 @@
   asort($select);
   return $select;
 }
-
