Index: google_admanager.module
===================================================================
--- google_admanager.module	(revision 248)
+++ google_admanager.module	(working copy)
@@ -100,40 +100,80 @@ function google_admanager_admin_settings
  * @return google admanager slot script 
  */ 
 function theme_google_admanager_block($id, $ad_slot) {
-
-  google_admanager_add_js('GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");');
-    
-  return '<script type="text/javascript">GA_googleFillSlot("' . $ad_slot . '");</script>';
+  google_admanager_add_js('GA_googleAddSlot("'. $id .'", "'. $ad_slot .'");');
+  return '<script type="text/javascript">GA_googleFillSlot("'. $ad_slot .'");</script>';
 }
 
 /**
- * Stored the ad slots and when called with no slot, return the whole ad manager javascript
- * This function should be called from pianofiles_preprocess_page(&$vars) as followed
- *  if (module_exists('google_admanager')) {
- *    $vars['scripts'] .= google_admanager_add_js();
- *  }
- * @param $ad_slot string with the addSlot script
- * @return google admanager code 
+ * Store ad slots js and when called with no slot, return the whole ad manager javascript
+ * 
+ * @param $js (optional) string with the slot script to add to the array.
+ * @param $type (optional) scripts have to be split up into 4 types and are output
+ *   in order ['init', 'service', 'slot', 'close'].
+ * @return if $js is empty, then an array of stored google_admanager javascript 
  */ 
-function google_admanager_add_js($ad_slot = NULL) {
-  static $google_admanager_slots;
- 
-  if (isset($ad_slot)) {
-    $google_admanager_slots .= $ad_slot;
-  }
-  else {
-    if (isset($google_admanager_slots)) {
+function google_admanager_add_js($js = NULL, $type = 'slot') {
+  static $ga_js = array();
+
+  // add the js to a type
+  if(isset($js) && isset($type)) {
+    $ga_js[$type][] = $js;
+     
+    //add the init and service scripts the first time this is run
+    if (!isset($ga_js['init'])) {
+      //drupal_add_js can't load externaljs in 6, but it will in 7. this is a workaround.
+      $external_js = 'http://partner.googleadservices.com/gampad/google_service.js';
+      google_admanager_add_js('document.write(unescape("%3Cscript src=\''. $external_js . '\' type=\'text/javascript\'%3E%3C/script%3E"));', 'init');
+
       $id = variable_get('google_admanager_account', '');
-      // javascript which is supposed to come in the header
-      $js = '<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>' . "\n";
-      $js .= '<script type="text/javascript">GS_googleAddAdSenseService("' . $id . '"); GS_googleEnableAllServices();</script>' . "\n";
-      $js .= '<script type="text/javascript">' . $google_admanager_slots . '</script>' . "\n";
-      $js .= '<script type="text/javascript">GA_googleFetchAds();</script>' . "\n";
-      return $js;
+      google_admanager_add_js('GS_googleAddAdSenseService("'. $id .'");', 'service');
+      google_admanager_add_js('GS_googleEnableAllServices();', 'service');
+   
+      // set the close script to fetch the ads.
+      google_admanager_add_js('GA_googleFetchAds();', 'close');  
     }
+  return;
+  }
+
+  // $ga_js['init'] must have been set since we didn't return, so if js isn't set then return
+  // the whole array, just like drupal_add_js(). 
+
+  if (!isset($js)) {
+    return $ga_js;
   }
 }
 
+/** 
+* Output the Google Admanager scripts by way of drupal_add_js().
+*  
+* @param $scope (optional) the scope to output the javascript. see drupal_add_js().
+*/
+function google_admanager_get_js($scope = 'header') {
+  //get the js for this page if it exists
+  $ga_js = google_admanager_add_js(); 
+  if(isset($ga_js)) {
+    $output_order = array('init', 'service', 'slot', 'close');
+    foreach ($output_order as $type) {
+      $output = '';
+      foreach($ga_js[$type] as $js) {
+        $output .= $js . "\n";
+      }
+      drupal_add_js($output, 'inline', $scope);
+    }
+  } 
+}
+   
+/**
+ * Implementation of hook_preprocess_page().
+ */
+function google_admanager_preprocess_page(&$vars) {
+  //output the scripts through drupal_add_js()
+  google_admanager_get_js();
+ 
+  //this doesn't have any effect if another module/theme later overides it. 
+  $vars['scripts'] =  drupal_get_js();
+}
+
 /**
  * Implementation of hook_filter().
  *
