<!-- 
// This script determines correct code required to embed MEDIA files 
// for a large number of browsers, including AOL and WebTV
// Windows Media Player is required and always used, except for WebTV
// Written by Les Gorven, http://midistudio.com/ 
// Ver. 3.0  Last Updated: January 17, 2006

// 02/13/2007  Ron Pulcer    Refactored to work with Capstone Project.
// Modified the embed* methods to return a string, and split playMedia() into codegen and document.write().
// This allows me to put the generated code into innerHTML attributes directly (otherwise it overwrites web page).

// 03/16/2007  Ron Pulcer    Added new function called reviewCode():
// I noticed that the generated code works OK locally on PC in Windows IE,
// but when it is served from a web server, the audio control is "disabled" and won't play sound,
// and you can't click the Play button.  I also noticed that the generated code does not always 
//	consistently have quotes around tag attribute values.  So I added a function to add the quotes
//  to the generated code, before it gets used in webpage.


function playMedia(mediaURL,rpt,height,width) {

   document.write( getMediaCode(mediaURL,rpt,height,width) );
}


function reviewCode(theCode) {
	// alert(theCode);
	var reviewedCode='';
	theCode = theCode.replace(/></g, '> <');
	theCode = theCode.replace(/>/g, ' >');
	theCode = theCode.replace(/= /g, '=');
	theCode = theCode.replace(/\n/g, ' ');

	var theParts = new Array();
	theParts = theCode.split(' ');

	for(var i=0; i<theParts.length; i++) {

		// alert(theParts[i]);
		if(theParts[i].indexOf('="')>=0) {
			theParts[i] += ' ';
		}
		else if(theParts[i].indexOf('=')>=0) {
			theParts[i] = theParts[i].replace(/=/, '="') + '" ';
		}
		else {
			theParts[i] += ' ';
		}

		// Include id attribute, so that VALUE can be set to another sound file.
		if(theParts[i].indexOf('NAME="Filename"')>=0) {
			theParts[i] = theParts[i] + theParts[i];
			theParts[i] = theParts[i].replace(/NAME/, 'id');
		}
		// alert(theParts[i]);

		reviewedCode += theParts[i];
	}
	theCode = theCode.replace(/ >/g, '>');
	// alert(reviewedCode);

	return reviewedCode;
}


function getMediaCode(mediaURL,rpt,height,width) {

var mediaURL,rpt,height,width

var theCode = "";

if (GetBrowser() == "Netscape") 
	theCode = embedMPlayer(mediaURL,rpt,height,width);  
if (GetBrowser() == "IE") 
	theCode = embedIEobject(mediaURL,rpt,height,width);
if (navigator.appName.substring(0,5) == "WebTV")
	theCode = embedSource(mediaURL,rpt,height,width);

	theCode = reviewCode(theCode);

return theCode;
}

function embedSource(mediaURL,rpt,height,width) {

    var CodeGen = ""
    var mediaURL,rpt,height,width
 		 	
	 CodeGen = '<embed src="' + mediaURL + '"' + '\n' ;
	 CodeGen += ' height=' + height + ' width=' + width + ' autostart="true"' + '\n'
	 CodeGen += ' LOOP=' + rpt + '>'
	 
    //document.write(CodeGen)
    return CodeGen;

}


function embedMPlayer(mediaURL,rpt,height,width)	{

		CodeGen = "" 
    	var mediaURL,rpt,height,width
				 	
		CodeGen = '<embed type="application/x-mplayer2" ' + '\n' ;
    	CodeGen = CodeGen + ' pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" ' + '\n' ;
	 	CodeGen = CodeGen + 'Name="MediaPlayer" ' + 'src="' + mediaURL + '" ' + '\n' ;
	 	CodeGen = CodeGen + 'autoStart=1 ' ;
		if ((height == 24) && (width == 299)) 
			CodeGen = CodeGen + 'ShowStatusBar=1 '; 
		if ((height >= 50) && (width >= 200)) 
			CodeGen = CodeGen + 'ShowStatusBar=1 '; 
		if ((height <= 49) && (width != 299))
			CodeGen = CodeGen + 'ShowStatusBar=0 '; 
		
		CodeGen = CodeGen + 'playCount=' + rpt + ' ' ;
		CodeGen = CodeGen + 'volume=-1 ' ;
		CodeGen = CodeGen + 'HEIGHT=' + height + ' WIDTH=' + width + '>'
				
		 
		//document.write(CodeGen)
      return CodeGen;
	
}

function embedIEobject(mediaURL,rpt,height,width){

		CodeGen = "" 
    	var mediaURL,rpt,height,width

		CodeGen = '<object id=Player' + '\n' ;
		CodeGen += 'codeBase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902' + '\n' ;
		CodeGen += 'type=application/x-oleobject height=' + height + ' width=' + width + '\n' ;
		CodeGen += ' standby="Loading Microsoft® Windows® Media Player components..." ' + '\n' ;
		CodeGen += 'classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"> ' + '\n' ;
		CodeGen += '<param NAME="Filename" VALUE="' + mediaURL + '">' + '\n' ;
		if ((height == 24) && (width == 299)) 
			CodeGen += '<param NAME="ShowStatusBar" VALUE= "true">';
		if ((height >= 50) && (width >= 200)) 
			CodeGen += '<param NAME="ShowStatusBar" VALUE= "true">'; 
		if ((height <= 49) && (width != 299))
			CodeGen += '<param NAME="ShowStatusBar" VALUE= "false"> ';
		
		CodeGen += '<param NAME="autoStart" VALUE="true"><param NAME="Volume" VALUE="-1">' + '\n' ;
		CodeGen += '<param NAME="playCount" VALUE=' + rpt + '></object>'
		
		//document.write(CodeGen)
      return CodeGen;

}

function GetBrowser()
{
   var agt=navigator.userAgent.toLowerCase();
   if( ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) )
       return "IE";
   else if( ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
         && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
         && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)) )
       return "Netscape";
   else
       return "unknown";
}
//-->
