/*
 * This file contains functions to generate OBJECT and EMBED tags for QuickTime content. 
 */

/************** 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 gQTGeneratorVersion		= 1.2;
var gQTBehaviorID			= "MEDIA_MANAGER_event_source";
var gQTEventsEnabled		= false;

var gObjectType = "quicktime";
var gClassId_behavior = "clsid:CB927D12-4FF7-4a9e-A169-56E4B8A75598";
var gClassId = "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";
var gPluginsPage = "http://www.apple.com/quicktime/download/";
var gActiveXVers = "7,3,0,0";
var gCodeBase = "http://www.apple.com/qtactivex/qtplugin.cab#version=" + gActiveXVers;
var gSrcName = "src";

/*
	gTagAttrs["pluginspage"] = "http://www.apple.com/quicktime/download/";

	// set up codebase attribute with specified or default version before parsing args so
	//  anything passed in will override
	var activexVers = args[3]
	if ( (null == activexVers) || ("" == activexVers) )
		activexVers = "7,3,0,0";
	gTagAttrs["codebase"] = "http://www.apple.com/qtactivex/qtplugin.cab#version=" + activexVers;

*/

function AC_QuickTimeVersion()	{ return gQTGeneratorVersion; }

function _MEDIA_MANAGERComplain(callingFcnName, errMsg)
{
    errMsg = errMsg.replace("%%", callingFcnName);
	alert(errMsg);
}

function _MEDIA_MANAGERIsMSIE()
{
    var ua = navigator.userAgent.toLowerCase();
	var msie = /msie/.test(ua) && !/opera/.test(ua);

	return msie;
}


function _MEDIA_MANAGERGenerateBehavior()
{
	return objTag = '<!--[if IE]>'
				 + '<object id="' + gQTBehaviorID + '" classid="'+gClassId_behavior+'"></object>'
				 + '<![endif]-->';
}

function _MEDIA_MANAGERPageHasBehaviorObject(callingFcnName, args)
{
	var haveBehavior = false;
	var objects = document.getElementsByTagName('object');
	
	for ( var ndx = 0, obj; obj = objects[ndx]; ndx++ )
	{
		if ( obj.getAttribute('classid') == gClassId_behavior )
		{
			if ( obj.getAttribute('id') == gQTBehaviorID )
				haveBehavior = false;
			break;
		}
	}

	return haveBehavior;
}


function _MEDIA_MANAGERShouldInsertBehavior()
{
	var		shouldDo = false;

	if ( gQTEventsEnabled && _MEDIA_MANAGERIsMSIE() && !_MEDIA_MANAGERPageHasBehaviorObject() )
		shouldDo = true;
	
	return shouldDo;
}


function _MEDIA_MANAGERAddAttribute(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 _MEDIA_MANAGERAddObjectAttr(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 _MEDIA_MANAGERAddAttribute("obj#", slotName, tagName);
}

function _MEDIA_MANAGERAddEmbedAttr(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 _MEDIA_MANAGERAddAttribute("emb#", slotName, tagName);
}


function _MEDIA_MANAGERAddObjectParam(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;
	}

	return paramStr;
}

function _MEDIA_MANAGERDeleteTagAttrs()
{
	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 _MEDIA_MANAGERGenerate(callingFcnName, generateXHTML, args)
{
	// is the number of optional arguments even?
	if ( args.length < 4 || (0 != (args.length % 2)) )
	{
		_MEDIA_MANAGERComplain(callingFcnName, gArgCountErr);
		return "";
	}
	
	// allocate an array, fill in the required attributes with fixed place params and defaults
	gTagAttrs = new Object();
	gTagAttrs[gSrcName] = args[0];
	gTagAttrs["src"] = args[0];
	gTagAttrs["width"] = args[1];
	gTagAttrs["height"] = args[2];
	gTagAttrs["classid"] = gClassId;
		//Impportant note: It is recommended that you use this exact classid in order to ensure a seamless experience for all viewers
	gTagAttrs["pluginspage"] = gPluginsPage;

	// set up codebase attribute with specified or default version before parsing args so
	//  anything passed in will override
	/*
	var activexVers = args[3]
	if ( (null == activexVers) || ("" == activexVers) )
	*/
	
	activexVers = gActiveXVers;
		
	gTagAttrs["codebase"] = gCodeBase;

	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];

		gTagAttrs[attrName] = attrValue;

		if ( ("postdomevents" == attrName) && (attrValue.toLowerCase() != "false") )
		{
			gQTEventsEnabled = true;
			if ( _MEDIA_MANAGERIsMSIE() )
				gTagAttrs["obj#style"] = "behavior:url(#" + gQTBehaviorID + ")";
		}
	}

	// init both tags with the required and "special" attributes
	var objTag =  '<object '
					+ _MEDIA_MANAGERAddObjectAttr("classid")
					+ _MEDIA_MANAGERAddObjectAttr("width")
					+ _MEDIA_MANAGERAddObjectAttr("height")
					+ _MEDIA_MANAGERAddObjectAttr("codebase")
					+ _MEDIA_MANAGERAddObjectAttr("name")
					+ _MEDIA_MANAGERAddObjectAttr("id")
					+ _MEDIA_MANAGERAddObjectAttr("tabindex")
					+ _MEDIA_MANAGERAddObjectAttr("hspace")
					+ _MEDIA_MANAGERAddObjectAttr("vspace")
					+ _MEDIA_MANAGERAddObjectAttr("border")
					+ _MEDIA_MANAGERAddObjectAttr("align")
					+ _MEDIA_MANAGERAddObjectAttr("class")
					+ _MEDIA_MANAGERAddObjectAttr("title")
					+ _MEDIA_MANAGERAddObjectAttr("accesskey")
					+ _MEDIA_MANAGERAddObjectAttr("noexternaldata")
					+ _MEDIA_MANAGERAddObjectAttr("obj#style")
					+ '>'
					+ _MEDIA_MANAGERAddObjectParam(gSrcName, generateXHTML);
	var embedTag = '<embed '
					+ _MEDIA_MANAGERAddEmbedAttr("src")
					+ _MEDIA_MANAGERAddEmbedAttr("width")
					+ _MEDIA_MANAGERAddEmbedAttr("height")
					+ _MEDIA_MANAGERAddEmbedAttr("pluginspage")
					+ _MEDIA_MANAGERAddEmbedAttr("name")
					+ _MEDIA_MANAGERAddEmbedAttr("id")
					+ _MEDIA_MANAGERAddEmbedAttr("align")
					+ _MEDIA_MANAGERAddEmbedAttr("tabindex");

	// delete the attributes/params we have already added
	_MEDIA_MANAGERDeleteTagAttrs(gSrcName,"width","height","pluginspage","classid","codebase","name","tabindex",
					"hspace","vspace","border","align","noexternaldata","class","title","accesskey","id","style");

	// 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 += _MEDIA_MANAGERAddEmbedAttr(attrName);
			objTag += _MEDIA_MANAGERAddObjectParam(attrName, generateXHTML);
		}
	} 

	// end both tags, we're done
	return objTag + embedTag + '></em' + 'bed></ob' + 'ject' + '>';
}


// return the object/embed as a string
function MEDIA_MANAGER_ObjectTYPE(objectType) {
	if ( objectType == "quicktime" ) {
		gObjectType = "quicktime";
		gClassId_behavior = "clsid:CB927D12-4FF7-4a9e-A169-56E4B8A75598";
		gClassId = "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";
		gPluginsPage = "http://www.apple.com/quicktime/download/";
		gActiveXVers = "7,3,0,0";
		gCodeBase = "http://www.apple.com/qtactivex/qtplugin.cab#version=" + gActiveXVers;
		gSrcName = "src";
	}
	
	if ( objectType == "flash" ) {
		gObjectType = "flash";
		gClassId_behavior = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
		gClassId = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
		gPluginsPage = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash";
		gActiveXVers = "4,0,0,0";
		gCodeBase = "http://active.macromedia.com/flash2/cabs/swflash.cab#version=" + gActiveXVers;
		gSrcName = "movie";
	}
}

function MEDIA_MANAGER_GenerateOBJECTText()
{
	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_GenerateOBJECTText", false, arguments);
	if ( _MEDIA_MANAGERShouldInsertBehavior() )
		txt = _MEDIA_MANAGERGenerateBehavior() + txt;
	return txt;
}

function MEDIA_MANAGER_GenerateOBJECTText_XHTML()
{
	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_GenerateOBJECTText_XHTML", true, arguments);
	if ( _MEDIA_MANAGERShouldInsertBehavior() )
		txt = _MEDIA_MANAGERGenerateBehavior() + txt;
	return txt;
}

function MEDIA_MANAGER_WriteOBJECT()
{
	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_WriteOBJECT", false, arguments);
	if ( _MEDIA_MANAGERShouldInsertBehavior() )
		document.writeln(_MEDIA_MANAGERGenerateBehavior());
	document.writeln(txt);
}

function MEDIA_MANAGER_WriteOBJECT_XHTML()
{
	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_WriteOBJECT_XHTML", true, arguments);
	if ( _MEDIA_MANAGERShouldInsertBehavior() )
		document.writeln(_MEDIA_MANAGERGenerateBehavior());
	document.writeln(txt);
}

function MEDIA_MANAGER_GenerateBehaviorOBJECT()
{
	return _MEDIA_MANAGERGenerateBehavior();
}

function MEDIA_MANAGER_ReplaceElementContents()
{
	var element = arguments[0];
	var args = [];

	// copy all other arguments we want to pass through to the fcn
	for ( var ndx = 1; ndx < arguments.length; ndx++ )
		args.push(arguments[ndx]);

	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_ReplaceElementContents", false, args);
	if ( txt.length > 0 )
		element.innerHTML = txt;
}


function MEDIA_MANAGER_ReplaceElementContents_XHTML()
{
	var element = arguments[0];
	var args = [];

	// copy all other arguments we want to pass through to the fcn
	for ( var ndx = 1; ndx < arguments.length; ndx++ )
		args.push(arguments[ndx]);

	var	txt = _MEDIA_MANAGERGenerate("MEDIA_MANAGER_ReplaceElementContents_XHTML", true, args);
	if ( txt.length > 0 )
		element.innerHTML = txt;
}


