
	function CFlash() {}

	// Constants
	CFlash.CODEBASE_FLA		= "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0";
	CFlash.CLSID_FLA 		= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	CFlash.PLUGINS_FLA 		=  "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash";
	CFlash.MIMETYPE_FLA 	= "application/x-shockwave-flash";

	CFlash.CODEBASE_SW 		= "http://fpdownload.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,0,0";
	CFlash.CLSID_SW 		= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	CFlash.PLUGINS_SW 		=  "http://www.macromedia.com/shockwave/download/";
	CFlash.MIMETYPE_SW 		= null;

	// Error message constants
	CFlash.MSG_EVEN_ARGS 	= 'The %s function requires an even number of arguments.'
			                 	+ '\nArguments should be in the form "atttributeName","attributeValue",...';
	CFlash.MSG_NO_SOURCE 	= "The %s function requires that a movie src be passed in as one of the arguments.";

	CFlash.DEFAULT_ARGS 	= {
		menu	: "false",
		quality	: "high",
		bgcolor	: "#FFFFFF",
		wmode	: "transparent"
	}
	
	// Finds a parameter with the name paramName, and checks to see if it has the 
	// passed extension. If it doesn't have it, this function adds the extension.
	CFlash.addExtension = function(args, paramName, extension) {
		var currArg, paramVal, queryStr, endStr;
		for(var i=0; i < args.length; i=i+2) {
			currArg = args[i].toLowerCase();    
			if(currArg == paramName.toLowerCase() && args.length > i+1) {
				paramVal = args[i+1];
				queryStr = "";
				
				// Pull off the query string if it exists.
				var indQueryStr = args[i+1].indexOf('?');
				if(indQueryStr != -1) {
					paramVal = args[i+1].substring(0, indQueryStr);
					queryStr = args[i+1].substr(indQueryStr);
				}
				
				endStr = "";
				if(paramVal.length > extension.length)
					endStr = paramVal.substr(paramVal.length - extension.length);
				if(endStr.toLowerCase() != extension.toLowerCase()) {
					// Extension doesn't exist, add it
					args[i+1] = paramVal + extension + queryStr;
				}
			}
		}
	}

	// Substitutes values for %s in a string.
	// Usage: AC_sprintf("The %s function requires %s arguments.","foo()","4");
	CFlash.sprintf = function(str) {
		for (var i=1; i < arguments.length; i++)
			str = str.replace(/%s/,arguments[i]);
		return str;
	}
		
	// Checks that args, the argument list to check, has an even number of 
	// arguments. Alerts the user if an odd number of arguments is found.
	CFlash.checkArgs = function(args,callingFn) {
		// If number of arguments isn't even, show a warning and return false.
		if(parseFloat(args.length/2) != parseInt(args.length/2)) {
			alert(CFlash.sprintf(CFlash.MSG_EVEN_ARGS, callingFn));
			return false;
		}
		return true;
	}
	
	CFlash.generateObj = function(callingFn, classid, codebase, pluginsPage, mimeType, args) {
		if(!CFlash.checkArgs(args, callingFn))
			return;
		
		// Initialize variables
		var tagStr = '';
		var currArg = '';
		var closer = '/>';
		var srcFound = false;
		var embedStr = '<embed';
		var paramStr = '';
		var embedNameAttr = '';
		var objStr = '<object classid="' + classid + '" codebase="' + codebase + '"';
		
		// Spin through all the argument pairs, assigning attributes and values to the object,
		// param, and embed tags as appropriate.
		for (var i=0; i < args.length; i=i+2) {
			currArg = args[i].toLowerCase();    
			
			if(currArg == "src") {
				if(callingFn.indexOf("RunSW") != -1) {
					paramStr += '<param name="' + args[i] + '" value="' + args[i+1] + '"' + closer + '\n';
					embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
					srcFound = true;
				}
				else if(!srcFound) {
					paramStr += '<param name="movie" value="' + args[i+1] + '"' + closer + '\n'; 
					embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
					srcFound = true;
				}
			}
			else if(currArg == "movie") {
				if(!srcFound) {
					paramStr += '<param name="' + args[i] + '" value="' + args[i+1] + '"' + closer + '\n'; 
					embedStr += ' src="' + args[i+1] + '"';
					srcFound = true;
				}
			}
			else if(   currArg == "width" 
					|| currArg == "height" 
					|| currArg == "align" 
					|| currArg == "vspace" 
					|| currArg == "hspace" 
					|| currArg == "class" 
					|| currArg == "title" 
					|| currArg == "accesskey" 
					|| currArg == "tabindex") {
				objStr += ' ' + args[i] + '="' + args[i+1] + '"';
				embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
			}
			else if(currArg == "id") {
				objStr += ' ' + args[i] + '="' + args[i+1] + '"';
				// Only add the name attribute to the embed tag if a name attribute 
				// isn't already there. This is what Dreamweaver does if the user
				// enters a name for a movie in the PI: it adds id to the object
				// tag, and name to the embed tag.
				if(embedNameAttr == "")
					embedNameAttr = ' name="' + args[i+1] + '"';
			}
			else if(currArg == "name"){
				objStr += ' ' + args[i] + '="' + args[i+1] + '"';
				// Replace the current embed tag name attribute with the one passed in.
				embedNameAttr = ' ' + args[i] + '="' + args[i+1] + '"';
			}    
			else if(currArg == "codebase") {
				// The codebase parameter has already been handled, so ignore it. 
			}    
			// This is an attribute we don't know about. Assume that we should add it to the 
			// param and embed strings.
			else {
				paramStr += '<param name="' + args[i] + '" value="' + args[i+1] + '"' + closer + '\n'; 
				embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
			}
		}
		
		// Tell the user that a movie/src is required, if one was not passed in.
		if(!srcFound) {
			alert(CFlash.sprintf(CFlash.MSG_NO_SOURCE, callingFn));
			return;
		}
		
		if(embedNameAttr)
			embedStr += embedNameAttr;	
		if(pluginsPage)
			embedStr += ' pluginspage="' + pluginsPage + '"';
		if(mimeType)
			embedStr += ' type="' + mimeType + '"';
		
		// Close off the object and embed strings
		objStr += '>\n';
		embedStr += '></embed>\n'; 
		
		// Assemble the three tag strings into a single string.
		tagStr = objStr + paramStr + embedStr + "</object>\n"; 
		
		//document.write("<textarea cols=80 rows=15>"+tagStr+"</textarea>");
		document.write(tagStr);
	}

	// Run flash movie
	CFlash.runFlash = function() {
		CFlash.addExtension(arguments, "movie", ".swf");
		CFlash.addExtension(arguments, "src", ".swf");
		CFlash.generateObj("CFlash.runFlash()", CFlash.CLSID_FLA,	CFlash.CODEBASE_FLA, 
				CFlash.PLUGINS_FLA, CFlash.MIMETYPE_FLA, arguments);
	}

	// Run director movie
	CFlash.runDirector = function() {
		CFlash.addExtension(arguments, "src", ".dcr");
		CFlash.generateObj("CFlash.runDirector()", CFlash.CLSID_SW,	CFlash.CODEBASE_SW,	
			CFlash.PLUGINS_SW, CFlash.MIMETYPE_SW, arguments);
	}
