1 // Add a way to instanciate using jQuery prototype.
  2 if (!jQuery.fn.osmplayer) {
  3 
  4   /**
  5    * @constructor
  6    *
  7    * Define a jQuery osmplayer prototype.
  8    *
  9    * @param {object} options The options for this jQuery prototype.
 10    * @return {Array} jQuery object.
 11    */
 12   jQuery.fn.osmplayer = function(options) {
 13     return jQuery(this).each(function() {
 14       options = options || {};
 15       options.id = options.id || jQuery(this).attr('id') || Math.random();
 16       if (!minplayer.plugins[options.id]) {
 17         options.template = options.template || 'default';
 18         if (osmplayer[options.template]) {
 19           new osmplayer[options.template](jQuery(this), options);
 20         }
 21         else {
 22           new osmplayer(jQuery(this), options);
 23         }
 24       }
 25     });
 26   };
 27 }
 28 
 29 /**
 30  * @constructor
 31  * @extends minplayer
 32  * @class The main osmplayer class.
 33  *
 34  * <p><strong>Usage:</strong>
 35  * <pre><code>
 36  *
 37  *   // Create a media player.
 38  *   var player = jQuery("#player").osmplayer({
 39  *
 40  *   });
 41  *
 42  * </code></pre>
 43  * </p>
 44  *
 45  * @param {object} context The jQuery context.
 46  * @param {object} options This components options.
 47  */
 48 osmplayer = function(context, options) {
 49 
 50   // Derive from minplayer
 51   minplayer.call(this, context, options);
 52 };
 53 
 54 /** Derive from minplayer. */
 55 osmplayer.prototype = new minplayer();
 56 
 57 /** Reset the constructor. */
 58 osmplayer.prototype.constructor = osmplayer;
 59 
 60 /**
 61  * Creates a new plugin within this context.
 62  *
 63  * @param {string} name The name of the plugin you wish to create.
 64  * @param {object} base The base object for this plugin.
 65  * @param {object} context The context which you would like to create.
 66  * @return {object} The new plugin object.
 67  */
 68 osmplayer.prototype.create = function(name, base, context) {
 69   return minplayer.prototype.create.call(this, name, 'osmplayer', context);
 70 };
 71 
 72 /**
 73  * @see minplayer.plugin.construct
 74  */
 75 osmplayer.prototype.construct = function() {
 76 
 77   // Make sure we provide default options...
 78   this.options = jQuery.extend({
 79     playlist: '',
 80     node: {},
 81     swfplayer: 'minplayer/flash/minplayer.swf',
 82     logo: 'logo.png',
 83     link: 'http://www.mediafront.org'
 84   }, this.options);
 85 
 86   // Call the minplayer display constructor.
 87   minplayer.prototype.construct.call(this);
 88 
 89   /** The play queue and index. */
 90   this.playQueue = [];
 91   this.playIndex = 0;
 92 
 93   /** The playlist for this media player. */
 94   this.create('playlist', 'osmplayer');
 95 
 96   /** Get the playlist or any other playlist that connects. */
 97   this.get('playlist', function(playlist) {
 98     playlist.bind('nodeLoad', (function(player) {
 99       return function(event, data) {
100         player.loadNode(data);
101       };
102     })(this));
103   });
104 
105   // Play each media sequentially...
106   this.get('media', (function(player) {
107     return function(media) {
108       media.bind('ended', function() {
109         player.options.autoplay = true;
110         player.playNext();
111       });
112     };
113   })(this));
114 
115   // Load the node if one is provided.
116   if (this.options.node) {
117     this.loadNode(this.options.node);
118   }
119 };
120 
121 /**
122  * Gets the full screen element.
123  *
124  * @return {object} The element that will go into fullscreen.
125  */
126 osmplayer.prototype.fullScreenElement = function() {
127   return this.elements.minplayer;
128 };
129 
130 /**
131  * The load node function.
132  *
133  * @param {object} node A media node object.
134  */
135 osmplayer.prototype.loadNode = function(node) {
136   if (node && node.mediafiles) {
137 
138     // Load the media files.
139     var media = node.mediafiles.media;
140     if (media) {
141       this.playQueue.length = 0;
142       this.playQueue = [];
143       this.playIndex = 0;
144       var file = null;
145       var types = [];
146 
147       // For mobile devices, we should only show the main media.
148       if (minplayer.isAndroid || minplayer.isIDevice) {
149         types = ['media'];
150       }
151       else {
152         types = ['intro', 'commercial', 'prereel', 'media', 'postreel'];
153       }
154 
155       // Iterate through the types.
156       jQuery.each(types, (function(player) {
157         return function(key, type) {
158           if (file = player.addToQueue(media[type])) {
159             file.queueType = type;
160           }
161         };
162       })(this));
163     }
164 
165     // Load the preview image.
166     osmplayer.getImage(node.mediafiles, 'preview', (function(player) {
167       return function(image) {
168         player.options.preview = image.path;
169         if (player.playLoader) {
170           player.playLoader.initialize();
171         }
172       };
173     })(this));
174 
175     // Play the next media
176     this.playNext();
177   }
178 };
179 
180 /**
181  * Adds a file to the play queue.
182  *
183  * @param {object} file The file to add to the queue.
184  * @return {object} The file that was added to the queue.
185  */
186 osmplayer.prototype.addToQueue = function(file) {
187   if (file = minplayer.getMediaFile(file)) {
188     this.playQueue.push(file);
189   }
190   return file;
191 };
192 
193 /**
194  * Plays the next media file in the queue.
195  */
196 osmplayer.prototype.playNext = function() {
197   if (this.playQueue.length > this.playIndex) {
198     this.load(this.playQueue[this.playIndex]);
199     this.playIndex++;
200   }
201   else if (this.options.repeat) {
202     this.playIndex = 0;
203     this.playNext();
204   }
205   else if (this.playQueue.length > 0) {
206     // If there is no playlist, and no repeat, we will
207     // just seek to the beginning and pause.
208     this.options.autoplay = false;
209     this.playIndex = 0;
210     this.playNext();
211   }
212   else if (this.media) {
213     // Stop the player and unload.
214     this.media.stop();
215   }
216 };
217 
218 /**
219  * Returns an image provided image array.
220  *
221  * @param {object} mediafiles The mediafiles to search within.
222  * @param {string} type The type of image to look for.
223  * @param {function} callback Called when the image is retrieved.
224  */
225 osmplayer.getImage = function(mediafiles, type, callback) {
226 
227   var image = '';
228   var images = mediafiles.image;
229   if (images) {
230 
231     // If the image type exists, then just use that one...
232     if (images[type]) {
233       image = images[type];
234     }
235     // Or try the original image...
236     else if (images['image']) {
237       image = images['image'];
238     }
239     // Otherwise, just try ANY image...
240     else {
241 
242       // Or, just pick the first one available.
243       for (type in images) {
244         if (images.hasOwnProperty(type)) {
245           image = images[type];
246           break;
247         }
248       }
249     }
250   }
251 
252   // If the image exists, then callback with that image.
253   if (image) {
254     callback(new minplayer.file(image));
255   }
256   else {
257     // Get the image from the media player...
258     var mediaFile = minplayer.getMediaFile(mediafiles.media.media);
259     if (mediaFile) {
260       var player = minplayer.players[mediaFile.player];
261       if (player && (typeof player.getImage === 'function')) {
262         player.getImage(mediaFile, type, function(src) {
263           callback(new minplayer.file(src));
264         });
265       }
266     }
267   }
268 };
269