//v1.0
//Copyright 2006 Adobe Systems, Inc. All rights reserved.
//                                                                       __
//      ___   __ __ ____ ____     ____ _____ _____ ____       ____  ____/ /       
//     / _ \ / // // __// __ \   / __// ___// ___// __ \     /   / / __  /
//    / ___// // // /  / /_/ /  / /  (__  )/ /__ / /_/ /   / _/ /_/ /_/ /
//    \___/ \___//_/   \____/  /_/  /____/ \___/ \__  /  /___  __/\____/
//                                              /____/      /_/
// Modifications:
//
// - Added 'style' attribute to general attributes in AC_GetArgs() to prevent
//   it from becoming a param-value
//
// - Moved 'id' attribute to object-only section and moved 'name' attribute
//   to embed-only section; id names must be unique within a document, so
//   they cannot be assigned to both object- and embed-tags. Original
//   Macromedia technotes used 'id' in object part and 'name' in embed part
//   Also values from both attributes are stored to be able to copy them
//   in case either of them is missing; this requires the values of both
//   id and name to not contain character references (entities).
//
// - Added handling of missing 'id' or 'name' values (if both are missing
//   then there is no problem). 'id' values must be unique within a document,
//   and previous code copied them to both object- and embed-tag
//
// - Added handling of 'pe' and 'po' attributes which can contain the
//   output of $PE and $PO in Flash Publish Templates. The code adds the
//   attribute-value pairs contained in those variables to the array of
//   parameters and parses them in the end. 

function AC_AddExtension(src, ext)
{
  //first remove extension if it happens to be present
  //'movie' and 'src' from Flash Publish Templates have
  //extension; manually build pages should not have it.
  myRex = '\\'+ext+'\\?';
  myRe = new RegExp(myRex);
  src = src.replace(myRe, '?');
  myRex = '\\'+ext+'$';
  myRe = new RegExp(myRex);
  src = src.replace(myRe, '');
  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(as, ext, srcParamName, classid, mimeType){
  var args = new Array();
  for (var i=0; i < as.length; i++) {
    args[i] = as[i];
  }
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  var attrsId = '';
  var attrsName = '';
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    
    switch (currArg){	
      case "classid":
        break;
      case "name":
        attrsName = args[i+1];  // no break! continue rest of handling
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "id":
        attrsId = args[i+1]; // no break! continue rest of handling
      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 "tabindex":
      case "style":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      case "po":
        //object parameters as generated by Flash Publish command
        poRe = new RegExp ("\\s*<param\\s+name=\"([^\"]+)\"\\s+value=\"([^\"]+)\"\\s*\/?>", "mgi");
        var lastIndex = 0;
        var thisloop = true;
        while (thisloop) {
          poArray = poRe.exec(args[i+1]);
          if (!poArray) break;
          for (var j=1; j<poArray.length; j=j+2) {
            args[args.length] = poArray[j];
            args[args.length] = poArray[j+1];
          }
          lastIndex = poRe.lastIndex;
        }
        break;
      case "pe":
        //embed parameters as generated by Flash Publish command
        poRe = new RegExp ("\\s*([^=]+)\\s*=\\s*\"([^\"]+)\"", "mgi");
        var lastIndex = 0;
        var thisloop = true;
        while (thisloop) {
          poArray = poRe.exec(args[i+1]);
          if (!poArray) break;
          for (var j=1; j<poArray.length; j=j+2) {
            args[args.length] = poArray[j];
            args[args.length] = poArray[j+1];
          }
          lastIndex = poRe.lastIndex;
        }
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  //fix missing id or name attribute
  // this fix assumes that id and name values do not contain character 
  // references (entities), since the HTML DTD does not allow them for id's
  if (attrsId != '' && attrsName == '') {
    ret.embedAttrs['name'] = attrsId;
  } else if (attrsId == '' && attrsName != '') {
    ret.objAttrs['id'] = attrsName;
  }
  //endifx
  return ret;
}