diff --git a/autologout.js b/autologout.js
index b33da3f..c0f7ce5 100644
--- a/autologout.js
+++ b/autologout.js
@@ -1,13 +1,31 @@
 // $Id:
 
 Drupal.behaviors.autologout = function (context) {
-  var t = setTimeout(init, Drupal.settings.autologout.timeout);
-  var paddingTimer;
+  if(context == document) {
+    var t = setTimeout(init, Drupal.settings.autologout.timeout);
+    var paddingTimer;
+  }
   
   function init() {
     if (Drupal.settings.autologout.jquery_ui) {
-      dialog();
-      paddingTimer = setTimeout(logout, Drupal.settings.autologout.timeout_padding);
+      paddingTimer = setTimeout(confirmLogout, Drupal.settings.autologout.timeout_padding);
+
+      // While the countdown timer is going, lookup the remaining time. If there
+      // is more time remaining (i.e. a user is navigating in another tab), then
+      // reset the timer for opening the dialog.
+      $.ajax({
+        url : Drupal.settings.basePath + 'autologout_ajax_get_time_left',
+        dataType: 'json',
+        success: function(data) {
+          window.console.log(data.time);
+          if(data.time > 0) {
+            clearTimeout(paddingTimer);
+            t = setTimeout(init, data.time);
+          } else {
+            theDialog = dialog();
+          }
+        }
+      });
     } else {
       if (confirm(Drupal.settings.autologout.message) ) {
         refresh();    
@@ -18,7 +36,7 @@ Drupal.behaviors.autologout = function (context) {
   }
   
   function dialog() {
-    $('<div> ' +  Drupal.settings.autologout.message + '</div>').dialog({
+    return $('<div> ' +  Drupal.settings.autologout.message + '</div>').dialog({
       modal: true,
       closeOnEscape: false,
       width: 'auto',
@@ -53,6 +71,26 @@ Drupal.behaviors.autologout = function (context) {
     });
   }
   
+  // A user could have used the reset button on the tab/window they're actively
+  // using, so we need to double check before actually logging out.
+  function confirmLogout() {
+    $(theDialog).dialog('destroy');
+    $.ajax({
+      url : Drupal.settings.basePath + 'autologout_ajax_get_time_left',
+      dataType: 'json',
+      error: logout,
+      success: function(data) {
+        window.console.log(data.time);
+        if(data.time > 0) {
+          t = setTimeout(init, data.time);
+        }
+        else {
+          logout();
+        }
+      }
+    });
+  }
+
   function logout() {
     $.ajax({
       url: Drupal.settings.basePath + 'autologout_ahah_logout',
diff --git a/autologout.module b/autologout.module
index f340a2f..3a19859 100644
--- a/autologout.module
+++ b/autologout.module
@@ -49,6 +49,12 @@ function autologout_menu() {
     'access callback' => '_autologout_access_ahah',
     'type' => MENU_CALLBACK
   );
+  $items['autologout_ajax_get_time_left'] = array(
+    'title' => 'JS Logout AJAX Get Time Until Logout',
+    'page callback' => 'autologout_ahah_get_remaining_time',
+    'access callback' => '_autologout_access_ahah',
+    'type' => MENU_CALLBACK
+  );
   
   return $items;
 }
@@ -184,10 +190,17 @@ function autologout_user_profile_submit(&$form, &$form_state) {
  */
 function autologout_init() {
   global $user;
+
+  // The AJAX callbacks should not influence the time until a user is logged
+  // out, nor is the the JS inclusion necessary.
+  $ajax_callbacks = array('autologout_ahah_logout', 'autologout_ahah_set_last', 'autologout_ajax_get_time_left');
+  if (in_array($_GET['q'], $ajax_callbacks)) {
+    return;
+  }
+
   if ($user->uid && _autologout_logout_role($user)) { // if user is not anonymous and they should be autologged out
     // should we be enforcing on admin pages?
-    if (arg(0) == 'admin' && !variable_get('autologout_enforce_admin', FALSE)) { //Check to see if autologout on admin pages is enforeced 
-      return;
+    if (arg(0) == 'admin' && !variable_get('autologout_enforce_admin', FALSE)) { //Check to see if autologout on admin pages is enforeced       return;
     }
     
     $now = time();
@@ -345,3 +358,12 @@ function _autologout_logout_role($user) {
   
   return FALSE; 
 }
+
+/**
+ * AJAX callback that returns the time remaining for this user is logged out.
+ */
+function autologout_ahah_get_remaining_time() {
+  $timeout = variable_get('autologout_timeout', 1800);
+  $time_passed = time() - $_SESSION['autologout_last'];
+  drupal_json(array('time' => (($timeout - $time_passed) * 1000)));
+}
\ No newline at end of file
