/******************************************************************************

CURRENTLY SUPPORTED MEDIA TYPES

	                   MIME type                       Filename extensions
    ---------------------------------------------------------------------------
	Macromedia Flash   application/x-shockwave-flash   .swf
	Windows Media      application/x-oleobject         .wmv, .wvx, .asf, .asx
	QuickTime                                          .mov

USAGE/SYNOPSIS/EXAMPLE

	<script type="text/javascript">
		insertEmbeddedObject({                // remember the {} inside the ().
			url: "http://www.geappliances.com/products/introductions/bottom_freezers/feature_gallery/interactive_feature_gallery.swf",
			width: 320,
			height: 240,

			// optional id and/or name for the <object> element.
			id: "object1_id",
			name: "object1_name",

			// optional id and/or name for the <embed> element.
			embedId: "embed1_id",
			embedName: "embed1_name",

			// parameters for the movie passed as the <object>'s
			// <param>s and as <embed> tag attributes
			quality: "high",
			bgcolor: "#ffffff",
			menu: "false",

			// optional, passed as attribute to <object> and <embed> tags:
			align: "middle"
		});
	</script>

RARE SITUATIONS

	For those rare situations where URLs with an unrecognized file
	extension, we need to specify one of the listed MIME types as
	well.

	<script type="text/javascript">
		insertEmbeddedObject({
			url: "moo.flash", // not .swf
			type: "application/x-shockwave-flash",
			width: 320,
			height: 240
		});
	</script>

	For those even rarer situations where we need to insert a
	multimedia file whose type isn't even listed, we also need to
	specify classid, codebase, plugisnpage, and (still rarely)
	embedType parameters.  See the next section.

CONVERTING HTML TO insertEmbeddedObject IN A NUTSHELL

This...                                   | Becomes...
__________________________________________|__________________________________________________________
                                          |
                                          |   <script type="text/javascript">
                                          |       insertEmbeddedObject({
<object id="object1_id"                   |_______    id: "object1_id",         // OPTIONAL
        name="object1_name"                       |   name: "object1_name",     // OPTIONAL
        type="application/x-shockwave-flash"      |   type: "...",              // USU. NOT SPECIFIED [1]
        classid="clsid:..."                       |   classid: "...",           // USU. NOT SPECIFIED [2]
        codebase="http://download..."             |   codebase: "...",          // USU. NOT SPECIFIED [2]
        height="240"                              |   height: 240,              // SHOULD BE SPECIFIED
        width="320"                               |   width: 320,               // SHOULD BE SPECIFIED
        align="middle"                            |   align: "middle",          // OPTIONAL
>                                                 |
    <param name="movie"                           |
        value="http://www.ge.../int...swf" />     |   url: "http://www.ge.../int...swf",  // REQUIRED
    <param name="quality" value="high" />         |   quality: "high",
    <param name="bgcolor" value="#ffffff" />      |   bgcolor: "#ffffff",
    <param name="menu" value="false" />           |   menu: "false",
    ...                                           |   ...
    <embed                                        |
        id="embed1_id"                            |   embedId: "embed1_id",     // OPTIONAL
        name="embed1_name"                        |   embedName: "embed1_name", // OPTIONAL
        type="application/x-shockwave-flash"      |   embedType: "...",         // USU. NOT SPECIFIED [2, 3]
        src="http://www.ge.../int...swf"          |     
        pluginspage="http://www...getflashplayer" |   pluginspage: "..."        // USU. NOT SPECIFIED [2]
        quality="high"                            |
        bgcolor="#ffffff"                         |
        menu="false"                              |
        height="240"                       _______|
        width="320"                       |        
        align="middle"                    |
    >                                     |        
    </embed>                              |       });
</object>                                 |   </script>

SUPPORTING ADDITIONAL MULTIMEDIA TYPES: THE embeddedDefaults ARRAY

	Each entry in the embeddedDefaults array is simply an Object,
	specified using the JavaScript shorthand { name: value, ... }
	syntax.  Each entry has the following properties, all of which
	need to be specified (unless indicated otherwise):

		filenameMatches - a regular expression against which filenames
		are tested.  This is the lookup criteria used when a URL is
		specified and no MIME type is specified.  Examples:

			To test for a single file extension:
				/\.swf$/i
			To test for multiple file extensions:
				/\.(wmv|asf|wmx)$/i
			The /i modifier is required for case-INsensitivity.
			This means we DON'T have to do things like this:
				/\.(SWF|swf)$/

		type - the default MIME type for matching filenames.  This is
		the lookup criteria used when a MIME type is explicitly
		specified in the parameters to insertEmbeddedObject.

		(OPTIONAL) embedType - if the <embed> tag's type="..."
		attribute is different from the <object> tag's type="..."
		attribute for some reason, specify it here.

		paramAttributeForURL - the name of the <param> element whose
		value is the URL.  e.g., "movie" for Macromedia Flash files

		classid, codebase, pluginspage - see code example above.

TROUBLESHOOTING

	Make sure your URL matches at least one of the filenameMatches
	regular expressions.

	If it doesn't, make sure it matches one of the type (not
	embedType) parameters listed in the embeddedDefaults array.

NOTES

	[1] If your URL doesn't match one of the filenameMatches regular
	expressions, you will need to specify a MIME type here.

	[2] You will *only* need to specify classid, codebase, and
	pluginspage if your URL doesn't match any of the filenameMatches
	regexes *and* you don't specify one of the MIME types listed as a
	type (not embedType) in embeddedDefaults.

	[3] Really, you should almost *never* have to specify this one.
	You will *only* need to do so if your URL doesn't match any of the
	filenameMatches regexes *and* you don't specify a MIME type listed
	as a type (not embedType) in embeddedDefaults *and* the <embed>
	tag's type="..." attribute is actually different from the <object>
	tag's.

	We've only seen this situation for Windows Media files, and our
	embeddedDefaults account for this already, but... you never know.

******************************************************************************/

var embeddedDefaults = [
	{
		filenameMatches: /\.swf$/i,
		type: "application/x-shockwave-flash",
		classid: "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
		codebase: "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0",
		pluginspage: "http://www.macromedia.com/go/getflashplayer",
		paramAttributeForURL: "movie"
	},
	{
		filenameMatches: /\.(wmv|wvx|asf|asx)$/i,
		type: "application/x-oleobject",
		embedType: "application/x-mplayer2",
		classid: "clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95",
		codebase: "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab",
		pluginspage: null,
		paramAttributeForURL: "FileName"
	},
	{
		filenameMatches: /\.(qt|mov)$/i,
		type: "video/quicktime",
		classid: "clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b",
		codebase: "http://www.apple.com/qtactivex/qtplugin.cab",
		pluginspage: "http://www.apple.com/quicktime/download/",
		paramAttributeForURL: "src"
	}
];

/* Most parameters passed to insertEmbeddedObject are passed as
<param>s of the <object> and as attributes of the <embed>.  These are
treated differently by insertEmbeddedObject. */

var isEmbeddedSpecialArgument = {
	id: true,
	name: true,
	embedId: true,
	embedName: true,
	url: true,
	type: true,
	width: true,
	height: true,
	classid: true,
	codebase: true,
	embedType: true,
	pluginspage: true,
	align: true,
	DEBUG_GENERATED_SOURCE: true
};

/*****************************************************************************/

/* Utility functions */

function lookupDefaultsByURL (url) {
	for (var i = 0; i < embeddedDefaults.length; ++i) {
		if (embeddedDefaults[i].filenameMatches.test(url)) {
			return embeddedDefaults[i];
		}
	}
	return {}; /* default */
}

function lookupDefaultsByType (type) {
	for (var i = 0; i < embeddedDefaults.length; ++i) {
		if (type == embeddedDefaults[i].type) {
			return embeddedDefaults[i];
		}
	}
	return {}; /* default */
}

function embeddedEncodeEntities (s) {
	return ((s + "")
		.replace(/\&/g, "&amp;")
		.replace(/\</g, "&lt;")
		.replace(/\>/g, "&gt;")
		.replace(/\"/g, "&quot;")
		.replace(/\'/g, "&#38;")
		.replace(/[^\x20-\x7e]/g, function (c) { return "&#" + c.charCodeAt(0) + ";" })
	);
}

function paramTagIfNotNull (name, value) {
	if (name != null && value != null) {
		return "<param name=\"" + embeddedEncodeEntities(name) + "\" value=\"" + embeddedEncodeEntities(value) + "\" />\n";
	} else {
		return "";
	}
}

function attributeIfNotNull (name, value) {
	if (name != null && value != null) {
		return " " + name + "=\"" + embeddedEncodeEntities(value) + "\"\n";
	} else {
		return "";
	}
}

function insertEmbeddedObject (args) {

	/* REMEMBER: When changing this function, you MAY have to add
	things to isEmbeddedSpecialArgument above. */

	var url = args.url;
	if (url == null) { /* sanity check */
		return;
	}

	var type      = args.type; /* MIME type */

	/* If no type is specified, look it up using the URL, and also
	look up defaults for classid, codebase, and pluginspage using the
	URL.  If a type is specified, look up the default classid, codebase,
	and pluginspage based on the type. */

	var defaults;
	if (type == null) {
		defaults = lookupDefaultsByURL(url);
		type = defaults.type;
	} else {
		defaults = lookupDefaultsByType(type);
	}

	var embedType = args.embedType;
	if (embedType == null) {
		embedType = defaults.embedType;
	}
	if (embedType == null) {
		embedType = type;
	}

	var align = args.align;

	/* If a classid, codebase, and/or pluginspage are specified when
	calling this function, use them; otherwise, use their defaults. */

	var classid     = args.classid     || defaults.classid;
	var codebase    = args.codebase    || defaults.codebase;
	var pluginspage = args.pluginspage || defaults.pluginspage;

	var html = "";

	html += "<object\n";
	html += attributeIfNotNull("id", args.id);
	html += attributeIfNotNull("name", args.name);
	html += attributeIfNotNull("type", type);
	html += attributeIfNotNull("width", args.width);
	html += attributeIfNotNull("height", args.height);
	html += attributeIfNotNull("classid", classid);
	html += attributeIfNotNull("codebase", codebase);
	html += attributeIfNotNull("align", align);
	html += ">\n";

	html += paramTagIfNotNull(defaults.paramAttributeForURL, url);

	for (var i in args) {
		if (defaults.paramAttributeForURL == i) {	
			continue;
		}
		if (isEmbeddedSpecialArgument[i]) {
			continue;
		}
		html += paramTagIfNotNull(i, args[i]);
	}

	html += "<embed\n";
	html += attributeIfNotNull("id", args.embedId);
	html += attributeIfNotNull("name", args.embedName);
	html += attributeIfNotNull("type", embedType);
	html += attributeIfNotNull("src", url);
	html += attributeIfNotNull("width", args.width);
	html += attributeIfNotNull("height", args.height);
	html += attributeIfNotNull("pluginspage", pluginspage);
	html += attributeIfNotNull("align", args.align);

	for (var i in args) {
		if (defaults.paramAttributeForURL == i) {
			continue;
		}
		if (isEmbeddedSpecialArgument[i]) {
			continue;
		}
		html += attributeIfNotNull(i, args[i]);
	}

	html += ">\n"; /* end of open <embed> tag */
	html += "</embed>\n";
	html += "</object>\n";

	if (args.DEBUG_GENERATED_SOURCE) {
		window.alert(html);
	}

	document.write(html);
}


