var fpvideos = [];

jQuery.fn.fpvideo = function(options) {
	// =========================================================
	// == Settings =============================================
	// =========================================================
	var settings = jQuery.extend({
		videoAttribute: "video",
		linkAttribute: 	"linkTo",
		ipadAttribute: 	"ipad",
		flowplayerPath: "resources/flowplayer/",
		autoplay: 		false,
		showControls: 	true,
		showPlayAgain: 	true,
		rtmpUrl: 		"",
		ipadUrl: 		"",
		onLoad: 		function() {},
		onFinish: 		function() {},
		onPause: 		function() {},
		onResume: 		function() {},
		testAnalytics: 	false,
		testipad: 		false
	}, options);
	
	settings.streaming = (settings.rtmpUrl.length > 0);
	
	// =========================================================
	// == Initialization =======================================
	// =========================================================
	this.each(function(){
		if(this.id == "")
			this.id = new Date().getTime();
			
		if(fpvideos[this.id] != null)
			return;
		
		var $this = jQuery(this);
		
		// == Video Urls =======================================
		// =====================================================
		var videoProvider 	= (settings.streaming) ? "rtmp" : "http";
		var videoUrl 		= $this.attr(settings.videoAttribute);
		var linkUrl 		= $this.attr(settings.linkAttribute) || null;
		var linkTarget 		= $this.attr("target") || "_self";
		var ipadUrl			= settings.ipadUrl + ($this.attr(settings.ipadAttribute) || $this.attr(settings.videoAttribute));
		
		if(settings.streaming)
			videoUrl = "mp4:" + videoUrl;
		if(linkUrl != null)
			linkUrl = linkUrl.replace(/&/g, '%26') 
			
		// == Analytics ========================================
		// =====================================================
		var analytics = null;
		if(jQuery.analytics != null) {
			var analyticsID = jQuery.analytics().getAccount();
			if(analyticsID != "") {
				analytics = {
					url: 		settings.flowplayerPath + "flowplayer.analytics-3.2.2.swf",
					debug: 		settings.testAnalytics,
					accountId: 	analyticsID
				}
			}
		}
		
		// == Controls =========================================
		// =====================================================
		var controls = null;
		settings.onBeforeFinish = null;
		if(settings.showControls)
			controls = { }
		if(!settings.showPlayAgain)
			settings.onBeforeFinish = function() { this.getPlugin("play").css({opacity: 0}); }
			
		// Store the video in the array
		fpvideos[this.id] = $f(
			this.id, { 
				src: settings.flowplayerPath + "flowplayer-3.2.7.swf", 
				wmode: 'opaque'
			}, {
				plugins: {
					controls: controls,
					rtmp: {
						url: settings.flowplayerPath + "flowplayer.rtmp-3.2.3.swf",
						netConnectionUrl: settings.rtmpUrl
					},
					gatracker: analytics
				},
				clip: {
					ipadUrl: 		ipadUrl,
					url: 			videoUrl,
					linkUrl: 		linkUrl,
					linkWindow: 	linkTarget,
					provider: 		videoProvider,
					autoPlay: 		settings.autoplay,
					autoBuffering: 	true,
					eventCategory: 	"Video",
					onFinish: 		settings.onFinish,
					onPause: 		settings.onPause,
					onBegin: 		settings.onResume,
					onResume: 		settings.onResume,
					onBeforeFinish: settings.onBeforeFinish
				},
				onLoad: settings.onLoad
			}			
		)
				
		// == iPad Setup =======================================
		// =====================================================
		if(fpvideos[this.id].ipad != null)
			fpvideos[this.id].ipad({ simulateiDevice: settings.testipad });
	})
	
	return this;
}

// == Play =============================================
// =====================================================
jQuery.fn.play = function() {
	this.each(function(){
		if(fpvideos[this.id] == null)
			return;

		fpvideos[this.id].play();
	});
	
	return this;
}

// == Pause ============================================
// =====================================================
jQuery.fn.pause = function() {
	this.each(function(){
		if(fpvideos[this.id] == null)
			return;
			
		fpvideos[this.id].pause();
	});
	
	return this;
}

// == Stop =============================================
// =====================================================
jQuery.fn.stop = function() {
	this.each(function(){
		if(fpvideos[this.id] == null)
			return;
			
		fpvideos[this.id].stop();
	});
	
	return this;
}

jQuery.fn.resetVideo = function() {
	this.each(function(){
		if(fpvideos[this.id] == null)
			return;
			
		fpvideos[this.id].unload();
	});
	
	return this;
}
