
/**
 * Flowplay bridge for TV2 Regioner
 *
 * Flowplayer for jQuery bridge for TV2 Regioner
 *
 * @author HAAN <haan@fynskemedier.dk>
 * @author jbsj <jbsj@fynskemedier.dk>
 */
(function($) {
	$.fn.TV2RegionFlowplayer = function(file, file_id, options) {
		$(this).each(function(i) {
			// Define default options
			var settings = $.extend(true, {
				clip : {
					autoPlay : true,
					scaling : "fit",
					provider : 'rtmp',
					urlResolvers : 'bwcheck',
					start : 0,
					duration : 0,
					bitrates : [],
					onStart: function(clip) {

						pageTracker._trackEvent("Videos", "Play", clip.url);
					},
					onPause: function(clip) {
						pageTracker._trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime()));
					},
					onStop: function(clip) {
						pageTracker._trackEvent("Videos", "Stop", clip.url, parseInt(this.getTime()));
					},
					onFinish: function(clip) {
						pageTracker._trackEvent("Videos", "Finish", clip.url);
					}
				},
				play: {
					replayLabel: 'Afspil igen'
				},
				canvas : {
					backgroundImage : 'url('+options.clip.splash_image+')',
					backgroundGradient : 'none'
				},
				plugins : {
					viral: {
						url: '/modules/tv2region/video/flowplayer.viralvideos-3.2.3.swf',
						email: {
							required: ['name','email','to','message','subject'],
							texts: {
								title: 'Send denne video til en ven',
								to: 'Indtast e-mail adresse',
								toSmall: 'adskil adresser med komma',
								message: 'Besked',
								optional: '(valgfri)',
								subject: 'Video fra TV 2 / Nord',
								template: '{0} Videolink: {2}',
								from: 'Dit navn',
								fromAddress: 'Din e-mail',
								Send: 'Send e-mail'
							},
							script: '/modules/tv2region/viral_email.php',
							tokenUrl: '/modules/tv2region/viral_token.php'							
						},
						share: {
							title: 'Del denne video p&aring;',
							description: 'Video fra TV 2 / Nord',							
							facebook: true,
							twitter: true,
							myspace: true,
							stumbleupon: false,
							livespaces: false,
							digg: false,
							orkut: false,
							bebo: false
						},
						embed: {
							title: 'Kopier denne kode til din hjemmeside',
							options: 'V&aelig;lg st&oslash;rrelse og farver',
							backgroundColor: 'Baggrundsfarve',
							buttonColor: 'Knapfarve',
							size: 'St&oslash;rrelse',
							copy: 'Kopier',
							configUrl: 'http://tv2nord.dk/flowplayer/'+file_id,
							autoBuffering: false,
							autoPlay: false							
						}
					},				
					controls:{
						timeColor:'rgba(255, 184, 6, 1)',
						borderRadius:0,
						bufferGradient:'none',
						slowForward:true,
						backgroundColor:'rgba(0, 0, 0, 0.9)',
						volumeSliderGradient:'none',
						slowBackward:false,
						timeBorderRadius:20,
						progressGradient:'none',
						time:true,
						height:30,
						volumeColor:'#ffffff',
						tooltips:{
							marginBottom:5,
							volume:true,
							scrubber:true,
							buttons:false
						},
						fastBackward:false,
						opacity:1,
						timeFontSize:12,
						bufferColor:'rgba(0, 0, 0, 1)',
						volumeSliderColor:'rgba(0, 0, 0, 1)',
						border:'0px',
						buttonColor:'#ffffff',
						mute:true,
						autoHide:{
							enabled:true,
							hideDelay:1077,
							hideStyle:'fade',
							mouseOutDelay:500,
							hideDuration:667,
							fullscreenOnly:false
						},
						backgroundGradient:[
							0,
							0.2,
							0.4
						],
						width:'100pct',
						display:'block',
						sliderBorder:'1px solid rgba(128, 128, 128, 0.7)',
						buttonOverColor:'rgba(255, 184, 6, 1)',
						fullscreen:true,
						timeBgColor:'rgba(0, 0, 0, 1)',
						borderWidth:0,
						scrubberBarHeightRatio:0.2,
						bottom:0,
						stop:true,
						sliderColor:'#000000',
						zIndex:1,
						scrubberHeightRatio:0.7,
						tooltipTextColor:'#ffffff',
						spacing:{
							time:6,
							volume:8,
							all:2
						},
						sliderGradient:'none',
						timeBgHeightRatio:0.7,
						volumeSliderHeightRatio:0.5,
						name:'controls',
						timeSeparator:' ',
						volumeBarHeightRatio:0.2,
						left:'50pct',
						tooltipColor:'#000000',
						playlist:false,
						durationColor:'#a3a3a3',
						play:true,
						fastForward:true,
						timeBorder:'0px solid rgba(0, 0, 0, 0.3)',
						progressColor:'#ffffff',
						scrubber:true,
						volume:true,
						builtIn:false,
						volumeBorder:'1px solid rgba(128, 128, 128, 0.7)',
						margins:[
							2,
							6,
							2,
							12
						]
					},
					bwcheck : {
						url: '/modules/tv2region/video/flowplayer.bwcheck-3.2.3.swf',
						serverType : 'wowza',
						dynamic : true,
						netConnectionUrl : 'rtmp://80.63.11.91:80/bwcheck'
					},
					rtmp : {
						url : '/modules/tv2region/video/flowplayer.rtmp-3.2.3.swf',
						netConnectionUrl : 'rtmp://80.63.11.91:80/vod'
					}
				},
				onFullscreen: function() {
					this.getPlugin("canvas").css({backgroundImage: "url(/themes/tv2nord_theme/images/black_dot.png)"});

				}				
			}, options);
			
			settings.key = "#@5b4729179c82490ead4"; // TV2Nord product key
//			settings.key = "#@330025f202862ba3232"; // Fynskemedier productkey 

			// Define file bitrates
			var bitrates = [
				300,
				1000,
				2000
			];

			// Build bitrates
			for(b in bitrates) {
				var bitrate = bitrates[b];

				settings.clip.bitrates.push({
					url : "mp4:" + file + "_" + bitrate + ".mp4",
					bitrate : bitrate,
					start : 200,
					duration : 60
				});
			}

			// Build player
			$(this).flowplayer("/modules/tv2region/video/flowplayer.commercial-3.2.5.swf", settings);
			$(this).flowplayer(i).load();
		});

		return $(this); // Return all selected elements according to jQuery principals
	}
})(jQuery);

function playLiveVideo(stream) {
	var enableTimer = function () {
		var f = $f();
		if (f.liveTimer) {
			// Timer already set
			return;
		}
		f.liveTimer = setTimeout(function() {
			f.liveTimer = null;
			f.getPlugin('errorDisplay').show();
		}, 5000);
	}
	var disableTimer = function () {
		var f = $f();
		if (f.liveTimer) {
			clearTimeout(f.liveTimer);
			f.liveTimer = null;
		}
	}
	var clip = {
		urlResolvers: 'bwcheck',
		provider: "rtmp",
		scaling: "fit",
		live: true,

		bitrates: [
			{url: stream+"_300", bitrate: 300},
			{url: stream+"_1000", bitrate: 1000, isDefault: true},
			{url: stream+"_2000", bitrate: 2000}
		]
	};
	var flowconfig = {
		key: "#@5b4729179c82490ead4", // TV2Nord product key
	//	key: "#@330025f202862ba3232", // Fynskemedier productkey 
		
		clip: clip,

//		log: { level: 'error', filter: 'org.flowplayer.view.Launcher' },

/*		logo: {
			url: XOOPS.path.theme + '/images/flowplayer-watermark-' + XOOPS.theme_class + '.png',
			top: 20,
			right: 20,
			opacity: 0.4,
			fullscreenOnly: false,
			displayTime: 0
		},
*/		plugins: {
			bwcheck: {
				url: '/modules/tv2region/video/flowplayer.bwcheck-3.2.3.swf',
				serverType: 'wowza',
				checkOnStart: true,
				rememberBitrate: false,
				netConnectionUrl: 'rtmp://80.63.11.91:80/bwcheck'
			},
			rtmp: {
				url: '/modules/tv2region/video/flowplayer.rtmp-3.2.3.swf',
				netConnectionUrl: 'rtmp://80.63.11.91:80/redirect'
			},
			errorDisplay: {
				// location of the plugin
				url: '/modules/tv2region/video/flowplayer.content-3.2.0.swf',

				// display properties
				top: 0,
				width: 705,
				height: 400,
				borderRadius: 0,
				backgroundImage: 'url(/themes/tv2nord_theme/images/no_stream_'+stream+'.jpg)',
				display: 'none',
				opacity: 1
			},
			controls: {
				timeColor:'rgba(255, 184, 6, 1)',
				borderRadius:0,
				bufferGradient:'none',
				slowForward:true,
				backgroundColor:'rgba(0, 0, 0, 0.9)',
				volumeSliderGradient:'none',
				slowBackward:false,
				timeBorderRadius:20,
				progressGradient:'none',
				time:true,
				height:30,
				volumeColor:'#ffffff',
				tooltips:{
					marginBottom:5,
					volume:true,
					scrubber:true,
					buttons:false
				},
				fastBackward:false,
				opacity:1,
				timeFontSize:12,
				bufferColor:'rgba(0, 0, 0, 1)',
				volumeSliderColor:'rgba(0, 0, 0, 1)',
				border:'0px',
				buttonColor:'#ffffff',
				mute:true,
				autoHide:{
					enabled:true,
					hideDelay:1077,
					hideStyle:'fade',
					mouseOutDelay:500,
					hideDuration:667,
					fullscreenOnly:false
				},
				backgroundGradient:[
					0,
					0.2,
					0.4
				],
				width:'100pct',
				display:'block',
				sliderBorder:'1px solid rgba(128, 128, 128, 0.7)',
				buttonOverColor:'rgba(255, 184, 6, 1)',
				fullscreen:true,
				timeBgColor:'rgba(0, 0, 0, 1)',
				borderWidth:0,
				scrubberBarHeightRatio:0.2,
				bottom:0,
				stop:true,
				sliderColor:'#000000',
				zIndex:1,
				scrubberHeightRatio:0.7,
				tooltipTextColor:'#ffffff',
				spacing:{
					time:6,
					volume:8,
					all:2
				},
				sliderGradient:'none',
				timeBgHeightRatio:0.7,
				volumeSliderHeightRatio:0.5,
				name:'controls',
				timeSeparator:' ',
				volumeBarHeightRatio:0.2,
				left:'50pct',
				tooltipColor:'#000000',
				playlist:false,
				durationColor:'#a3a3a3',
				play:true,
				fastForward:true,
				timeBorder:'0px solid rgba(0, 0, 0, 0.3)',
				progressColor:'#ffffff',
				scrubber:true,
				volume:true,
				builtIn:false,
				volumeBorder:'1px solid rgba(128, 128, 128, 0.7)',
				margins:[
					2,
					6,
					2,
					12
				]
			}
		},
		onError: function (errorCode) {
			if (errorCode == 200 || errorCode == 201) {
				this.getPlugin('errorDisplay').show();
				this.stop();
				disableTimer();
				setTimeout(function () { $f().play(); }, 2000);
			}
		},
		onLoad: function () {
			enableTimer();
		},
		onMetaData: function (clip) {
			// Video starting. Cancel timers etc.
			disableTimer();
			this.getPlugin('errorDisplay').hide();
		},
		onBufferEmpty: function () {
			enableTimer();
		},
		onBufferFull: function () {
			disableTimer();
			this.getPlugin('errorDisplay').hide();
		},
		showErrors: false,
		buffering: false
	}
	flowplayer("tv2region-videoplayer", "/modules/tv2region/video/flowplayer.commercial-3.2.5.swf", flowconfig);
}

