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