1 /** The minplayer namespace. */
  2 var minplayer = minplayer || {};
  3 
  4 /** All the media player implementations */
  5 minplayer.players = minplayer.players || {};
  6 
  7 /**
  8  * @constructor
  9  * @extends minplayer.players.base
 10  * @class The vimeo media player.
 11  *
 12  * @param {object} context The jQuery context.
 13  * @param {object} options This components options.
 14  */
 15 minplayer.players.vimeo = function(context, options) {
 16 
 17   // Derive from players base.
 18   minplayer.players.base.call(this, context, options);
 19 };
 20 
 21 /** Derive from minplayer.players.base. */
 22 minplayer.players.vimeo.prototype = new minplayer.players.base();
 23 
 24 /** Reset the constructor. */
 25 minplayer.players.vimeo.prototype.constructor = minplayer.players.vimeo;
 26 
 27 /**
 28  * @see minplayer.players.base#getPriority
 29  * @return {number} The priority of this media player.
 30  */
 31 minplayer.players.vimeo.getPriority = function() {
 32   return 10;
 33 };
 34 
 35 /**
 36  * @see minplayer.players.base#canPlay
 37  * @return {boolean} If this player can play this media type.
 38  */
 39 minplayer.players.vimeo.canPlay = function(file) {
 40 
 41   // Check for the mimetype for vimeo.
 42   if (file.mimetype === 'video/vimeo') {
 43     return true;
 44   }
 45 
 46   // If the path is a vimeo path, then return true.
 47   return (file.path.search(/^http(s)?\:\/\/(www\.)?vimeo\.com/i) === 0);
 48 };
 49 
 50 /**
 51  * Return the ID for a provided media file.
 52  *
 53  * @param {object} file A {@link minplayer.file} object.
 54  * @return {string} The ID for the provided media.
 55  */
 56 minplayer.players.vimeo.getMediaId = function(file) {
 57   var regex = /^http[s]?\:\/\/(www\.)?vimeo\.com\/(\?v\=)?([0-9]+)/i;
 58   if (file.path.search(regex) === 0) {
 59     return file.path.replace(regex, '$3');
 60   }
 61   else {
 62     return file.path;
 63   }
 64 };
 65 
 66 /**
 67  * @see minplayer.players.base#reset
 68  */
 69 minplayer.players.vimeo.prototype.reset = function() {
 70 
 71   // Reset the flash variables..
 72   minplayer.players.base.prototype.reset.call(this);
 73 };
 74 
 75 /**
 76  * @see minplayer.players.base#create
 77  * @return {object} The media player entity.
 78  */
 79 minplayer.players.vimeo.prototype.create = function() {
 80   minplayer.players.base.prototype.create.call(this);
 81 
 82   // Insert the Vimeo Froogaloop player.
 83   var tag = document.createElement('script');
 84   tag.src = 'http://a.vimeocdn.com/js/froogaloop2.min.js';
 85   var firstScriptTag = document.getElementsByTagName('script')[0];
 86   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
 87 
 88   // Create the iframe for this player.
 89   var iframe = document.createElement('iframe');
 90   iframe.setAttribute('id', this.options.id + '-player');
 91   iframe.setAttribute('type', 'text/html');
 92   iframe.setAttribute('width', '100%');
 93   iframe.setAttribute('height', '100%');
 94   iframe.setAttribute('frameborder', '0');
 95 
 96   // Get the source.
 97   var src = 'http://player.vimeo.com/video/';
 98   src += this.mediaFile.id + '?';
 99 
100   // Add the parameters to the src.
101   src += jQuery.param({
102     'wmode': 'opaque',
103     'api': 1,
104     'player_id': this.options.id + '-player',
105     'title': 0,
106     'byline': 0,
107     'portrait': 0,
108     'autoplay': this.options.autoplay,
109     'loop': this.options.loop
110   });
111 
112   // Set the source of the iframe.
113   iframe.setAttribute('src', src);
114 
115   // Now register this player when the froogaloop code is loaded.
116   var _this = this;
117   setTimeout(function check() {
118     if (window.Froogaloop) {
119       _this.player = window.Froogaloop(iframe);
120       _this.player.addEvent('ready', function() {
121         _this.onReady();
122       });
123     }
124     else {
125       setTimeout(check, 200);
126     }
127   }, 200);
128 
129   // Trigger that the load has started.
130   this.trigger('loadstart');
131 
132   // Return the player.
133   return iframe;
134 };
135 
136 /**
137  * @see minplayer.players.base#onReady
138  */
139 minplayer.players.vimeo.prototype.onReady = function(player_id) {
140   // Store the this pointer within this context.
141   var _this = this;
142 
143   // Add the other listeners.
144   this.player.addEvent('loadProgress', function(progress) {
145 
146     // Set the duration, bytesLoaded, and bytesTotal.
147     _this.duration.set(parseFloat(progress.duration));
148     _this.bytesLoaded.set(progress.bytesLoaded);
149     _this.bytesTotal.set(progress.bytesTotal);
150   });
151 
152   this.player.addEvent('playProgress', function(progress) {
153 
154     // Set the duration and current time.
155     _this.duration.set(parseFloat(progress.duration));
156     _this.currentTime.set(parseFloat(progress.seconds));
157   });
158 
159   this.player.addEvent('play', function() {
160     _this.onPlaying();
161   });
162 
163   this.player.addEvent('pause', function() {
164     _this.onPaused();
165   });
166 
167   this.player.addEvent('finish', function() {
168     _this.onComplete();
169   });
170 
171   minplayer.players.base.prototype.onReady.call(this);
172   this.onLoaded();
173 };
174 
175 /**
176  * Checks to see if this player can be found.
177  * @return {bool} TRUE - Player is found, FALSE - otherwise.
178  */
179 minplayer.players.vimeo.prototype.playerFound = function() {
180   var iframe = this.display.find('iframe#' + this.options.id + '-player');
181   return (iframe.length > 0);
182 };
183 
184 /**
185  * @see minplayer.players.base#play
186  */
187 minplayer.players.vimeo.prototype.play = function() {
188   minplayer.players.base.prototype.play.call(this);
189   if (this.isReady()) {
190     this.player.api('play');
191   }
192 };
193 
194 /**
195  * @see minplayer.players.base#pause
196  */
197 minplayer.players.vimeo.prototype.pause = function() {
198   minplayer.players.base.prototype.pause.call(this);
199   if (this.isReady()) {
200     this.player.api('pause');
201   }
202 };
203 
204 /**
205  * @see minplayer.players.base#stop
206  */
207 minplayer.players.vimeo.prototype.stop = function() {
208   minplayer.players.base.prototype.stop.call(this);
209   if (this.isReady()) {
210     this.player.api('unload');
211   }
212 };
213 
214 /**
215  * @see minplayer.players.base#seek
216  */
217 minplayer.players.vimeo.prototype.seek = function(pos) {
218   minplayer.players.base.prototype.seek.call(this, pos);
219   if (this.isReady()) {
220     this.player.api('seekTo', pos);
221   }
222 };
223 
224 /**
225  * @see minplayer.players.base#setVolume
226  */
227 minplayer.players.vimeo.prototype.setVolume = function(vol) {
228   minplayer.players.base.prototype.setVolume.call(this, vol);
229   if (this.isReady()) {
230     this.volume.set(vol);
231     this.player.api('setVolume', vol);
232   }
233 };
234 
235 /**
236  * @see minplayer.players.base#getVolume
237  */
238 minplayer.players.vimeo.prototype.getVolume = function(callback) {
239   var _this = this;
240   this.player.api('getVolume', function(vol) {
241     callback(vol);
242   });
243 };
244 
245 /**
246  * @see minplayer.players.base#getDuration.
247  */
248 minplayer.players.vimeo.prototype.getDuration = function(callback) {
249   if (this.isReady()) {
250     if (this.duration.value) {
251       callback(this.duration.value);
252     }
253     else {
254       this.player.api('getDuration', function(duration) {
255         callback(duration);
256       });
257     }
258   }
259 };
260