From e8eea58c826d9aa67d08afa37ee18d1eb1a60c79 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 20 Jun 2011 14:29:31 -0400
Subject: [PATCH 1/2] Issue #1194384: Ensure that player IDs are always
 unique.

---
 includes/jplayer.theme.inc |   38 +++++++++++++++++++++++++++++++++++---
 1 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/includes/jplayer.theme.inc b/includes/jplayer.theme.inc
index 3cf8f15..f4f00a5 100644
--- a/includes/jplayer.theme.inc
+++ b/includes/jplayer.theme.inc
@@ -24,7 +24,7 @@ function template_preprocess_jplayer_single(&$vars) {
     'url' => file_create_url($vars['element']['#item']['filepath']),
     'label' => !empty($vars['element']['#item']['data']['description']) ? $vars['element']['#item']['data']['description'] : $vars['element']['#item']['filename'],
   );
-  $vars['player_id'] = 'jplayer-' . $vars['element']['#node']->nid . '-' . str_replace('_', '-', $vars['element']['#field_name']) . '-' . $vars['element']['#item']['#delta'];
+  $vars['player_id'] = _jplayer_check_id('jplayer-' . $vars['element']['#node']->nid . '-' . str_replace('_', '-', $vars['element']['#field_name']) . '-' . $vars['element']['#item']['#delta']);
 
 }
 
@@ -65,7 +65,8 @@ function template_preprocess_jplayer_playlist(&$vars) {
     );
     $number++;
   }
-  $vars['player_id'] = 'jplayer-' . $vars['element']['#node']->nid . '-' . str_replace('_', '-', $vars['element']['#field_name']);
+
+  $vars['player_id'] = _jplayer_check_id('jplayer-' . $vars['element']['#node']->nid . '-' . str_replace('_', '-', $vars['element']['#field_name']));
 }
 
 /**
@@ -92,5 +93,36 @@ function template_preprocess_jplayer_view_playlist(&$vars) {
   }
 
   $vars['mode'] = 'playlist';
-  $vars['player_id'] = 'jplayer-view-' . str_replace('_', '-', $vars['view']->name);
+  $vars['player_id'] = _jplayer_check_id('jplayer-view-' . str_replace('_', '-', $vars['view']->name));
+}
+
+/**
+ * Return a unique ID for a jPlayer. This allows multiple embedded jPlayers to
+ * point to an identical file on the same page.
+ *
+ * @param $id
+ *   The ID to check for uniqueness.
+ *
+ * @return
+ *   A modified ID if the ID was not unique, or the same ID passed in if it was
+ *   unique.
+ */
+function _jplayer_check_id($id) {
+  // We keep track of player IDs generated per request. This ensures that if a
+  // player pointing to the same field is shown multiple times on a page, that
+  // each player gets a unique ID. This is especially a problem when jPlayers
+  // are embedded in hidden content such as View rendered with Quicktabs.
+  static $player_ids = array();
+
+  // Store a count of the number of times a unique ID is used, and make it
+  // unique if needed.
+  if (isset($player_ids[$id])) {
+    $id = $id . '-' . $player_ids[$id]++;
+  }
+  else {
+    $player_ids[$id] = 0;
+  }
+
+  return $id;
 }
+
-- 
1.7.5.4


From 8a15d7b9cf3bb34a9ead74deee9c94219032a861 Mon Sep 17 00:00:00 2001
From: Andrew Berry <deviantintegral@gmail.com>
Date: Mon, 20 Jun 2011 14:55:34 -0400
Subject: [PATCH 2/2] Issue #1194384: Add the request timestamp to the ID to
 ensure uniqueness across AJAX requests.

---
 includes/jplayer.theme.inc |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/includes/jplayer.theme.inc b/includes/jplayer.theme.inc
index f4f00a5..b9d0f4f 100644
--- a/includes/jplayer.theme.inc
+++ b/includes/jplayer.theme.inc
@@ -114,6 +114,10 @@ function _jplayer_check_id($id) {
   // are embedded in hidden content such as View rendered with Quicktabs.
   static $player_ids = array();
 
+  // Add the request time, so if the same player is inserted multiple times
+  // AJAX all players are functional.
+  $id = $id . '-' . $_SERVER['REQUEST_TIME'];
+
   // Store a count of the number of times a unique ID is used, and make it
   // unique if needed.
   if (isset($player_ids[$id])) {
-- 
1.7.5.4

