var f_objs = [];
var isAlertFsCommand = false;
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function ConstructFlashObject(obj, isCommonFunction, param) {
  f_objs = obj;
  for (var i in obj) {
    obj[i].id = i;
    InitFlashParameters(obj[i], param);
    InitFSCommandHandle(i, obj[i], isCommonFunction);
    if (isInternetExplorer) document.write(initIEHookScript(i, obj[i]));
  }
}

function InitFlashParameters(obj, param) {
  obj.addParam("allowScriptAccess", "view=sameDomain");
  obj.addParam("quality", "high");
  obj.addParam("wmode", "transparent");
  if (param) {
    for (var i in param) {
      obj.addParam(i, param[i]);
    }
  }
}

function InitFSCommandHandle(sObjName, obj, isCommonFunction) {
  var sFuncName = 'window.' + sObjName + '_DoFSCommand';
  var sFunc = sFuncName + ' = function (command, args) {'
            + ' var ' + sObjName + 'Obj = isInternetExplorer ? document.all.' + sObjName + ' : document.' + sObjName + ';'
            + ' f_objs["' + sObjName + '"].doCommand(command, args);'
            + '}';
  eval(sFunc);
  if (isCommonFunction) {
    obj.doCommand = function(command, args) {
      if (isAlertFsCommand) alert(command + '\n' + args);
      var oFunc = null;
      try {oFunc = eval('window.fs_' + command);} catch (err) {}
      if (typeof(oFunc) == 'undefined') {
        oFunc = function (args) {
          alert('fscommand function unassigned - fs_' + command);
        }
      } else {
        eval('window.fs_' + command + '(args)');
      }
    }
  } else {
    obj.doCommand = function(command, args) {
      if (isAlertFsCommand) alert(command + '\n' + args);
      var oFunc = eval('this.' + command);
      if (typeof(oFunc) == 'function') oFunc(args);
        else return oFunc;
    }
  }
}

function initIEHookScript(sObjName, obj) {
  obj.IEHook = '<SCRIPT event=FSCommand(command,args) for=' + obj.id + '>\n'
             + '  ' + obj.id + '_DoFSCommand(command, args);\n'
             + '</SCRIPT>';
  //obj.IEHook = obj.id + '_DoFSCommand(command, args);';
  return obj.IEHook;
}
