--- ad.module.original	Thu Apr 16 18:24:54 2009
+++ ad.module	Fri May 01 17:15:06 2009
@@ -11,6 +11,13 @@
 
 require_once('ad_token.inc');
 
+
+function ad_init() {
+  // do this in init hook so that block caching doesn't skip it
+  $method=variable_get('ad_display', 'javascript');
+  if ($method=='jquery') drupal_add_js('misc/jquery.js', 'core');
+}
+
 /**
  * Implementation of hook_theme().
  */
@@ -157,7 +164,7 @@
       else {
         $query['u'] = $_GET['q'];
       }
-      $src = htmlentities(url($base_url .'/'. $adserve, array('query' => $query)));
+      $src = url($base_url .'/'. $adserve, array('query' => $query));
       if ($options['ad_display'] == 'iframe') {
         // TODO: We need to know the IFrame size before it is displayed.  This
         // limits the flexibility of what can be displayed in these frames.
@@ -172,25 +179,20 @@
         if ($width = variable_get('ad_iframe_width', '')) {
           $append .= 'width="'. $width .'" ';
         }
-        $output = '<iframe src="'. $src ."\" $append></iframe>";
+        $output = '<iframe src="'. htmlentities($src) ."\" $append></iframe>";
       }
       else if ($options['ad_display'] == 'jquery') {
         // The theme function uses this to generate a CSS id for jQuery to use.
         $output = $src;
       }
       else {
-        $output = "<script type='text/javascript' src='$src'></script>";
+        $output = "<script type='text/javascript' src='". htmlentities($src) ."'></script>";
       }
       break;
   }
 
   if (user_access('show advertisements')) {
-    if (isset($options['div']) && $options['div'] !== FALSE) {
-      return theme('ad_display', $group, $output, $options['ad_display']);
-    }
-    else {
-      return theme('ad_display', $group, $output, 'raw');
-    }
+    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. -->");
@@ -209,10 +211,10 @@
 
   // The naming convention for the id attribute doesn't allow commas.
   $group = preg_replace('/[,]/', '+', $group);
-
+  
   if ($method == 'jquery') {
-    drupal_add_js('misc/jquery.js', 'core');
-    return "\n<div class=\"advertisement group-$group\" id=\"group-id-$id\">\n <script type=\"text/javascript\">\n//<![CDATA[\n  $(document).ready(function(){ jQuery(\"div#group-id-$id\").load(\"$display\"); });\n //]]>\n </script>\n</div>\n";
+    $div_id='group-id-'. $group .'-'. $id .'-'. mt_rand(); // trying to make id unique - useful for block caching
+    return "\n<div class=\"advertisement group-$group\" id=\"$div_id\">\n <script type=\"text/javascript\">\n//<![CDATA[\n  $(document).ready(function(){ jQuery(\"div#$div_id\").load(\"$display\"); });\n //]]>\n </script>\n</div>\n";
   }
   else if ($method == 'raw') {
     return $display;
