Index: swfobject_api.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/swfobject_api/swfobject_api.module,v
retrieving revision 1.2.2.20
diff -u -p -r1.2.2.20 swfobject_api.module
--- swfobject_api.module	24 Jun 2008 12:33:39 -0000	1.2.2.20
+++ swfobject_api.module	1 Oct 2008 14:32:36 -0000
@@ -1,52 +1,40 @@
-<?php 
-
+<?php
 // $Id: swfobject_api.module,v 1.2.2.20 2008/06/24 12:33:39 arthuregg Exp $
 
-/* Please see the README.txt file for more information on this module */
-
+/**
+ * @file
+ * Integrates the SWFObject Javascript library, allowing you to easily
+ * embed flash in your website.
+ *
+ * Please see the README.txt file for more information on this module.
+ */
 
-/** 
- *  Implementation of hook_help()
+/**
+ * Implementation of hook_help().
  */
-function swfobject_api_help($section) {
-  $output = '';
-  switch ($section) {
+function swfobject_api_help($path, $arg) {
+  switch ($path) {
     case 'admin/help#swfobject_api':
     case 'admin/settings/swfobject_api':
-      return t('This module creates an API for flash content generation in pages based on the swfObject library. For more information, visit <a href="http://blog.deconcept.com/swfobject/">http://blog.deconcept.com/swfobject/</a>.');
+      return t('This module creates an API for flash content generation in pages based on the swfObject library. For more information, visit <a href="http://code.google.com/p/swfobject/">http://code.google.com/p/swfobject/</a>.');
     break;
-    
   }
 }
 
-
 /**
  * Implementation of hook_menu().
  */
-function swfobject_api_menu($may_cache) {
+function swfobject_api_menu() {
   $items = array();
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'admin/settings/swfobject_api',
-      'title' => t('SWFObject API'),
-      '#description' => t('Configure the SWFObject API.'),
-      'access' => user_access('administer swfobject_api'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => 'swfobject_api_settings_form'
-    );
-  }
+  $items['admin/settings/swfobject_api'] = array(
+    'title' => 'SWFObject API',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('swfobject_api_settings_form'),
+    'access arguments' => array('administer site configuration')
+  );
   return $items;
 }
 
-
-/**
- * implementation of drupal's hook_perm
- */
-function swfobject_api_perm(){
-  return array('administer swfobject_api');
-}
-
-
 /**
  * Menu callback: SWFObject API configuration form.
  */
@@ -69,27 +57,47 @@ function swfobject_api_settings_form() {
     '#type' => 'checkbox',
     '#title' => t('Enable express install.'),
     '#description' => t('Express install allows player upgrades without having to leave the site. Only versions 6.0.65 and above are supported.'),
-    '#default_value' => variable_get('swfoa_express', false),
+    '#default_value' => variable_get('swfoa_express', FALSE),
   );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
 
   return system_settings_form($form);
 }
 
- 
+/**
+ * Implementation of hook_theme().
+ */
+function swfobject_api_theme() {
+  return array(
+    'swfobject_api' => array(
+      'arguments' => array(
+        'url' => NULL,
+        'params' => NULL,
+        'vars' => NULL,
+        'id' => NULL,
+        'attributes' => NULL
+      )
+    )
+  );
+}
+
 /**
  * Implementation of theme
- * @param $url a web accessible url to the flash file
- * @param $params an associative array of parameters that describe the SWF
- * @param $vars an associative array of variables to pass through to the SWF flashvars value
- * @param $id is an id to appened to the so object
- * @param string $method determins if the object is written or placed in jquery document ready
+ * @param string $url a web accessible url to the flash file
+ * @param array $params an associative array of parameters that describe the SWF
+ * @param array $vars an associative array of variables to pass through to the SWF flashvars value
+ * @param string $id is an id to appened to the so object
+ * @param array $attributes is keyed array of attributes to apply
  * @return themed html
  */
-function theme_swfobject_api($url, $params = array(), $vars = array(), $id = null, $method = null) {
+function theme_swfobject_api($url, $params = NULL, $vars = NULL, $id = NULL, $attributes = NULL) {
   static $id_count;
 
-  // set the base parameters
+  // load the swfobject library
+  $path = drupal_get_path('module', 'swfobject_api');
+  drupal_add_js($path .'/swfobject.js');
+
+  // build the base params
   $base_params = array(
     'width' => '100%',
     'height' => '100%',
@@ -98,91 +106,108 @@ function theme_swfobject_api($url, $para
     'type' => 'movie',
     'bg_color' => '#FFFFFF'
   );
-  
-  $params = array_merge($base_params, $params);
-  
-  // Express install redirect URL: as per the SWFObject docs, this should
-  // actually be xiRedirectUrl; variable name changed for simplicity.
-  if (isset($param['express_redirect'])) {
-    $redirect = $param['express_redirect'];
+
+  // merge the parameters
+  if ($params) {
+    $params = array_merge($base_params, $params);
   }
- 
+
   // create a unique id, use what's passed in, what has been saved locally
-  if ($id) { $id_count = $id; }
+  if ($id) {
+    $id_count = $id;
+  }
   else {
     $id_count = $id_count ? $id_count : 1;
   }
-  
+
   // set the name of the swf file
   $name = form_clean_id(str_replace('.swf', '', basename($url))) .'_'. $id_count;
- 
-  // set the div id to the params
+
+  // set the div id from the params
   if ($params['div_id']) {
-    $div_id = $params['div_id'];
+    $div_id =  $params['div_id'];
     unset($params['div_id']);
   }
-  else {    
-    $div_id = 'flashcontent_'. $name;    
+  else {
+    $div_id = 'flashcontent_'. $name;
   }
-  
-  // build the class
+
+  // build the class with anything that was passed in
   if ($params['class']) {
     $class = ' class="'. $params['class'] .'"';
     unset($params['class']);
   }
-  
-  // build the div structure
-  $html[] = '<div id="'. $div_id .'" '. $class .'>'. $params['no_flash'] ."</div>";
-  
-  // build the javascript
-  // get the path to thje swf object library
-  $path = base_path() . drupal_get_path('module', 'swfobject_api') .'/swfobject.js';
-  
-  // we need to add the swfobject library but we need to do this inside the
-  // html because theme functions can be cached 
-  $html[] = '<script type="text/javascript" src="'. $path .'"></script>';
-  
-  $html[] = '<script type="text/javascript"><!-- ';
-  
-  // are we using jquery to display this?
-  if (! $method) { $html[] = ' $(document).ready(function () {'; }
-  
-  // build the base of the object for swfobject to render
-  $html[] = "    var so$id_count = new SWFObject('$url', 'swf_$name', '". $params['width'] ."', '". $params['height'] ."', '". $params['version'] ."', '". $params['bg_color'] ."');";
 
-  // we can remove these all now since they should not be needed in the flash object
-  unset($params['width'], $params['height'], $params['no_flash'], $params['version'], $params['bg_color'], $params['express_redirect']);
+  // build the javascript
+  $script = array();
+  $script[] = "$(document).ready(function () {";
 
+  // add the parameters
   if ($params) {
+    $script[] = 'var params = {';
+    $i = 1;
+    $count = count($params);
     foreach ($params as $key => $value) {
-      $html[] = "      so$id_count.addParam('$key', '$value');";
+      // we don't output a comma on the last element in the array
+      $comma = $i++ < $count ? ',' : '';
+      $script[] = "  $key: '$value'" . $comma;
     }
+    $script[] ='};';
   }
-  
-  if ($vars) { 
+
+  // add the flashvars
+  if ($vars) {
+    $script[] = 'var flashvars = {';
+    $i = 1;
+    $count = count($vars);
     foreach ($vars as $key => $value) {
-      $html[] = "      so$id_count.addVariable('$key', ". drupal_to_js($value) .");";
+      $comma = $i++ < $count ? ',' : '';
+      $script[] = "  $key: '$value'" . $comma;
     }
+    $script[] = "};";
   }
-  
-  if (variable_get('swfoa_express', '')) {
-    $html[] = "      so$id_count.useExpressInstall('". $path ."/expressinstall.swf');";
-    if (isset($redirect)) {
-      $html[] = "      so$id_count.setAttribute('xiRedirectUrl', '". $redirect ."');";
-     }
-   }
-  
-  // now write the object
-  $html[] = "     so$id_count.write('$div_id');";
-  
-  // are we using jquery to display this?
-  if (! $method) { $html[] = " });"; }
-  
- 
-  $html[] = ' --></script>';
-  
+  else {
+    $script[] = "var flashvars = false;";
+  }
+
+  // add the attributes
+  if ($attributes) {
+    $script[] = 'var attributes = {';
+    $i = 1;
+    $count = count($attributes);
+    foreach ($attributes as $key => $value) {
+      $comma = $i++ < $count ? ',' : '';
+      $script[] = "  $key: '$value'" . $comma;
+    }
+    $script[] = "};";
+  }
+  else {
+    $script[] = "var attributes = {};";
+  }
+
+  // Express install redirect URL: as per the SWFObject docs, this should
+  // actually be xiRedirectUrl; variable name changed for simplicity.
+  if (!$params['express_redirect']) {
+    $params['express_redirect'] = variable_get('swfoa_express', TRUE) ? drupal_get_path('module', 'swfobject_api') .'/expressinstall.swf' : 'false';
+  }
+
+  // Set the minimum version of flash expected
+  if (!$params['version']) {
+    $params['version'] = variable_get('swfoa_version', 9) ? variable_get('swfoa_express', "9.0.0") : 'false';
+  }
+
+  // register the swfojbect
+  $script[] = "  swfobject.embedSWF('$url', '$div_id', '". $params['width'] ."', '". $params['height'] ."', '".  $params['version'] ."', '". $params['express_redirect'] ."', flashvars, params, attributes );";
+
+  // close the document ready
+  $script[] = "});";
+
+  // output the js
+  drupal_add_js(implode("\n", $script), 'inline' );
+
   // increment the id count
-  $id_count ++; 
-  
-  return implode("\n", $html);
+  $id_count++;
+
+  // create the html output
+  return "<div id=\"$div_id\" $class>". $base_params['no_flash'] ."</div>\n";
 }
