/*
 * RTBF UI Media Video
 *
 * @author 	    Frederic DEWINNE  <fdew@rtbf.be>
 * @copyright   RTBF
 * @version		1.2
 *
 * Depends:
 *	swfobject.js (version 1.4.2)
 *	jquery-1.3.2.js
 */
( function($) {
	var PROP_NAME = 'RTBF_Media_Video';
	function VideoPlayer() {
		this._defaults = {
			swf:				'http://static.infomaniak.ch/livetv/player-v3.1.swf',
			cb:					'http://static.infomaniak.ch/livetv/cb/cb2.swf',
			connector:			'/tv/carousel',
			ajustControlBarSize:'0.5',
			loop:				'0',
			html:				'',
			flashVersion:		'9',
			video:				'',
			showControls:		'1',
			autohideControls:	'1',
			allowfullscreen:	'1',
			uicolor:			'#333333',
			uicolorBtn:			'#34cff8',
			uicolorBtn2:		'#ffffff',
			uialpha:			'80',
			pLogoAnchor:		'topright',
			bufferingtime:		'3',
			swapvol:			'1',
			adHolderUrl:		'',
			pubTxtG:			'Publicité...',
			pubTxtD:			'$s',
			autostart:			'1',
			strechheight:		'0',
			showinfonetlogo:	'0',
			servername:			'rtmp://rtmp.infomaniak.ch',
			directory:			'livecast',
			streamname:			'',
			logoImg:			'',
			preloadimg:			'http://static.infomaniak.ch/rtbf-static/images/player_load.png',
			start:				'0',
			dureetotale:		'0',
			fadeDuration:		'2'
		};
		$.extend(VideoPlayer.prototype,{
			markerClassName : 'hasVideoplayer',
			setDefaults : function(settings) {
				extendRemove(this._defaults, settings || {});
				return this
			},
			_attachVideoplayer : function(target, settings) {
				if (!target.id) {
					target.id = 'vp' + (++this.uuid)
				}
				var container = $(target);
				if (container.hasClass(this.markerClassName)) {
					return
				}
				$('.'+this.markerClassName).RTBF_Media_Video('destroy');
				settings.html = container.html();
				$.data(target, PROP_NAME, settings);
				container.addClass(this.markerClassName);
				this._loadVideoplayer(target, settings);
			},
			_loadVideoplayer : function(target, settings) {
				container = $(target);
				container.html('<div id="swfPlayer_'+(++this.uuid)+'"></div>');
				
				var self = this;
				var swf = (settings ? settings.swf : null) || this._defaults.swf;
				var flashVersion = (settings ? settings.flashVersion : null) || this._defaults.flashVersion;
				var video = (settings ? settings.video : null) || this._defaults.video;
				var streamname = (settings ? settings.streamname : null) || this._defaults.streamname;
				var adHolderUrl = (settings ? settings.adHolderUrl : null) || this._defaults.adHolderUrl;
				
				switch (settings.provider) {
					case 'YOUTUBE':
						params = { allowScriptAccess: "always",
								wmode: 'opaque',
								allowfullscreen: 'true',
								quality: 'high' };
					    attr = { id: 'swfPlayer_'+this.uuid,
					    		 allowFullScreen: "true" };
					    swfobject.embedSWF( video + "?enablejsapi=1&showinfo=0&hd=1&playerapiid=ytplayer" + this.uuid + "&rel=0" 
					    	+ "&autoplay=" + ((settings ? settings.autostart : null) || this._defaults.autostart) 
					    	+ "&fs=" + ((settings ? settings.allowfullscreen : null) || self._defaults.allowfullscreen), 
					    		'swfPlayer_'+self.uuid, container.width(), container.height(), "8", "{RTBF_INFO_STATIC}/flash/expressInstall.swf", null, params, attr);
					    break;
			    	case "DAILYMOTION":
			    		params = { allowScriptAccess: "always",
							wmode: 'opaque',
							allowfullscreen: 'true',
							quality: 'high' };
			    		attr = { id: 'swfPlayer_'+this.uuid,
					    		 allowFullScreen: "true" };
			    		swfobject.embedSWF( video + "&related=0&explicit=0"
			    			+ "&autoplay=" + ((settings ? settings.autostart : null) || this._defaults.autostart), 
			    				'swfPlayer_'+this.uuid, container.width(), container.height(), "8", "{RTBF_INFO_STATIC}/flash/expressInstall.swf", null, params, attr);
						break;
					default:
						flashvars = {
							showControls: (settings ? settings.showControls : null) || this._defaults.showControls,
							autohideControls: (settings ? settings.autohideControls : null) || this._defaults.autohideControls,
							allowfullscreen: (settings ? settings.allowfullscreen : null) || this._defaults.allowfullscreen,
							uicolor: (settings ? settings.uicolor : null) || this._defaults.uicolor,
							uicolorBtn: (settings ? settings.uicolorBtn : null) || this._defaults.uicolorBtn,
							uicolorBtn2: (settings ? settings.uicolorBtn2 : null) || this._defaults.uicolorBtn2,
							uialpha: (settings ? settings.uialpha : null) || this._defaults.uialpha,
							pLogoAnchor: (settings ? settings.pLogoAnchor : null) || this._defaults.pLogoAnchor,
							bufferingtime: (settings ? settings.bufferingtime : null) || this._defaults.bufferingtime,
							swapvol: (settings ? settings.swapvol : null) || this._defaults.swapvol,
							autostart: (settings ? settings.autostart : null) || this._defaults.autostart,
							strechheight: (settings ? settings.strechheight : null) || this._defaults.strechheight,
							showinfonetlogo: (settings ? settings.showinfonetlogo : null) || this._defaults.showinfonetlogo,
							cb: (settings ? settings.cb : null) || this._defaults.cb,
							ajustControlBarSize: (settings ? settings.ajustControlBarSize : null) || this._defaults.ajustControlBarSize,
							loop: (settings ? settings.loop : null) || this._defaults.loop,
							logoImg: (settings ? settings.logoImg : null) || this._defaults.logoImg,
							preloadimg: (settings ? settings.preloadimg : null) || this._defaults.preloadimg
						};
						if (settings && settings.videoDuration!=null && settings.videoDuration!='0.00' && settings.videoDuration.length>0) {
							flashvars.fadeDuration = (settings ? settings.fadeDuration : null) || this._defaults.fadeDuration;
							flashvars.videoDuration = settings.videoDuration;
							if (settings.start!=null) {
								flashvars.start = settings.start;
							}
							if (settings.stop!=null) {
								flashvars.stop = settings.stop;						
							}
							if (flashvars.start==null && flashvars.stop==null)
								settings.dureetotale = "1";
							flashvars.dureetotale= (settings ? settings.dureetotale : null) || this._defaults.dureetotale;
							test = flashvars.videoDuration.split("|");
		                    if (test.length>1) {
		                    	flashvars.sequenceMode = '1';
		                    }
						};
						params = {
							allowscriptaccess: 'sameDomain',
							wmode: 'opaque',
							allowfullscreen: 'true',
							quality: 'high'
						};
						
						if (adHolderUrl!='') {
							flashvars.adHolderUrl = adHolderUrl;
							flashvars.pubTxtG = (settings ? settings.pubTxtG : null) || this._defaults.pubTxtG;
							flashvars.pubTxtD = (settings ? settings.pubTxtD : null) || this._defaults.pubTxtD;
						}
						
						if (streamname=='') {
							swfobject.embedSWF(swf+'?video='+video, 'swfPlayer_'+this.uuid, container.width(), container.height(), flashVersion, "{RTBF_INFO_STATIC}/flash/expressInstall.swf", flashvars, params);
						} else {
							flashvars.streamname = streamname;
							flashvars.servername = (settings ? settings.servername : null) || this._defaults.servername;
							flashvars.directory = (settings ? settings.directory : null) || this._defaults.directory;
							swfobject.embedSWF(swf, 'swfPlayer_'+this.uuid, container.width(), container.height(), flashVersion, "{RTBF_INFO_STATIC}/flash/expressInstall.swf", flashvars, params);
						}
						break;
				}
				
				if (settings && settings.mediaObjectId) {
					$.ajax({
						url: (settings ? settings.connector : null) || this._defaults.connector,
						async: false,
						data: $.jSONToString({
							method: 'playVideo',
							params: [settings.mediaObjectId],
							id: target.id }),
						contentType: 'application/json',
	                    cache: false,
						processData: false,
	                    error: function(){
	                        if (console) {
	                        	console.log('Unable to increment view count !');
	                        }
	                    },
						type: 'POST',
						dataType: 'json'
					});
				}
			},
			_destroyVideoplayer : function(target) {
				container = $(target);
				settings = $.data(target, PROP_NAME);
				if (settings ? settings.html : null) {
					var html = settings.html;
				} else {
					var html = '';
				}
				if (container.hasClass(this.markerClassName)) {
					container.html(html);
					container.removeClass(this.markerClassName);
					$.removeData(target);
				}
			}
		});
	}
	function extendRemove(target, props) {
		$.extend(target, props);
		for ( var name in props) {
			if (props[name] == null || props[name] == undefined) {
				target[name] = props[name]
			}
		}
		return target
	}
	$.fn.RTBF_Media_Video = function(options) {
		return this.each( function() {
			switch (typeof options) {
				case 'string':
					var otherArgs = Array.prototype.slice.call(arguments, 1);
					$.RTBF_Media_Video["_" + options + 'Videoplayer'].apply($.RTBF_Media_Video, [ this ].concat(otherArgs));
				break;
				case 'object':
					$.RTBF_Media_Video._attachVideoplayer(this, options);
				break;
				default:
				break;
			}
		})
	};
	$.RTBF_Media_Video = new VideoPlayer();
	$.RTBF_Media_Video.uuid = new Date().getTime();
	window.DP_jQuery = $
})(jQuery);

