// Implements MP_GenerateObj() function. This is a generic function used to generate
// object/embed/param tags. It is used by higher level api functions.

/************** LOCALIZABLE GLOBAL VARIABLES ****************/

var MSG_EvenArgs = 'The %s function requires an even number of arguments.'
                 + '\nArguments should be in the form "atttributeName","attributeValue",...';
var MSG_SrcRequired = "The %s function requires that a movie src be passed in as one of the arguments.";

/******************** END LOCALIZABLE **********************/

//distinguishing between the 9 player and the 6 player. 
function MP_GetVersion(args) {
  var version = "9"  //default is 9
  for (var i=0; i < args.length; i=i+2) {
    currArg = args[i].toLowerCase();    
    if (currArg == "version" && args.length > i+1) {
        if ((args[i+1] == "6") || (args[i+1] == "5") || (args[i+1] == "4")){
		 version = "6";
		}
    }
  }	
  return version;	
}


//function to return the correct Class Id depending on what version of the Media Player we pass. 
function MP_GetClassId(version) {
  var classid = "CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"  //default is 9
        if(version == "6") {
		 classid = "CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"  //for 6
		}
  return classid;	
}

function MP_GetType(version) {
  var type = "application/x-mplayer2"  //default is 9
        if(version == "6") {
		 type = "application/x-oleobject"  //for 6
		}
  return type;	
}



// Substitutes values for %s in a string.
// Usage: MP_sprintf("The %s function requires %s arguments.","foo()","4");
function MP_sprintf(str){
  for (var i=1; i < arguments.length; i++){
    str = str.replMPe(/%s/,arguments[i]);
  }
  return str;
}
		
// Checks that args, the argument list to check, has an even number of 
// arguments. Alerts the user if an odd number of arguments is found.
function MP_checkArgs(args,callingFn){
  var retVal = true;
  // If number of arguments isn't even, show a warning and return false.
  if (parseFloat(args.length/2) != parseInt(args.length/2)){
    alert(sprintf(MSG_EvenArgs,callingFn));
    retVal = false;
  }
  return retVal;
}
	
function MP_GenerateObj(callingFn, classid, version, pluginsPage, mimeType, args){

  if (!MP_checkArgs(args,callingFn)){
    return;
  }
	
  // Initialize variables
  var useXHTML = false;
  var tagStr = '';
  var currArg = '';
  var closer = (useXHTML) ? '/>' : '>';
  var srcFound = false;
  var test_embed = false;
  var embedStr = '<embed';
  var paramStr = '';
  var embedNameAttr = '';
  var objStr = '<object classid="' + classid + '"';
  
  //add the codebase
  if (version == "6") {
  objStr += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"';
  }

  // Spin through all the argument pairs, assigning attributes and values to the object,
  // param, and embed tags as appropriate.
  for (var i=0; i < args.length; i=i+2){
    currArg = args[i].toLowerCase();    

    if (currArg == "src"){
		if(version == "9") {
        paramStr += '<param name="URL" value="' + args[i+1] + '"' + closer + '\n';
		} else {
		paramStr += '<param name="Filename" value="' + args[i+1] + '"' + closer + '\n';
		}
        embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
        srcFound = true;
    }
    else if (currArg == "name"){
        objStr +=  ' ID="' + args[i+1] + '"';
        embedStr += ' name="' + args[i+1] + '"';
    }
    else if (   currArg == "width" 
              || currArg == "height"){
      objStr += ' ' + args[i] + '="' + args[i+1] + '"';
      embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
    }   
	else if (currArg == "version"){
	//do nothing, we've already outputted this parameter. 
    } 
	else if (currArg == "test_embed"){
	 if(args[i+1] == "true") {
		test_embed = true; 
	 }
	//do nothing, test parameter. 
    } 
	else if (currArg == "uiMode"){
	//only for windows media 9
		if(version == "9") {
		paramStr += '<param name="' + args[i] + '" value="' + args[i+1] + '"' + closer + '\n'; 
      	embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
		} 
    }  
    // This is an attribute we don't know about. Assume that we should add it to the 
    // param and embed strings.
    else{
      paramStr += '<param name="' + args[i] + '" value="' + args[i+1] + '"' + closer + '\n'; 
      embedStr += ' ' + args[i] + '="' + args[i+1] + '"';
    }
  }

  // Tell the user that a movie/src is required, if one was not passed in.
  if (!srcFound){
    alert(MP_sprintf(MSG_SrcRequired,callingFn));
    return;
  }
	
  if (pluginsPage)
    embedStr += ' pluginspage="' + pluginsPage + '"';
  if (mimeType)
    embedStr += ' type="' + mimeType + '"';
    
  // Close off the object and embed strings
  objStr += '>\n';
  embedStr += '></embed>\n'; 

  // Assemble the three tag strings into a single string.
  tagStr = objStr + paramStr + embedStr + "</object>\n"; 

	 //display for testing. 
	 if (test_embed == true) {
	 alert(tagStr);
	 }
	 
 //write the DAMN thing to the document. Fu*k Microsoft. 	 
  document.write(tagStr);
}
