--- ad.module.orig	2008-04-19 12:02:00.000000000 +1000
+++ ad.module	2008-04-24 02:20:13.000000000 +1000
@@ -52,6 +52,7 @@
       $output = adserve_ad($options);
       break;
     case 'iframe':
+    case 'jquery':
       $display_variables = "m=iframe";
       // Fall through...
     case 'javascript':
@@ -101,6 +102,10 @@
         }
         $output = "<iframe src=\"$src\" $append></iframe>";
       }
+      else if ($options['ad_display'] == 'jquery') {
+        // Pass the url to the theme function to get a CSS id for jQuery to use
+        $output = $src;
+      }
       else {
         $output = "<script type=\"text/javascript\" src=\"$src\"></script>";
       }
@@ -108,7 +113,7 @@
   }
 
   if (user_access('show advertisements')) {
-    return theme('ad_display', $group, $output);
+    return theme('ad_display', $group, $output, $options['ad_display']);
   }
   else {
     return theme('ad_display', 'none', "<!-- Enable 'show advertisements' permission if you wish to display ads here. -->");
@@ -119,10 +124,24 @@
  * Function to display the actual advertisement to the screen.  Wrap it in a 
  * theme function to make it possible to customize in your own theme.
  */
-function theme_ad_display($group, $display) {
+function theme_ad_display($group, $display, $method='javascript') {
+  static $id = 0; $id++;
   // The naming convention for the id attribute doesn't allow commas.
-  $group = preg_replace('/[,]/', '', $group);
-  return "\n<div class=\"advertisement\" id=\"group-$group\">$display</div>\n";
+  $group = preg_replace('/[,]/', '+', $group);
+  switch ($method) {
+    case 'jquery':
+      // We wrap the javascript as CDATA so that ampersands in the url will pass as valid xhtml
+      $display = str_replace('&amp;','&',$display);
+      return "\n<div class=\"advertisement group-$group\" id=\"ad_group-$id\">"
+        ."<script type=\"text/javascript\">//<![CDATA[\n"
+        ."$(document).ready(function(){ jQuery(\"div#ad_group-$id\").load(\"$display\"); });\n"
+        ."//]]></script></div>\n";
+    case 'raw':
+    case 'iframe':
+    case 'javascript':
+    default:
+      return "\n<div class=\"advertisement group-$group\" id=\"ad_group-$id\">$display</div>\n";
+  }
 }
 
 /**
@@ -2164,7 +2183,7 @@
   );
 
   // Provide hook for ad_display_TYPE modules to set display TYPE.
-  $display_options = array_merge(array('javascript' => t('JavaScript'), 'iframe' => t('IFrame'), 'raw' => t('Raw')), module_invoke_all('displayapi', 'display_method'), array());
+  $display_options = array_merge(array('javascript' => t('JavaScript'), 'jquery' => t('jQuery'), 'iframe' => t('IFrame'), 'raw' => t('Raw')), module_invoke_all('displayapi', 'display_method'), array());
 
   // Provide hook for ad_display_TYPE modules to define inline description.
   $description = t('This setting configures the default method for displaying advertisements on your website.  It is possible to override this setting when making direct calls to ad(), as described in the documentation.  Using the JavaScript display method allows you to display random ads and track views even on cached pages.  Using the Raw method does not work well together with Drupal\'s page cache enabled as the advertisements will not change until the page cache is updated, and statistics will not be incremented each time an advertisement is displayed.');
