// These paths are from the point of view of the HTML
var PATH_TO_FLV_PLAYER   = "/Sites/17/Templates/js/flv-player.swf";
var MIN_FLASH_VERSION = "9.0.0";

// Note: this script expects MooTools and SWFObject to be included already

var ytFavicon = new Image();
ytFavicon.src = "http://www.youtube.com/favicon.ico";
var ytFavicon2 = new Image();
ytFavicon2.src = "http://s.ytimg.com/yt/favicon.ico";

function embedYouTube(options) {
	if (!options.altContentId || !options.youtubePageUrl || !options.altFlvUrl || !options.height || !options.width) { return; }
	
	window.addEvent("domready", function() {
		// Some preparation
		options.minFlash = options.minFlash || MIN_FLASH_VERSION;  // default value
		//options.youtubeSwfUrl = options.youtubeSwfUrl+"&autoplay=1"
		var flashvars = {};
		var params = {};
		var attributes = {};
		params.allowfullscreen = "true";
		params.allowscriptaccess = "always";
		attributes.id = "flashContent";

		$$("#"+options.altContentId+" a", "a#"+options.altContentId)[0].addEvent("click", function() {
			
			// Before embedding the swf, make sure that the page won't juggle around (as a result of removing the video screenshot)
			var container = $(options.altContentId).getParent();
			var containerWidth = container.getSize().y;
			container.setStyle("height", containerWidth+"px");

			if (ytFavicon.height !== 16 || ytFavicon2.height !== 16) {
				// YouTube is blocked, so use video that's hosted somewhere else
				/* flashvars.file = options.altFlvUrl;
				flashvars.duration = options.videoLength;
				flashvars.autostart = true; */
				flashvars.movie = options.altFlvUrl;
				flashvars.bgcolor = "0x000000";
				flashvars.fgcolor = "0xD34242";
				flashvars.autoload = "on";
				flashvars.autorewind = "on";
				flashvars.autoplay = "on";
				flashvars.volume = "65";
				swfobject.embedSWF(PATH_TO_FLV_PLAYER, options.altContentId, options.width, options.height, options.minFlash, false, flashvars, params, attributes);
			} else {
				swfobject.embedSWF(p_videoOnly(options.youtubePageUrl), options.altContentId, options.width, options.height, options.minFlash, false, flashvars, params, attributes);
			}

			if (swfobject.hasFlashPlayerVersion(options.minFlash)) {
				// has Flash installed
				return false; // disable the default behaviour of the link
			}
		});
	});	
}


// returns the URL to view the YouTube video only.  Note that it'll play the high-quality video by default.
function p_videoOnly(pageUrl) {
	return "http://www.youtube.com/v/" + p_parseVideoId(pageUrl) + "&autoplay=1&ap=%2526fmt%3D18";
}

// returns the value of the variable 'v' in the query string
function p_parseVideoId(url) {
	if (url.contains("?")) {
		var startOfQuery = url.lastIndexOf("?") + 1;
		var query = url.substr(startOfQuery);
		var variables = new Array();
		variables = query.split("&");
		
		var s = "v=";
		var videoId = "";
		for (var i=0; i < variables.length; i++) {
			if (variables[i].substr(0, s.length) === s) {
				videoId = variables[i].substr(s.length);
				break;
			}
		}
		return videoId;
	}
	return "";
}