var MSG_EvenArgs = 'The %s function requires an even number of arguments.' + '\nArguments should be in the form "atttributeName","attributeValue",...';
var MSG_SrcRequired = "The %s function requires that a movie src be passed in as one of the arguments.";

// 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.
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;
			}
		}
	}
}

// Builds the codebase value to use. If the 'codebase' parameter is found in the args,
// uses its value as the version for the baseURL. If 'codebase' is not found in the args,
// uses the defaultVersion.
getCodebase = function(baseURL, defaultVersion, args) {
	var codebase = baseURL + defaultVersion;
	for (var i=0; i < args.length; i=i+2) {
		currArg = args[i].toLowerCase();
		if (currArg == "codebase" && args.length > i+1) {
			if ((args[i+1].indexOf("https://") == 0)||(args[i+1].indexOf("http://") == 0)) {
				// User passed in a full codebase, so use it.
				codebase = args[i+1];
			} else {
				codebase = baseURL + args[i+1];
			}
		}
	}
	return codebase;
}

// Substitutes values for %s in a string.
// Usage: sprintfSubs("The %s function requires %s arguments.","foo()","4");
sprintfSubs = 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.
checkArgs = function(args,callingFn) {
	var retVal = true;
	// If number of arguments isn't even, show a warning and return false.
	if (parseFloat(args.length/2) != parseInt(args.length/2)) {
		alert(sprintf(MSG_EvenArgs,callingFn));
		retVal = false;
	}
	return retVal;
}
	
generateObj = function(callingFn, useXHTML, classid, codebase, pluginsPage, mimeType, args) {
	if (!checkArgs(args,callingFn)) {
		return;
	}
	// Initialize variables
	var tagStr = '';
	var currArg = '';
	var closer = (useXHTML) ? '/>' : '>';
	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();
		switch (currArg) {
			case "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;
				}
				break;
			case "movie":
				if (!srcFound) {
					paramStr += '<param name="' + args[i] + '" value="' + args[i+1];
					var initVar = true;
					for (var j=0; j < args.length; j=j+2) {
						if (args[j] == "var") {
							if (initVar) {
								paramStr += '?' + args[j+1];
								initVar = false;
							} else {
								paramStr += '&' + args[j+1];
							}
						}
					}
					paramStr += '"' + closer + '\n';
					embedStr += ' src="' + args[i+1];
					var initVar = true;
					for (var j=0; j < args.length; j=j+2) {
						if (args[j] == "var") {
							if (initVar) {
								embedStr += '?' + args[j+1];
								initVar = false;
							} else {
								embedStr += '&' + args[j+1];
							}
						}
					}
					embedStr +=  '"';
					srcFound = true;
				}
				break;
			case "width": case "height": case "align": case "vspace": case "hspace": case "class": case "title": case "accesskey": case "tabindex":
				objStr += ' ' + args[i] + '="' + args[i+1] + '"';
				embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
				break;
			case "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] + '"';
				break;
			case "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] + '"';
				break;
			default:
				// This is an attribute we don't know about. Assume that we should add it to the
				// param and embed strings.
				if (args[i] != "var") {
					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(sprintfSubs(MSG_SrcRequired,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(tagStr);
}

runFlContent = function() {
	// First, look for a "movie" and "src" params, and if either exists, add a ".swf" to the end
	// if it doesn't already have one (this function will only run swf files)
	addExtension(arguments, "movie", ".swf");
	addExtension(arguments, "src", ".swf");
	// Build the codebase value. If user passed in a version for the codebase, add the version
	// to the base codebase url. Otherwise, use the default version.
	var codebase = getCodebase("https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=", "8,0,0,0", arguments);
	generateObj("runFlContent()", true, "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", codebase, "https://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash", "application/x-shockwave-flash", arguments);
}