(function() {
	var detectVideoSupport = function (){
		var detect = document.createElement('video') || false;
		this.html5 = detect && typeof detect.canPlayType !== "undefined";
		this.mp4 = this.html5 && (detect.canPlayType("video/mp4") === "maybe" || detect.canPlayType("video/mp4") === "probably");
		this.ogg = this.html5 && (detect.canPlayType("video/ogg") === "maybe" || detect.canPlayType("video/ogg") === "probably");
		this.mp3 = this.html5 && (detect.canPlayType("audio/mp3") === "maybe" || detect.canPlayType("audio/mp3") === "probably");
		return this;
	};
	 
	var requireVideoViews = function(video) {
		var numberOfViews = 0,
			nextButton = $('#nextSubmit'),
			item = $(video).parents(".tableFormat1"),
			goAhead,
			showNextButton = null;
		
		if(item.hasClass("requireView")) {
			goAhead = item.find('span.goAhead').hide();
			nextButton.css("visibility", "hidden");
			showNextButton = function() {
				if(this.getPlugin) this.getPlugin("play").hide();
				numberOfViews++;
				nextButton.css("visibility", "visible");
				item.find('span.beforeGoAhead').hide();
				goAhead.show();
			};
		}

		return showNextButton;
	};
	 
	var replaceMediaTagWithFlash = function (media){    
		if(media && media.src && (media.width && media.height || media.tagName == "AUDIO")) {

			 p = {},

			 div = $(document.createElement("div")),
			 controlObject = p.media ? {} : { 
				//Settings below used for AUDIO
				fullscreen: false,
				height: 30,
				autoHide: false
			};
			
			var onFinish = requireVideoViews(media);

			p.media = (media.tagName == "VIDEO");
			p.id = media.id;
			p.src = media.src || "";
			p.preload = Boolean(media.attributes["preload"]) || false;
			p.autoplay = Boolean(media.attributes["autoplay"])  || false;

			p.controls = media.attributes["controls"]  ? controlObject : null;
			p.loop = media.attributes["loop"] ? function() {
				this.play(0);
				return false;
			} : null;
			p.height = media.height || !p.controls || "30px"; //30px used for AUDIO
			p.width = media.width || !p.controls || "480px"; //480px used for AUDIO
			$(media).after(div);
			
			div.height(p.height).width(p.width).addClass(media.tagName);
			div.flowplayer("/uiscripts/flowplayer/flowplayer-3.2.2.swf", {
				clip : {
					url: media.src,
					linkUrl:"#",
					autoPlay : p.autoplay,
					autoBuffering : p.preload,
					onBeforeFinish : p.loop,
					onFinish : onFinish
				},
				plugins : {
					controls: p.controls
				}
			});
			$(media).remove();
			div.id = p.id;
		}
	};

	$(document).ready(function() {
		var media = detectVideoSupport();
		//Both Opera and Firefox support OGG but lack MP4 support
		if(!media.mp4){
			$("video").each(function() {
				replaceMediaTagWithFlash(this);
			});
		}
		else {
			$("video").each(function() {
				if(this.autoplay === false && this.controls === false) this.controls = true;
				this.addEventListener("ended", requireVideoViews(this));
			});
		}
		if(!media.mp3){
			$("audio").each(function() {
				replaceMediaTagWithFlash(this);
			});
		}
	 });
 })();
