/*
   This file contains functions to generate OBJECT and EMBED tags for WindowsMediaPlayer content. 
   These functions assemble the tags from parameters passed to them as parameters.

    WMP_WriteOBJECT()              - generate HTML tags and insert them into the calling document
    WMP_WriteOBJECT_XHTML()        - generate XHTML tags and insert them into the calling document

    WMP_GenerateOBJECTText()       - generate HTML tags and return them as a string
    WMP_GenerateOBJECTText_XHTML() - generate XHTML tags and return them as a string
    
    AC_WindowsMediaPlayerVersion()         - return the version of this file as a floating point number.

   To call one of these functions, pass the url, width, height, and required ActiveX
   control version as parameters 1 through 4, and pass all other attributes and 
   parameters that you would otherwise specify for the OBJECT, PARAM, and EMBED 
   tags as pairs of parameters:

   <script language="JavaScript" type="text/javascript">
      WMP_WriteOBJECT(srcURL, width, height, activeXVersion,
         "attributeName1", "attributeValue1",
         "attributeName1", "attributeValue2",
         "attributeName1", "attributeValue3",
         "attributeName1", "attributeValue4",
         ...
         "attributeNameN", "attributeValueN"
      );
    </script>

   These functions automatically add the "classid", "codebase", and "pluginspage" tags to 
   the OBJECT and/or EMBED tags with standard values if they are not specified in the
   parameter list, so you need not supply these tags unless you require non-standard values.
   
   Although the OBJECT and EMBED tags typically have the same attributes and parameters, it
   is sometimes useful to have different values for each. 
   Any attribute name prefixed with "obj#" is added to the OBJECT tag only, any attribute 
   name prefixed with "emb#" is added to the EMBED tag only.

   <script language="JavaScript" type="text/javascript">
      WMP_WriteOBJECT('http://www.domain.com/sample.mov', '480', '288', ''
        , 'emb#bgcolor', '#FFFFFF');
   </script>

 */

/************** LOCALIZABLE GLOBAL VARIABLES ****************/

var gArgCountErr =	'The "%%" function requires an even number of arguments.'
				+	'\nArguments should be in the form "atttributeName", "attributeValue", ...';

/******************** END LOCALIZABLE **********************/

var gTagAttrs				= null;
var gWMPGeneratorVersion		= 0.8;

function AC_WindowsMediaPlayerVersion()	{ return gWMPGeneratorVersion; }

function _WMPComplain(callingFcnName, errMsg)
{
    errMsg = errMsg.replace("%%", callingFcnName);
	alert(errMsg);
}

function _WMPAddAttribute(prefix, slotName, tagName)
{
	var		value;

	value = gTagAttrs[prefix + slotName];
	if ( null == value )
		value = gTagAttrs[slotName];

	if ( null != value )
	{
		if ( 0 == slotName.indexOf(prefix) && (null == tagName) )
			tagName = slotName.substring(prefix.length); 
		if ( null == tagName ) 
			tagName = slotName;
		return tagName + '="' + value + '" ';
	}
	else
		return "";
}

function _WMPAddObjectAttr(slotName, tagName)
{
	// don't bother if it is only for the embed tag
	if ( 0 == slotName.indexOf("emb#") )
		return "";

	if ( 0 == slotName.indexOf("obj#") && (null == tagName) )
		tagName = slotName.substring(4); 

	return _WMPAddAttribute("obj#", slotName, tagName);
}

function _WMPAddEmbedAttr(slotName, tagName)
{
	// don't bother if it is only for the object tag
	if ( 0 == slotName.indexOf("obj#") )
		return "";

	if ( 0 == slotName.indexOf("emb#") && (null == tagName) )
		tagName = slotName.substring(4); 

	return _WMPAddAttribute("emb#", slotName, tagName);
}


function _WMPAddObjectParam(slotName, generateXHTML)
{
	var		paramValue;
	var		paramStr = "";
	var		endTagChar = (generateXHTML) ? ' />' : '>';

	if ( -1 == slotName.indexOf("emb#") )
	{
		// look for the OBJECT-only param first. if there is none, look for a generic one
		paramValue = gTagAttrs["obj#" + slotName];
		if ( null == paramValue )
			paramValue = gTagAttrs[slotName];

		if ( 0 == slotName.indexOf("obj#") )
			slotName = slotName.substring(4); 
	
		if ( null != paramValue )
			paramStr = '  <param name="' + slotName + '" value="' + paramValue + '"' + endTagChar + '\n';
	}

	return paramStr;
}

function _WMPDeleteTagAttrs()
{
	for ( var ndx = 0; ndx < arguments.length; ndx++ )
	{
		var attrName = arguments[ndx];
		delete gTagAttrs[attrName];
		delete gTagAttrs["emb#" + attrName];
		delete gTagAttrs["obj#" + attrName];
	}
}

		

// generate an embed and object tag, return as a string
function _WMPGenerate(callingFcnName, generateXHTML, args)
{
	// is the number of optional arguments even?
	if ( args.length < 4 || (0 != (args.length % 2)) )
	{
		_WMPComplain(callingFcnName, gArgCountErr);
		return "";
	}
	
	// allocate an array, fill in the required attributes with fixed place params and defaults
	gTagAttrs = new Array();
	gTagAttrs["src"] = args[0];
	gTagAttrs["width"] = args[1];
	gTagAttrs["height"] = args[2];
	gTagAttrs["classid"] = "clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95";
	//gTagAttrs["pluginspage"] = "http://www.apple.com/quicktime/download/";
	gTagAttrs["codebase"] = "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab";

	var	attrName,
		attrValue;

	// add all of the optional attributes to the array
	for ( var ndx = 4; ndx < args.length; ndx += 2)
	{
		attrName = args[ndx].toLowerCase();
		attrValue = args[ndx + 1];

		// "name" and "id" should have the same value, the former goes in the embed and the later goes in
		//  the object. use one array slot 
		if ( "name" == attrName || "id" == attrName ) {
			gTagAttrs["name"] = attrValue;
			gTagAttrs["id"] = attrValue;
        }

		else 
			gTagAttrs[attrName] = attrValue;
	}

	// init both tags with the required and "special" attributes
	var objTag =  '<object '
					+ _WMPAddObjectAttr("classid")
					+ _WMPAddObjectAttr("width")
					+ _WMPAddObjectAttr("height")
					+ _WMPAddObjectAttr("codebase")
					+ _WMPAddObjectAttr("name", "id")
					+ _WMPAddObjectAttr("tabindex")
					+ _WMPAddObjectAttr("hspace")
					+ _WMPAddObjectAttr("vspace")
					+ _WMPAddObjectAttr("border")
					+ _WMPAddObjectAttr("align")
					+ _WMPAddObjectAttr("class")
					+ _WMPAddObjectAttr("title")
					+ _WMPAddObjectAttr("accesskey")
					+ _WMPAddObjectAttr("noexternaldata")
					+ '>\n'
					+ _WMPAddObjectParam("src", generateXHTML);
	var embedTag = '  <embed '
					+ _WMPAddEmbedAttr("src")
					+ _WMPAddEmbedAttr("width")
					+ _WMPAddEmbedAttr("height")
					+ _WMPAddEmbedAttr("pluginspage")
					+ _WMPAddEmbedAttr("name")
					+ _WMPAddEmbedAttr("align")
					+ _WMPAddEmbedAttr("tabindex");

	// delete the attributes/params we have already added
	_WMPDeleteTagAttrs("src","width","height","pluginspage","classid","codebase","name","tabindex",
					"hspace","vspace","border","align","noexternaldata","class","title","accesskey");

	// and finally, add all of the remaining attributes to the embed and object
	for ( var attrName in gTagAttrs )
	{
		attrValue = gTagAttrs[attrName];
		if ( null != attrValue )
		{
			embedTag += _WMPAddEmbedAttr(attrName);
			objTag += _WMPAddObjectParam(attrName, generateXHTML);
		}
	} 

	// end both tags, we're done
	return objTag + embedTag + '> </em' + 'bed>\n</ob' + 'ject' + '>';
}

// return the object/embed as a string
function WMP_GenerateOBJECTText()
{
	return _WMPGenerate("WMP_GenerateOBJECTText", false, arguments);
}

function WMP_GenerateOBJECTText_XHTML()
{
	return _WMPGenerate("WMP_GenerateOBJECTText_XHTML", true, arguments);
}

function WMP_WriteOBJECT()
{
	document.writeln(_WMPGenerate("WMP_WriteOBJECT", false, arguments));
}

function WMP_WriteOBJECT_XHTML()
{
	document.writeln(_WMPGenerate("WMP_WriteOBJECT_XHTML", true, arguments));
}


