//v1.0
//Copyright 2006 Adobe Systems, Inc. All rights reserved.



// Wrapper functions added to Adobe Systems, Inc.  File.
//_____________________________________________________________________________________________
//Display the content in Lectora using an external HTML object as the Obj param.  If you use
//an external object as the object passed to this function then it must remain the correct 
//size in Lectora.  This function is specific to Lectora created objects, use DisplayContent()
//for any other situation.
//Author: Mark Mastro
//SweetRush.com
//Function Version: 1.0
function DisplayLectoraContent(Obj, fileName, flashVars)
{
	DisplayContent(fileName, Obj.w, Obj.h, flashVars);
}

//----------------------------------------------------------------------------------
//Parses the parameters passed to the function and creates an object to display flash content
//The first element in the function should be the file name, the second should be the width, and
//the third should be the height.  The fourth parameter is optional (flashVars).  If you want 
//to include more variables that aren't 'standard' you need to specify flashVars as "".
//Put everything else following flashVars parameter.  The format for the parameters following flashVars
//is 'elementname', 'value'

//Usage: DisplayContentv2(fileName, Width, Height);
//DisplayContentv2(fileName, width, height, "List of flash vars");
//DisplayContentv2(fileName, width, height, "List of flash vars", "bgcolor", "ED1F22");
//DisplayContentv2(fileName, width, height, "", "bgcolor", "ED1F22");

//Author: Mark Mastro
//SweetRush.com
//Function Version: 1.0
//----------------------------------------------------------------------------------
function DisplayContent()
{
	//Don't continue if there are no arguments
	if(DisplayContent.arguments.length == 0)
		return;
		
	//Strip out the default
	var fileName, width, height;					//These are the standard variables they MUST be in the function call
	var flashVars;									//This is the first of the optional elements
	var str = new Array();  						//This variable is going to replace the arguments array
	var standardElements = new Array('quality', 'high', 'align', 'middle', 
									'scale', 'noscale',  
									'devicefont', 'false', 'allowScriptAccess', 
									'sameDomain');

	//Loop through the arguments and grab the filename, width, height, flashVars and anyother elements include									
	for(var i=0, index=0; i < DisplayContent.arguments.length; i++)
	{
		switch(i)
		{
		case 0:
			str[index++] = "src";
			str[index++] = DisplayContent.arguments[i];
			str[index++] = "id";
			str[index++] = DisplayContent.arguments[i];
			str[index++] = "name";
			str[index++] = DisplayContent.arguments[i];
			str[index++] = "movie";
			str[index++] = DisplayContent.arguments[i];
			break;
		case 1:
			str[index++] = 'width';
			str[index++] = DisplayContent.arguments[i];
			break;
		case 2:
			str[index++] = 'height';
			str[index++] = DisplayContent.arguments[i];
			break;
		case 3:
			if(DisplayContent.arguments[i] != "")
			{
				str[index++] = 'flashVars';
				str[index++] = DisplayContent.arguments[i];
			}
			break;
		default:
			str[index++] = DisplayContent.arguments[i];
			break;
		};
	}
	
	//Done adding all the parameters, now add the default values to the array
	for(var i=0, index=str.length; i < standardElements.length; i++)
	{
		str[index++] = standardElements[i];	
	}
	
	var ret = AC_GetArgs( str, 
						".swf", 
						"movie", 
						"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", 
						"application/x-shockwave-flash", 
						false);
							
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

//----------------------------------------------------------------------------------
//Parses the parameters passed to the function and creates an object to display flash content
//The first element in the function should be the file name, the second should be the width, and
//the third should be the height.  If you need to define the flashvars you need to add the parameter to the parameter list
//'FlashVars', 'disableNext=true'   
//Parameter list structure is 'elementname', 'value'

//Usage: DisplayContentv2(fileName, Width, Height);
//DisplayContentv2(fileName, Width, Height, List of variables to add or override);
//example : 
// DisplayContentv2('example', 1010, 600, 
//					'id' , 'newid', 
//					'bgcolor', '#ffffff');
//The default values that will be filled if no parameter list is included are src, id, name, movie tags in the <object> tag

//Author: Mark Mastro
//SweetRush.com
//Function Version: 2.0
//----------------------------------------------------------------------------------
function DisplayContentv2()
{
	//Don't continue if there are no arguments
	if(DisplayContentv2.arguments.length == 0)
		return;

	//Strip out the default
	var fileName, width, height;					//These are the standard variables they MUST be in the function call
	var flashVars;									//This is the first of the optional elements
	var str = new Array();  						//This variable is going to replace the arguments array
	var standardElements = new Array('quality', 'high', 'align', 'middle', 
									'scale', 'noscale',  
									'devicefont', 'false', 'allowScriptAccess', 
									'sameDomain');

	//Loop through the arguments and grab the filename, width, height, flashVars and anyother elements include									
	for(var i=0, index=0; i < DisplayContentv2.arguments.length; i++)
	{
		switch(i)
		{
		case 0:
			str[index++] = "src";
			str[index++] = DisplayContentv2.arguments[i];
			//Check to see if the ID is present in the array of attributes
			if(!HasValue(DisplayContentv2, "id"))
			{
				str[index++] = "id";
				str[index++] = DisplayContentv2.arguments[i];
			}
			if(!HasValue(DisplayContentv2, "name"))
			{
				str[index++] = "name";
				str[index++] = DisplayContentv2.arguments[i];
			}
			if(!HasValue(DisplayContentv2, "movie"))
			{
				str[index++] = "movie";
				str[index++] = DisplayContentv2.arguments[i];
			}
			break;
		case 1:
			str[index++] = 'width';
			str[index++] = DisplayContentv2.arguments[i];
			break;
		case 2:
			str[index++] = 'height';
			str[index++] = DisplayContentv2.arguments[i];
			break;
		default:
			str[index++] = DisplayContentv2.arguments[i];
			break;
		};
	}

	//Done adding all the parameters, now add the default values to the array
	for(var i=0, index=str.length; i < standardElements.length; i++)
	{
		if(!HasValue(DisplayContentv2, standardElements[i]))
			str[index++] = standardElements[i];	
		else
			i++;
	}
	
	var ret = AC_GetArgs( str, 
						".swf", 
						"movie", 
						"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", 
						"application/x-shockwave-flash", 
						true);
				
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function HasValue(content, value)
{
	//Check to see if value is anywhere in content
	for(var i=0; i < content.arguments.length; i++)
	{
		var bufferValue = value.toLowerCase();
		var temp = content.arguments[i].toString();
		var bufferContent = temp.toLowerCase();
		if(bufferValue == bufferContent)
			return true;
	}
	
	return false;
}
//_____________________________________________________________________________________________



function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType, version2){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
		//Removed for version 2 of the displaycontent function 6_4_2007 MM error adding extensions to complex URLs, just type it in...
		if(!version2)
			args[i+1] = AC_AddExtension(args[i+1], ext);
		ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}

