var elevationVideoEmbed = {
	isipad: /ipad/gi.test(navigator.userAgent),
	isiphone: /iphone/gi.test(navigator.userAgent),
	isiphone4: (function() {
		var bool = false;
		if ($('<div id="iphone4bug"/>').appendTo('body').css('display') == 'inline') {bool = true;}
		$('#iphone4bug').remove();
		return bool;
	})(),
	isipod: /ipod/gi.test(navigator.userAgent),
	
	init: function(objID, width, height, file, duration, autoplay) {
		var self = this;
		var flashfunction = function() {self.flashDetect(objID, width, height, file, duration, autoplay, self);}
		
		if (!Modernizr.video.h264 || self.isipad || self.isiphone || self.isipod || !/\.mp4/gi.test(file)) {
			if (!self.isipad && !self.isiphone && !self.isipod) {
				if (typeof DetectFlashVer == 'function') {
					flashfunction();
				} else {
					$.ajax({ type: "GET", url: "/scripts/jwplayer/AC_OETags.js", dataType: "script", cache: true, success: function(data, textStatus) {
							if (textStatus == 'success') {
								flashfunction();
							}
						}
					});
				}
			} else if ((self.isipad || self.isiphone || self.isipod) && !/\.mp4/gi.test(file)) {
				//iOS with an .flv
				var alternateContent = '<p style="width:' + width + 'px; height:' + height + 'px; background-color:#000; color:#fff; padding:15px; font-size:2em; line-height:1.5em;">We\'re sorry, but this content isn\'t compatible with your mobile device. Please <a href="http://www.elevationchurch.org/feed/podcast" target="_blank">check out our Podcast</a> for access to all of our archived audio content.</p>';
				$('#' + objID).replaceWith(alternateContent).css('visibility','visible');  // insert non-flash content
			} else if ((self.isiphone || self.isipad) && !self.isiphone4) {
				if ($('#' + objID + ' source:nth-child(2)').length) { $('#' + objID + ' source:nth-child(1)').hide(); }
			} else {
				self.fireJWPlayer(objID, width, height, file, duration, autoplay);
			}
		} else {
			$('#' + objID).css('visibility','visible');
		}
	},
	
	flashDetect: function(objID, width, height, file, duration, autoplay, self) {
						
		if (typeof DetectFlashVer !== 'function') {
			setTimeout(function() {self.flashDetect(objID, width, height, file, duration, autoplay, self)},100);
			return;
		}
		
		// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
		var hasProductInstall = DetectFlashVer(6, 0, 65);
		// Version check for the video player
		var hasReqestedVersion = DetectFlashVer(10, 0, 0);
		
		// Check to see if a player with Flash Product Install is available and the version does not meet the requirements for playback
		if ( hasProductInstall && !hasReqestedVersion ) {
			// MMdoctitle is the stored document.title value used by the installation process to close the window that started the process
			// This is necessary in order to close browser windows that are still utilizing the older version of the player after installation has completed
			// DO NOT MODIFY THE FOLLOWING FOUR LINES
			// Location visited after installation is complete if installation is required
			var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
			var MMredirectURL = window.location;
			document.title = document.title.slice(0, 47) + " - Flash Player Installation";
			var MMdoctitle = document.title;
		
			$('#' + objID).replaceWith(
				AC_FL_RunContent(
					"src", "/scripts/jwplayer/playerProductInstall",
					"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
					"width", width,
					"height", height,
					"align", "middle",
					"id", "flashExpressInstall",
					"quality", "high",
					"bgcolor", "#000000",
					"name", "flashExpressInstall",
					"allowScriptAccess","always",
					"type", "application/x-shockwave-flash",
					"pluginspage", "http://www.adobe.com/go/getflashplayer"
				)
			);
		} else if (hasReqestedVersion) {
			// if we've detected an acceptable version
			self.fireJWPlayer(objID, width, height, file, duration, autoplay);
		} else {
			// flash is too old or we can't detect the plugin
			var alternateContent = 'This video player requires Adobe Flash Player 10 or greater.<br/>'
			+ '<a href=http://www.adobe.com/go/getflash/ target="_blank">Please click here to install Flash.</a>';
			$('#' + objID).html(alternateContent);  // insert non-flash content
		}
	},
	
	fireJWPlayer: function(objID, width, height, file, duration, autoplay) {
		var self = this;
		var embedstreamer = !(/multicastmedia\.com/i.test(file)) || $.browser.webkit ? '' : 'rtmp://cp23245.edgefcs.net/ondemand/';
		var embedfile = $.browser.webkit ? file : /\.mp4/gi.test(file) ? "mp4:23245" + file.replace('http://vidego-http.multicastmedia.com', '') : /multicastmedia\.com/gi.test(file) ? "23245" + file.replace('http://vidego-http.multicastmedia.com', '') : file;
		var embedimage = /multicastmedia\.com/gi.test(file) && !autoplay ? 'http://mediasuite.multicastmedia.com/thumb.php?' + file.substr(file.indexOf('aid=')) + '&w=' + width + '&h=' + height : '';
		var jwembed = function() {
			jwplayer(objID).setup({
				    streamer: embedstreamer,
					file: embedfile,
				    flashplayer: "/scripts/jwplayer/player.swf",
					width: width,
					height: height,
					image: embedimage,
					duration: duration,
					autostart: autoplay ? !$.browser.msie : false
				});
				setTimeout(function() {$('#' + objID).css('visibility','visible');},250);
			};
		
		if (typeof jwplayer == 'function') {
			jwembed();
		} else {
			$.ajax({ type: "GET", url: "/scripts/jwplayer/jwplayer.js", dataType: "script", cache: true, success: function(data, textStatus) {
					jwembed();
				}
			});
		}
	}
}

