--- javascript_aggregator.module	2008-04-19 11:58:43.000000000 -0400
+++ javascript_aggregator_pre.module	2008-10-16 14:19:09.000000000 -0400
@@ -1,5 +1,5 @@
 <?php
-// $Id: javascript_aggregator.module,v 1.15 2008/04/19 15:58:43 derjochenmeyer Exp $
+// $Id: javascript_aggregator.module 2812 2008-10-10 06:58:33Z btully $
 
 /**
  * @file
@@ -14,14 +14,23 @@
  */
 function javascript_aggregator_cache($scripts) {
   if (variable_get('javascript_aggregator_aggregate_js', 0)) {
-    // compiling exclude pattern
-    if ($exclude_pattern = variable_get('javascript_aggregator_exclude_js', FALSE)) {
-      $exclude_pattern = strtr($exclude_pattern, array(
+    // compiling pre exclude patterns
+    if ($pre_exclude_pattern = variable_get('javascript_aggregator_pre_exclude_js', FALSE)) {
+      $pre_exclude_pattern = strtr($pre_exclude_pattern, array(
           '\\' => '/',
           '.' => '\\.',
         ));
-      $exclude_pattern = preg_split('/\r?\n/', $exclude_pattern, -1, PREG_SPLIT_NO_EMPTY);
-      $exclude_pattern = '~'.implode("$|", $exclude_pattern).'$~';
+      $pre_exclude_pattern = preg_split('/\r?\n/', $pre_exclude_pattern, -1, PREG_SPLIT_NO_EMPTY);
+      $pre_exclude_pattern = '~'.implode("$|", $pre_exclude_pattern).'$~';
+    }
+    // compiling post exclude patterns
+    if ($post_exclude_pattern = variable_get('javascript_aggregator_post_exclude_js', FALSE)) {
+      $post_exclude_pattern = strtr($post_exclude_pattern, array(
+          '\\' => '/',
+          '.' => '\\.',
+        ));
+      $post_exclude_pattern = preg_split('/\r?\n/', $post_exclude_pattern, -1, PREG_SPLIT_NO_EMPTY);
+      $post_exclude_pattern = '~'.implode("$|", $post_exclude_pattern).'$~';
     }
     // One regular expression to extract and remove .js paths and filenames from $scripts variable
     $pattern = "!(<script type=\"text\/javascript\" src=\")(.*?)(\"(.*?)><\/script>)!";
@@ -33,11 +42,15 @@ function javascript_aggregator_cache($sc
     $scripts  = preg_replace($pattern, "", $scripts);
 
     $scripts_js_files = array();
-		$scripts_js_links = array();
+		$scripts_js_links_pre = array();
+		$scripts_js_links_post = array();
 
     foreach ($matches[2] as $value) {
-      if ($exclude_pattern && preg_match($exclude_pattern, $value)) {
-        $scripts_js_links[] = $value; // prepares it to add it later after aggregation
+      if ($pre_exclude_pattern && preg_match($pre_exclude_pattern, $value)) {
+        $scripts_js_links_pre[] = $value; // prepares it to add it to BEFORE aggregation
+      }
+      elseif ($post_exclude_pattern && preg_match($post_exclude_pattern, $value)) {
+          $scripts_js_links_post[] = $value; // prepares it to add it to AFTER aggregation)
       }
       else {
         // read contents of js file and add it to $contents
@@ -58,9 +71,18 @@ function javascript_aggregator_cache($sc
     file_check_directory($jspath, FILE_CREATE_DIRECTORY);
     $jsfile = $jspath.'/'.$filename;
 
-    // link the aggregated file
-    array_unshift($scripts_js_links, base_path().$jsfile);
-
+    $scripts_js_links = array();
+ 
+    foreach ($scripts_js_links_pre as $pre) {
+      $scripts_js_links[] = $pre;
+    }
+    
+    $scripts_js_links[] = base_path().$jsfile;
+ 
+    foreach ($scripts_js_links_post as $post) {
+      $scripts_js_links[] = $post;
+    }
+    
     if (!file_exists($jsfile)) {
       $contents = '';
 
@@ -84,7 +106,7 @@ function javascript_aggregator_cache($sc
         $script_links .= "\n<script type=\"text/javascript\" src=\"$add_to_scripts\"></script>";
       }
     }
-    $scripts = $script_links.$scripts;
+		$scripts = $script_links.$scripts;
   }
   return $scripts;
 }
@@ -144,10 +166,18 @@ function javascript_aggregator_form_alte
       '#description' => t("This option removes /* */ comments to reduce file size. If you encounter js errors disable this option."),
     );
     
-    $form['javascript_aggregation']['javascript_aggregator_exclude_js'] = array(
+    $form['javascript_aggregation']['javascript_aggregator_pre_exclude_js'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Exclude from js aggregation and load BEFORE aggregated scripts'),
+      '#default_value' => variable_get('javascript_aggregator_pre_exclude_js', ''),
+      '#disabled' => !$is_writable,
+      '#description' => t('Enter one js file per line that should be excluded from js aggregation. Check your HTML source for paths. Jquery Example: <em>/misc/jquery.js</em> Partial paths are also possible, this does the same <em>jquery.js</em>'),
+    );
+
+    $form['javascript_aggregation']['javascript_aggregator_post_exclude_js'] = array(
       '#type' => 'textarea',
-      '#title' => t('Exclude from js aggregation'),
-      '#default_value' => variable_get('javascript_aggregator_exclude_js', ''),
+      '#title' => t('Exclude from js aggregation and load AFTER aggregated scripts'),
+      '#default_value' => variable_get('javascript_aggregator_post_exclude_js', ''),
       '#disabled' => !$is_writable,
       '#description' => t('Enter one js file per line that should be excluded from js aggregation. Check your HTML source for paths. TinyMCE Example: <em>/sites/all/modules/tinymce/tinymce/jscripts/tiny_mce/tiny_mce.js</em> Partial paths are also possible, this does the same <em>tiny_mce.js</em>'),
       
