/**
 * 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" : [
						{"url" : "mp4:"+file+"_300.m4v", "bitrate" : 300},
						{"url" : "mp4:"+file+"_1500.m4v", "bitrate" : 1500, "isDefault" : true}
					],
					"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"
				},
	/*			logo: {
					url: XOOPS.path.theme + '/images/flowplayer-watermark-' + XOOPS.theme_class + '.png',
					top: 20,
					right: 20,
					opacity: 0.2,
					fullscreenOnly: false,
					displayTime: 0
				},
	*/			"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/Fyn",
								"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;",
							"facebook" : true,
							"twitter" : true,
							"myspace" : false,
							"stumbleupon" : false,
							"livespaces" : false,
							"digg" : false,
							"orkut" : false,
							"bebo" : false
						},
						"embed" : {
							"title" : "Kopier denne kode til din hjemmeside (<a class=\"terms\" href=\"http://www.tv2fyn.dk/article/226:Om-tv2fyn-dk\" target=\"_blank\">L&aelig;s vilk&aring;r</a>)",
							"options" : "V&aelig;lg st&oslash;rrelse og farver",
							"backgroundColor" : "Baggrundsfarve",
							"buttonColor" : "Knapfarve",
							"size" : "St&oslash;rrelse",
							"copy" : "Kopier",
							"configUrl" : "http://tv2fyn.dk/flowplayer/"+file_id,
							"autoBuffering" : false,
							"autoPlay" : false,
							"anchorText" : ""
						},
						"autoHide" : {
							"hideDelay" : 500
						},
						"canvas" : {
							".terms" : {
								"color" : "#E5F405"
							}
						}
					},
					"controls" : {
						"autoHide" : {
							"enabled" : true,
							"fullscreenOnly": false,
							"hideDelay" : 1000
						}
					},
					"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/tv2fyn_theme/images/black_dot.png)"});

				}
			}, options);
			settings.key = "#@59d0288bf15770064fa"; // Fynskemedier product key 


			// 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(flexblockUniqueId, stream, canvas, flexblockWidth, flexblockHeight) { 
	if (flexblockWidth == undefined) { 
		flexblockWidth = 590;					
	}	
	if (flexblockHeight == undefined) { 
		flexblockHeight = 332;					
	}	
	
	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,
		autoPlay: false,

		bitrates: [
			{url: stream+"_1000", bitrate: 1000, isDefault: true}
		]
	};
	var flowconfig = {
		key: "#@59d0288bf15770064fa", 
		
		clip: clip,

//		log: { level: 'error', filter: 'org.flowplayer.view.Launcher' },
/*
		logo: {
			url: '/themes/tv2fyn_theme/images/watermark.png',
			top: 20,
			right: 20,
			opacity: 0.4,
			fullscreenOnly: false,
			displayTime: 0
		},
*/
		canvas : {			
			backgroundImage : 'url('+canvas+')',
			backgroundGradient : 'none'
		},
		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: flexblockWidth,
				height: flexblockHeight,
				borderRadius: 0,
				backgroundImage: 'url(/themes/tv2fyn_theme/images/no_stream_'+stream+'.jpg)',
				display: 'none',
				opacity: 1
			},
			controls:{
			}
		},
		onError: function (errorCode) {
			if (errorCode == 200 || errorCode == 201) {
				this.getPlugin('errorDisplay').show();
				this.stop();
				disableTimer();
				setTimeout(function () { $f().play(); }, 2000);
			}
		},
		onLoad: function () {
			if (canvas == undefined) { 
				enableTimer();
			}	
		},
		onBeforeBegin: 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-live"+flexblockUniqueId, "/modules/tv2region/video/flowplayer.commercial-3.2.7.swf", flowconfig);
}

