/*
   This file contains functions to generate OBJECT and EMBED tags for QuickTime and Flash content. 
   These functions assemble the tags from parameters passed to them as parameters.

    WP_WriteOBJECT()              - generate HTML tags and insert them into the calling document
    WP_WriteOBJECT_XHTML()        - generate XHTML tags and insert them into the calling document

    WP_GenerateOBJECTText()       - generate HTML tags and return them as a string
    WP_GenerateOBJECTText_XHTML() - generate XHTML tags and return them as a string

   To call one of these functions, pass the url, width, height, objectType [qt|flash]
   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">
      WP_WriteOBJECT(srcURL, width, height, objectType,
         "attributeName1=attributeValue1",
         "attributeName1=attributeValue2",
         "attributeName1=attributeValue3",
         "attributeName1=attributeValue4",
         ...
         "attributeNameN=attributeValueN"
      );
    </script>

   These functions automatically add appropriate "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">
      WP_WriteOBJECT('http://www.domain.com/sample.mov', '480', '288', 'qt', 'emb#bgcolor||#FFFFFF');
   </script>

 */
 
// additional objects should be described here

function wpMediaObjectData(in_type){
	this.type = in_type;
	if (this.type == 'flash'){
		this.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
		this.codebase = "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0";
		this.pluginspage = "http://www.macromedia.com/go/getflashplayer";	
	}
	else if(this.type == 'qt'){
		this.classid = "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";
		this.codebase = "http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0";
		this.pluginspage = "http://www.apple.com/quicktime/download/";	
	}
	else if(this.type == 'realvideo'){
		this.classid = "clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA";
		this.codebase = null;
		this.pluginspage = null;	
	}
	else{
		this.classid = "unknown";
		this.codebase = "unknown";
		this.pluginspage = "unknown";	
	}
}
 

var gTagAttrs = null;

function _WPAddAttribute(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 _WPAddObjectAttr(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 _WPAddAttribute("obj#", slotName, tagName);
}

function _WPAddEmbedAttr(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 _WPAddAttribute("emb#", slotName, tagName);
}


function _WPAddObjectParam(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 _WPDeleteTagAttrs()
{
	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 _WPGenerate(callingFcnName, generateXHTML, args)
{
	// is the number of optional arguments even?
	if (args.length < 4){
		alert( callingFcnName + " function requires at least 4 attributes.")
		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];

	// set up codebase and other ActiveX descriptors (depends on it's type [flash|qt])
	
	var m_object = new wpMediaObjectData(args[3]);
	
	gTagAttrs["classid"]     = m_object.classid;
	gTagAttrs["pluginspage"] = m_object.pluginspage;
	gTagAttrs["codebase"]    = m_object.codebase;

	var attrName, attrValue;

	// add all of the optional attributes to the array
	for ( var i = 4; i < args.length; i += 1){
		
		attrName = args[i].split("||")[0].toLowerCase();
		attrValue = args[i].split("||")[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;

		else 
			gTagAttrs[attrName] = attrValue;
	}

	// init both tags with the required and "special" attributes
	var objTag =  '<object '
					+ _WPAddObjectAttr("classid")
					+ _WPAddObjectAttr("width")
					+ _WPAddObjectAttr("height")
					+ _WPAddObjectAttr("codebase")
					+ _WPAddObjectAttr("name", "id")
					+ _WPAddObjectAttr("tabindex")
					+ _WPAddObjectAttr("hspace")
					+ _WPAddObjectAttr("vspace")
					+ _WPAddObjectAttr("border")
					+ _WPAddObjectAttr("align")
					+ _WPAddObjectAttr("class")
					+ _WPAddObjectAttr("title")
					+ _WPAddObjectAttr("accesskey")
					+ _WPAddObjectAttr("noexternaldata")
					+ '>\n'
					+ _WPAddObjectParam("src", generateXHTML);
	var embedTag = '  <embed '
					+ _WPAddEmbedAttr("src")
					+ _WPAddEmbedAttr("width")
					+ _WPAddEmbedAttr("height")
					+ _WPAddEmbedAttr("pluginspage")
					+ _WPAddEmbedAttr("name")
					+ _WPAddEmbedAttr("align")
					+ _WPAddEmbedAttr("tabindex");

	// delete the attributes/params we have already added
	_WPDeleteTagAttrs("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 += _WPAddEmbedAttr(attrName);
			objTag += _WPAddObjectParam(attrName, generateXHTML);
		}
	} 

	// end both tags, we're done
	return objTag + embedTag + '> </em' + 'bed>\n</ob' + 'ject' + '>';
}

// return the object/embed as a string 
function WP_GenerateOBJECTText(){
	return _WPGenerate("WP_GenerateOBJECTText", false, arguments);
}

// return the object/embed as a string XHTML
function WP_GenerateOBJECTText_XHTML(){
	return _WPGenerate("WP_GenerateOBJECTText_XHTML", true, arguments);
}


function WP_WriteOBJECT(){
	document.writeln(_WPGenerate("WP_WriteOBJECT", false, arguments));
}

function WP_WriteOBJECT_XHTML(){
	//alert(_WPGenerate("WP_WriteOBJECT_XHTML", true, arguments));
	document.writeln(_WPGenerate("WP_WriteOBJECT_XHTML", true, arguments));
}
