--- checkout.module	2009-04-24 09:08:57.000000000 -0400
+++ checkout.module	2009-11-20 15:29:53.000000000 -0500
@@ -33,17 +33,27 @@ function checkout_help($path, $arg) {
 }
 
 /**
- * Implementation of hook_init().
+ * implementation of hook_exit()
+ * check-in non-persistent locks
  */
-function checkout_init() {
+function checkout_exit() {
   global $user;
+
   if ($user->uid && user_access('check out documents')) {
     // Avoid AJAX requests unlocking a node. This header is automatically set
     // when doing AJAX requests through jQuery.
-    if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
-      checkout_handle_request($user->uid);
+    if (isset($_SESSION['checkout_edit']) && (empty($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')) {
+	  $path_parts = explode('/', $_GET['q']);
+      $on_edit_page = $path_parts[0] == 'node' && is_numeric($path_parts[1]) && $path_parts[2] == 'edit' ? true : false;
+	  foreach($_SESSION['checkout_edit'] as $nid) {
+		// do not release the lock for the current edit
+		if(!$on_edit_page || $nid != $path_parts[1]) {
+		  checkout_release($nid, $user->uid);
+		  unset($_SESSION['checkout_edit'][$nid]);
+		}
+	  }
     }
-  }
+  } 
 }
 
 /**
@@ -119,8 +129,24 @@ function checkout_form_alter(&$form, $fo
  */
 function checkout_nodeapi(&$node, $op, $teaser, $page) {
   global $user;
-
+  global $base_path;
   switch ($op) {
+    case 'prepare':
+        if ($node->nid && $user->uid && user_access('check out documents')) {
+            if (!checkout_node($node->nid, $user->uid)) {
+              // Node already locked: send back to refering page if possible
+              $referer = referer_uri();
+              $self_reference = strpos($referer, $_GET['q'], strlen($base_path));
+              if($self_reference) {
+                drupal_goto('node/'.$node->nid);
+              } else {
+                drupal_goto($referer);
+              }
+            } else {
+              $_SESSION['checkout_edit'][$node->nid] = $node->nid;
+            }
+          }
+        break;
     case 'validate':
       if (isset($node->nid) && user_access('check out documents')) {
         // Existing node. Check if we still own the lock.
@@ -143,8 +169,9 @@ function checkout_nodeapi(&$node, $op, $
     case 'update':
       if (!empty($node->checkout)) {
         checkout_persistent($node->nid);
+		unset($_SESSION['checkout_edit'][$node->nid]);
       }
-      else if ($op == 'update') {
+      elseif($op == 'update') {
         checkout_release($node->nid, $user->uid, TRUE);
       }
       break;
@@ -173,80 +200,6 @@ function checkout_cron() {
 }
 
 /**
- * Handle node locking.
- * 
- * When landing on a node edit page the current node needs to be locked.
- * When coming from an edit page the previous node needs to be unlocked.
- *
- * @param $uid
- *   The user id to (un)lock nodes for.
- */
-function checkout_handle_request($uid) {
-  global $base_path;
-  
-  // Build referer path
-  $referer_uri = parse_url(referer_uri());
-  if (variable_get('clean_url', 0)) {
-    $referer = substr($referer_uri['path'], strlen($base_path));
-  }
-  else {
-    $vars = array();
-    if (isset($referer_uri['query'])) {
-      parse_str($referer_uri['query'], $vars);
-    }
-    $referer = isset($vars['q']) ? $vars['q'] : '';
-  }
-  if ($referer = rtrim($referer, '/')) {
-    $referer = drupal_get_normal_path($referer);
-  }
-
-  // If refering and current paths match we can abort, since there can't be any
-  // locking action involved.
-  if ($_GET['q'] == $referer) {
-    return;
-  }
-
-  // Otherwise try to extract nid from path.
-  $previous_nid = checkout_get_nid($referer);
-  $current_nid = checkout_get_nid($_GET['q']);
-
-  // Check whether to release a previously edited node.
-  if ($previous_nid && (!$current_nid || $current_nid != $previous_nid)) {
-    checkout_release($previous_nid, $uid);
-  }
-
-  // Check whether to lock the current node.
-  if ($current_nid && (!$previous_nid || $previous_nid != $current_nid)) {
-    // Try to lock the node.
-    if (!checkout_node($current_nid, $uid)) {
-      // Node already locked: send back to refering page.
-      drupal_goto(referer_uri());
-    }
-  }
-}
-
-/**
- * Extract the node id from a node edit path.
- *
- * @param $path
- *   The path to match.
- * @return
- *   The node id extracted from the path.
- */
-function checkout_get_nid($path) {
-  static $regexp;
-
-  if (!isset($regexp)) {
-    $patterns = variable_get('checkout_edit_paths', "edit\nrevisions\nrevisions/*\noutline\nclassify");
-    $regexp = '@^node/(\d+)/(?:'. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/'), array('|', '.*'), preg_quote($patterns, '@')) .')$@';
-  }
-  if (preg_match($regexp, $path, $match)) {
-    return $match[1];
-  }
-  return FALSE;
-}
-
-/**
  * Fetch the lock for a node.
  *
  * @param $nid
