Index: mobile_theme.module
===================================================================
--- mobile_theme.module	(revision 729)
+++ mobile_theme.module	(working copy)
@@ -1,27 +1,113 @@
 <?php
 
 /**
- * Check to see if the user is running on a mobile browser.
+ * Implementation of hook_perm
  */
+function mobile_theme_perm(){
+	return array(
+    'administer mobile theme',
+  );
+}
+
+
+/**
+ * Implementation of hook_block
+ */
+function mobile_theme_block($op = 'list', $delta = 0, $edit = array()){
+	switch($op){
+		case 'list':
+      return array(
+        'mobile_theme_switcher' => array(
+          'info' => t('Mobile Theme Switcher'),
+          'cache' => BLOCK_NO_CACHE,
+        ),
+      );
+    break;
+    case 'view':
+      $block = array(
+        'subject' => t('Mobile Theme Switcher'),
+        'content' => NULL,
+      );
+      // If there's no mobile theme, do nothing
+      $mobile_theme = variable_get('mobile_theme_selection', 'default');
+      if( $mobile_theme == 'default' ){
+      	return $block;
+      }
+
+      if ( mobile_theme_is_device_mobile() || user_access('administer mobile theme') ){
+        if( $_SESSION['mobile_theme']['theme'] == $mobile_theme ){
+        	$link_text = t('Full theme');
+        } else {
+        	$link_text = t('Mobile theme');
+        }
+        // Use $_GET here to retrieve the original path in source form.
+        $path = isset($_GET['q']) ? $_GET['q'] : '';
+        $query = drupal_query_string_encode($_GET, array('q'));
+        if ($query != '') {
+          $query .= '&';
+        }
+        $query = 'mobile_theme_switch=1';
+        $block['content'] = l( $link_text, $path, array( 'query' => $query) );
+      }
+
+      return $block;
+    break;
+	}
+}
+
+/**
+ * Implementation of hook_init
+ */
 function mobile_theme_init() {
-  $browser = browscap_get_browser();
-  if (isset($browser['ismobiledevice'])) {
-    if ($browser['ismobiledevice']) {
-      $theme = variable_get('mobile_theme_selection', 'default');
-      if ($theme != 'default') {
-        global $custom_theme;
-        $custom_theme = $theme;
+  $mobile_theme = variable_get('mobile_theme_selection', 'default');
+  if ($mobile_theme == 'default') {
+    return;
+  }
+  $default_theme = variable_get('theme_default', 'garland');
+
+  // Check if the url parameter for switching is set
+  if( $_GET['mobile_theme_switch'] == 1 ){
+  	if( $_SESSION['mobile_theme']['theme'] == $mobile_theme  ){
+      mobile_theme_set_theme($default_theme);
+  	} else {
+  		mobile_theme_set_theme($mobile_theme);
+  	}
+    drupal_goto($_GET['q']);
+  } else {
+    if( empty($_SESSION['mobile_theme']['theme']) ){
+      if( mobile_theme_is_device_mobile() ){
+    	  $_SESSION['mobile_theme']['theme'] = $mobile_theme;
+      } else {
+      	$_SESSION['mobile_theme']['theme'] = $default_theme;
       }
     }
+    mobile_theme_set_theme($_SESSION['mobile_theme']['theme']);
   }
 }
 
+function mobile_theme_set_theme($theme){
+	global $custom_theme;
+  $_SESSION['mobile_theme']['theme'] = $custom_theme = $theme;
+}
+
 /**
+* Checks if current browser is mobile device
+*/
+function mobile_theme_is_device_mobile() {
+  $browser = browscap_get_browser();
+  if (isset($browser['ismobiledevice']) && $browser['ismobiledevice'] == true) {
+      return true;
+  } else {
+      return false;
+  }
+}
+
+/**
  * Alter the system theme settings form to add the mobile theme settings.
  */
 function mobile_theme_form_alter(&$form, $form_state, $form_id) {
   if ($form_id == 'system_theme_settings') {
-    if ($form['var']['#value'] == 'theme_settings') {
+    if ($form['var']['#value'] == 'theme_settings' && user_access('administer mobile theme')) {
       $themes = array('default' => t('Default'));
       $options = list_themes();
       foreach ($options as $name => $attr) {
