(function( $ ){

	var settings = {
		  TrackInterval	: 1000 //milliseconds
		  ,SegmentSize	: 5 //seconds
		  ,Controls	: {}
	};

	var methods = {
		init : function( options ) {

			if ( options ) $.extend( settings, options );

			settings.Player = "FlowPlayer";
			settings.FormatExt = ".mp4";
		  	settings.FlowPlayerUrl = $(this).find(".vjs-flash-fallback").attr("data");

			if (Modernizr.video) {
				switch("maybe") {
					case Modernizr.video.h264:
						settings.FormatExt = ".mp4";
						settings.Player = "VideoJS";
						break;
					case Modernizr.video.webm:
						settings.FormatExt = ".webm";
						settings.Player = "VideoJS";
						break;
					case Modernizr.video.ogg:
						settings.FormatExt = ".ogv";
						settings.Player = "VideoJS";
						break;
				}
				switch("probably") {
					case Modernizr.video.h264:
						settings.FormatExt = ".mp4";
						settings.Player = "VideoJS";
						break;
					case Modernizr.video.webm:
						settings.FormatExt = ".webm";
						settings.Player = "VideoJS";
						break;
					case Modernizr.video.ogg:
						settings.FormatExt = ".ogv";
						settings.Player = "VideoJS";
						break;
				}
			}

			switch( settings.Player ) {
			case "VideoJS":
				setInterval(function(){ $(this).Video("TrackVideoJS"); },settings.TrackInterval);
				return this.each(function(){
					$(this).Video("InitVideoJS");
				});
			case "FlowPlayer":
			default:
				setInterval(function(){ $(this).Video("TrackFlowPlayer"); },settings.TrackInterval);
				return this.each(function(){
					$(this).Video("InitFlowPlayer");
				});
			}
		},
		InitFlowPlayer : function( ) {
			$(this).find(".video-js-box").remove();
			var oFlowPlayer = $(this).find(".flowplayer");
			var strPoster = oFlowPlayer.attr("poster");
			var strVideo = oFlowPlayer.attr("video");

			oPlaylist = [];
			if( strPoster != "" ) {
				oPlaylist.push({
					url: strPoster
				});
			}
			if( strVideo != "" ) {
				oPlaylist.push({
					url: strVideo
					,autoPlay: false
					,autoBuffer: true
					,autoBuffering: true
				});
			}

			$f(
				oFlowPlayer[0]
				,{
					src: settings.FlowPlayerUrl
					,wmode:"transparent"
					,cashebuster: $.browser.msie
				}
				,{
					playlist: oPlaylist
					,clip: {
						onStart: function(clip){
							var v = clip.completeUrl;
							if(v == strPoster) return;
							OmnitureVideo_Start(v);
						}
						,onResume: function(clip){
							var v = clip.completeUrl;
							if(v == strPoster) return;
							OmnitureVideo_Start(v);
						}
						,onBeforeSeek: function(clip,target){
							var v = clip.completeUrl;
							if(v == strPoster) return;
							var t1 = this.getTime();
							var t2 = target;
							t1 = $(this).Video("IntervalTime",t1);
							t2 = $(this).Video("IntervalTime",t2);
							OmnitureVideo_Scrubbed(v,t1+":"+t2);
						}
						,onFinish: function(clip){
							var v = clip.completeUrl;
							if(v == strPoster) return;
							OmnitureVideo_Complete(v);
						}
					}
					,plugins: {
						controls: settings.Controls
					}
				}
			);
		},
		InitVideoJS : function( ) {

			$(this).find(".flowplayer").remove();

			$(this).find("video")
				.VideoJS()
				.bind("ended", function(){
					var v = $(this).attr("currentSrc");
					OmnitureVideo_Complete(v);
					$(this).data("SupressNextScrub",1);
				})
				.bind("play", function(){
					var v = $(this).attr("currentSrc");
					OmnitureVideo_Start(v);
				})
				.bind("seeked", function(){
					if( $(this).data("SupressNextScrub") == 1 ) {
						$(this).data("SupressNextScrub",0);
						return;
					}
					var v = $(this).attr("currentSrc");
					var t1 = $(this).data("pCurrentTime");
					var t2 = $(this).attr("currentTime");
					t1 = $(this).Video("IntervalTime",t1);
					t2 = $(this).Video("IntervalTime",t2);
					OmnitureVideo_Scrubbed(v,t1+":"+t2);
				})
				;

		},
		TrackFlowPlayer : function( ) {
			$f("*").each(function() {
				if( this.getState() == 3 ) {
					var c = this.getClip();
					var v = c.url;
					var t = this.getTime();
					t = $(this).Video("IntervalTime",t);
					OmnitureVideo_Viewed(v,t);
				}
			});
		},
		TrackVideoJS : function( ) {
			$("video").each(function(){
				if( !$(this).attr("paused") ) {
					var v = $(this).attr("currentSrc");
					var t = $(this).attr("currentTime");
					var ti = $(this).Video("IntervalTime",t);
					OmnitureVideo_Viewed(v,ti);
					$(this).data("pCurrentTime", t );
				}
			});
		},
		IntervalTime : function( value ) {
			var val = Math.floor(value);
			var t1 = Math.floor( value / settings.SegmentSize ) * settings.SegmentSize;
			var t2 = t1 + settings.SegmentSize;
			var ret = t1.toString() + "-" + t2.toString();
			return ret;
		},
		Pause : function() {
			switch( settings.Player ) {
			case "VideoJS":
				return this.each(function(){
					$(this).find("video")[0].pause();
				});
			case "FlowPlayer":
			default:
				return this.each(function(){
					$f("*").each(function(){
						this.pause();
					});
				});
			}
		},
		Play : function( strVideo , strPoster ) {
			switch( settings.Player ) {
			case "VideoJS":
				return this.each(function(){
					var v = $(this).find("video")[0];
					v.src = strVideo;
					v.poster = strPoster;
					v.load();
					v.play();
				});
			case "FlowPlayer":
			default:
				return this.each(function(){
					$f($(this).find(".flowplayer")[0]).play(strVideo);
				});
			}
		},
		Settings : function() {
			return settings;
		}

	};

	$.fn.Video = function( method ) {
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.Video' );
		}
	};

})( jQuery );
