// Use global value so player detection occurs only once per page refresh
var hasRightVersion = false;


/** @description Bind the event handler to the movie clip links
 * @param {String} clipLinksId id of element (list or div) enclosing the links
 * @param {String} swf full url to the swf
 * @param {String} statusline string to show in status bar when mousing over the links; clip number is appended
 * @param {String|Array} transcript (optional) path or array of paths to transcripts (some video clip sets will share a single transcript,
 * others will have a separate transcript for each clip
 * @param {String} className (optional)
 */
function attachClipLinkEvents(clipLinksId, swf, className, transcript, autoplay, videoWidth, videoHeight) {

	var node = document.getElementById(clipLinksId), 
		clip;
		
	clip = 1;
	playMovieClip(clip, swf, clip.transcript, videoWidth, videoHeight);
}

// Use this function to wrap attachClipLinkEvents, because the latter has three optional arguments.
function bindClipLinkEvents(args) {
	attachClipLinkEvents(args.clipLinksId,
					   args.swf,
					   args.className || "",
					   args.transcript || "",
					   args.autoplay || false, 
					   args.videoWidth || 320,
					   args.videoHeight || 240
					 )
}

// Play a Flash 8 video clip
// Arguments: 
// clip = clip number to play
// swf = full url to swf file
// transcript = transcript file to point to in case Flash Player is not installed
// Assumptions:
// * The page contains a div with id "video" that will enclose the video screen, replacing any original content
// * The page contains a div with id "getflash" enclosing the Adobe get flash link. It will be replaced by 
// a message including the get flash link in case the required version of flash is not detected.
// * The flv file are stored in the same directory as the swf and named: 1.flv
// * Other parameters of the video object (dimensions, looping, etc.) have been hard-coded,
// but could be parameterized.
// Styles for the player are defined in /style/flashvideoplayer.css and media.css

function playMovieClip(clip, swf, transcript, videoWidth, videoHeight) {
	
	var video = document.getElementById("video"), 
		intro = document.getElementById("intro"),
		htmlstring;

	// Hide the original text
	// Use 2 different divs rather than replacing the innerHTML of the original div: this allows
	// us to declare styles in a stylesheet for the video div without affecting the intro div.
	if (intro) {
		intro.className = "hidden";
	}
		
	// Check for required version of Flash Player
	// RY 7/19/06 Made hasRightVersion global, so it only gets tested once per page refresh
	if (!hasRightVersion) { 
		hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	}

	// An acceptable version has been detected
	if (hasRightVersion) { 
		
		video.innerHTML =
		
			'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' 
			+ ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"'
			+ ' width="' + videoWidth + '" height="' + videoHeight + '" align="middle">' 
			+ '<param name="allowScriptAccess" value="sameDomain" />' 
			+ '<param name="movie" value="' + swf + '?flv=' + clip + '">' 
			+ '<param name="loop" value="false" />' 
			+ '<param name="quality" value="high" />'
			+ '<param name="bgcolor" value="#000000" />'
			+ '<embed src="' + swf + '?flv=' + clip + '"' 
			+ ' play="true" loop="false" quality="high" bgcolor="#000000" width="' + videoWidth + '" height="' + videoHeight + '"'
			+ ' align="middle" type="application/x-shockwave-flash"'
			+ ' pluginspage="http://www.macromedia.com/go/getflashplayer" />' 
			+ '</object>'
			;
	}	

	// Flash is too old or we can't detect the plugin
	else { 
		// Need an intermediate variable to build up the string. It's interesting that it doesn't work to put partial, ill-formed
		// html into the video.innerHTML property, as happens if you build up the video.innerHTML value directly in 2-3
		// steps as below.
		htmlstring =
			'<p class="smalltext"><strong>Adobe Flash Player version 8 or above is required to view this video content.'
			+ ' Click on the button below to download the player';
		if (transcript) {
			htmlstring +=
			', or <a href="' + transcript + '"> read the transcript</a>'
			+ '<img src="/images/icons/pdfIcon.gif" class="icon"'
			+ 'style="margin-left: 5px; margin-right: 0px; padding-right: 1px;" /> instead';
		}
		htmlstring +=
			'.</strong></p><div style="text-align: center;">'
			+ '<a class="img" href="http://www.macromedia.com/go/getflashplayer/" target="_blank">'
			+ '<img src="/images/logos/for_link/get_flash_player.gif"'
			+ ' width="88" height="31" border="0" alt="get flash"'
			+ ' style="margin-top: 0; padding-top: 0; margin-bottom: 10px;" /></a>'
			+ '</div>';	
			
		video.innerHTML = htmlstring;
		video.style.textAlign = "left"; // don't center the text
		// suppress the "get flash" logo, since it is displayed as part of the error message
		document.getElementById("getflash").className = "hidden";
	}

}
