--- parallel.orig	2010-03-18 13:57:59.000000000 +0700
+++ parallel.module	2010-06-22 22:30:44.183750000 +0700
@@ -71,6 +71,20 @@ function parallel_form_alter(&$form, $fo
       '#description'   => t('//data3.example.com'),
       '#weight' => -16,
     );
+    $form['bandwidth_optimizations']['parallel_multiple_img'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Enter multiple IMG domains'),
+      '#default_value' => variable_get('parallel_multiple_img', ''),
+      '#description'   => t('//data3.example.com, //img2.cdnx.com, //foo9.cdn.com'),
+      '#weight' => -15,
+    );
+    $form['bandwidth_optimizations']['parallel_multiple_img_enabled'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Use multiple IMG domains'),
+      '#default_value' => variable_get('parallel_multiple_img_enabled', FALSE),
+      '#description' => t('If selected, enter multiple IMG domains above separated by commas.'),
+      '#weight' => -14,
+    );
   }
 }
 
@@ -133,16 +147,52 @@ function parallel_preprocess_page(&$vari
  */
 function parallel_image_replace($html, $subdomain) {
   $host_re = array(
-    '(https?\:)?\/\/' . preg_quote($_SERVER['SERVER_NAME']) . '\/',
-    '(https?\:)?\/\/' . preg_quote($_SERVER['HTTP_HOST']) . '\/',
+    preg_quote($_SERVER['SERVER_NAME']),
+    preg_quote($_SERVER['HTTP_HOST']),
     // manually add domains here
-    //'(https?\:)?\/\/' .preg_quote('manual.domain.com'). '\/',
+    // preg_quote('manual.domain.com'),
   );
-  $pattern = '/(<img\s+|<img\s+[^>]*\s+)src\s*=\s*(\'|")?(\/([^\/])|' . implode('|', $host_re) . ')/i';
-  $replacement = '$1 src=$2' . $subdomain . '/$4';
-  return preg_replace($pattern, $replacement, $html);
+  $pattern = '@(<img\s+([^>]*\s+)?)src\s*=\s*(\'|")(/([^/])|(https?\:)?//(' . implode('|', $host_re) . ')/)([^\'"]+)@i';
+  if (variable_get('parallel_multiple_img_enabled', FALSE)) {
+    $html = preg_replace_callback($pattern, parallel_image_replace_callback, $html);
+  }
+  else {
+    $html = preg_replace($pattern, '$1 src=$3' . $subdomain . '/$5$8', $html);
+  }
+  return $html;
 }
 
+/**
+ * A callback that will be called and passed an array of matched elements
+ *
+ * @param $matches
+ *   matched elements
+ *
+ * @return string
+ *   replacement string
+ */
+$_parallel_cdns;
+$_parallel_cdns_count;
+function parallel_image_replace_callback($matches) {
+  global $_parallel_cdns, $_parallel_cdns_count;
+  if (!isset($_parallel_cdns)) {
+    $_parallel_cdns = explode(',', str_replace(' ', '', variable_get('parallel_multiple_img', '')));
+    $_parallel_cdns_count = count($_parallel_cdns);
+  }
+  
+  if ($_parallel_cdns_count < 1) {
+    return $matches[0]; // return original string
+  }
+  
+  // pick a domain based on the filename
+  // Might need to use a UTF-8 compatible version of the ord() function. http://php.net/ord#78032
+  $count = 0;
+  foreach(str_split($matches['8']) as $char) {
+    $count += ord($char);
+  }
+  $idx = $count % $_parallel_cdns_count;
+  return "$matches[1] src=$matches[3]" . $_parallel_cdns[$idx] . "/$matches[5]$matches[8]";
+}
 
 /**
  * Implementation of hook_init().
