/* ****************************************************

	@file		  ix.video.js
	@version	  1.0
	@description  Lit et contrôle les vidéos H.264 en <video> HTML5 avec un fallback à Flash
	@author		  Rafaël (ixmedia.com)

***************************************************** */

// on détecte le support pour la balise vidéo ET le codec H.264
var fakeVideo = document.createElement('video');
window.HTML5_VIDEO_SUPPORTED = !!fakeVideo.play;
window.HTML5_H264_SUPPORTED	 = window.HTML5_VIDEO_SUPPORTED && fakeVideo.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');

(function($) {
	
	var HTML5_VIDEO_SUPPORTED = window.HTML5_VIDEO_SUPPORTED,
		HTML5_H264_SUPPORTED  = window.HTML5_H264_SUPPORTED;
	
/* Extensions de l’objet jQuery {{{
******************************************************************************/
	
	$.fn.createVideo = function(opts) {
		
		this.html('').append(
			$('<div/>', {
				'class': 'video-js-box vim-css'
			})
		);
		
		if ( HTML5_H264_SUPPORTED ) {
			this.find('.video-js-box').append(
				$('<video/>', {
					'class':  'video-js',
					'width':   opts['width'],
					'height':  opts['height'],
					'poster':  opts['poster'],
					'preload': opts['preload'] || 'auto'
				}).append(
					$('<source/>', {
						'src':	 opts['source'],
						'type': 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
					})
				)
			);
		}
		
		this.initVideo( opts );
	};
	
	$.fn.initVideo = function(opts) {
		
		if (opts == null && !HTML5_H264_SUPPORTED) {
			if ( window.HTML5_VIDEO_SUPPORTED ) {
				this.find('.video-js-box').append( this.find('iframe') );
				this.find('video').remove();
			}
		} else {
			if ( HTML5_H264_SUPPORTED ) {
				VideoJS.setup( this.find('video')[0] );
				
				if ( opts && opts['height'] ) {
					this.find('video').height( opts['height'] );
				}
			} else {
				var flashIframe = document.createElement('iframe');
				
				flashIframe.src	   = '/js/flash-fallback.html?source='+ opts['source'] +'&poster='+ opts['poster'] +'&width='+ opts['width'] +'&height='+ opts['height'] +'';
				flashIframe.width  =  opts['width'];
				flashIframe.height =  opts['height'];
				flashIframe.frameBorder	 =	0;
				flashIframe.scrolling	 = 'no';
				flashIframe.style.border = 'none';
				
				this.find('.video-js-box').append( flashIframe );
				
				$('video', this).remove();
			}
		}
	};
	
	$.fn.removeVideo = function() {
		this.find('.video-js-box').remove();
	};
	
	$.fn.playVideo = function() {
		if ( HTML5_H264_SUPPORTED ) {
			var video;
			if ( video = this.eq(0).find('video')[0] ) {
				video.play();
			}
		} else {
			var iframe;
			if ( iframe = this.find('iframe')[0] ) {
				with (iframe.contentWindow) {
					var flashFallback = $f('flash');
					if ( !flashFallback.isPlaying() ) {
						flashFallback.play();
					}
				}
			}
		}
	};
	
	$.fn.pauseVideo = function() {
		if ( HTML5_H264_SUPPORTED ) {
			var video;
			if ( video = this.eq(0).find('video')[0] ) {
				video.pause();
			}
		} else {
			var iframe;
			if ( iframe = this.find('iframe')[0] ) {
				with (iframe.contentWindow) {
					$f('flash').pause();
				}
			}
		}
	};
	
})(jQuery);
