

// mac os or not?
var defaultWidth=320;
var defaultHeight=320;
var MediaObject = function (mediaType,height,width){
	this.mediaType = mediaType;
	this.height = height;
	this.width = width;
	this.setupPlayer=tiiMedia_setupPlayer;
	if(this.mediaType==null && this.mediaType==""){
		this.mediaType = 'video';	
	}
	if(this.height==null){
		this.height=defaultHeight;
	}
	if(this.width==null){
		this.width=defaultWidth;
	}
	this.params = new Array();
	this.keys = new Array();
	this.sources=new Array();
}
// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}
// -->
function detectQuickTime(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!isMac && !pluginFound && detectableWithVB) {
		pluginFound = detectQuickTimeActiveXControl();
    }
    return pluginFound;
}

function detectReal(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!isMac && !pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return pluginFound;
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Windows Media Player');
    // if not found, try to detect with VisualBasic
    if(!isMac && !pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return pluginFound;
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin

MediaObject.prototype.getFileNotFoundMessage = function() {
	return 'This file is not available. Check back soon!';
}
MediaObject.prototype.addSource=function(name,value){
	this.sources[name]=value;
}
MediaObject.prototype.addParam=function(name,value){
	this.params[name]=value;
	this.keys.push(name);
}
MediaObject.prototype.addDefaultParam=function(name,value){
	if(this.params[name]==null){
		this.addParam(name,value);
	}
}
var drawingPlayer=null;
MediaObject.prototype.write = function (playerElementID, errorMessageElementID) {
	this.playerElement = document.getElementById(playerElementID);
	if(errorMessageElementID==null){
		errorMessageElementID=playerElementID;
	}
	this.errorMessageElement = document.getElementById(errorMessageElementID);
	drawingPlayer = this;
	drawingPlayer.setupAndDrawPlayer();
}
MediaObject.prototype.setupAndDrawPlayer=function(){
	if (typeof this.playerElement != 'undefined' && typeof this.errorMessageElement != 'undefined') {	
		this.playerHTML = '';
		this.errorMessageHTML = '';

		// file exists or not?
		var fileExists = false;

		var showObject=false;
		this.setupPlayer();
		if(this.mediaPlayer!=null){
			if(this.sources[this.mediaPlayer.id]!=""){
				this.addParam("src",this.sources[this.mediaPlayer.id]);
				fileExists=true;
			}
		}
		if (fileExists && this.errorMessageHTML=="") {
			this.drawPlayerIMPL();
		} else if(this.mediaPlayer!=null){
			this.errorMessageHTML = this.getFileNotFoundMessage();
		}
		if (this.playerHTML == '' && this.errorMessageHTML!='') {
			this.errorMessageElement.innerHTML = this.errorMessageHTML;
		}	
	}
}
MediaObject.prototype.drawPlayerIMPL=function(){
	var showObject=false;
	if(showObject){
		this.playerHTML += '<object id="'+this.id+'" width="'+this.width+'" height="'+this.height+'"  />';
		for(var x=0;x<this.keys.length;x++){
			this.playerHTML+='<param name="'+this.keys[x]+'" value="'+this.params[this.keys[x]]+'" />';
		}
	}
	this.playerHTML+='<embed name="'+this.id+'"  width="'+this.width+'" height="'+this.height+'"';
	for(var x=0;x<this.keys.length;x++){
		this.playerHTML+= ' '+this.keys[x]+' ="'+this.params[this.keys[x]]+'"';
	}
	this.playerHTML+=" />";	
	if(showObject){
		this.playerHTML+="</object>";
	}
	this.playerElement.innerHTML = this.playerHTML;
}

function getPluginNotInstalledMessage(plugin, downloadURL) {
	return 'You must have ' + plugin + ' installed to play this content. Please download it <a href="' + downloadURL + '" target="_new">here</a>.';
}
MediaPlayer=function(id,setupHandler){
	this.id=id;
	this.setup=setupHandler;
}
var mediaPlayers = new Array();
function addPlayer(playerId,setupHandler){
	var player=new MediaPlayer(playerId,setupHandler);
	mediaPlayers[playerId]=player;
}


function setupQT(mediaObj){
	mediaObj.id="QTPlayer";
	mediaObj.addDefaultParam("autoplay","true");
	mediaObj.addDefaultParam("loop","0");
	mediaObj.addDefaultParam("controller","1");
}
function setupWMP(mediaObj){
	mediaObj.id="WMPlayer";
	mediaObj.addParam("type","application/x-mplayer2");
	mediaObj.addDefaultParam("enabled","true");
	mediaObj.addDefaultParam("autostart","1");
	mediaObj.addDefaultParam("showcontrols","1");
	mediaObj.addDefaultParam("TransparentatStart","true");
	mediaObj.addDefaultParam("uiMode","full");
}
function setupDalaiLama(mediaObj){
	mediaObj.id="DLPlayer";
	mediaObj.drawPlayerIMPL=drawDalaiLama;
}
function drawDalaiLama(){
	document.write('<script type="text/javascript">DalaiLlama.PlayVideoOldMedium("'+this.sources[this.mediaPlayer.id]+'")</script>');
}
this.addPlayer("QuickTime",setupQT);
this.addPlayer("WindowsMedia",setupWMP);
this.addPlayer("DalaiLama",setupDalaiLama);
var isMac = (navigator.userAgent.toLowerCase().indexOf('mac') >= 0);


function tiiMedia_setupPlayer(){	
	this.mediaPlayer=null;	
	if (isMac) {
		if (detectQuickTime()) {
			this.mediaPlayer=mediaPlayers["QuickTime"];
		} else {
			this.errorMessage=getPluginNotInstalledMessage('QuickTime', 'http://www.apple.com');
		}
	}else{
		if(detectWindowsMedia()) {
			this.mediaPlayer=mediaPlayers["WindowsMedia"];
		} else {
			this.errorMessageHTML = getPluginNotInstalledMessage('Windows Media', 'http://www.microsoft.com');
		}	
	}
	if(this.mediaPlayer!=null){
		this.mediaPlayer.setup(this);
	}
}
function buildPlayer(url) {

	var isMac = (navigator.userAgent.toLowerCase().indexOf("mac") >= 0) ? 1: 0;
	
	if (isMac) {
		//alert ('We are loading the QuickTime player because we are on a Mac');
		document.write('<object id="QTplayer" width="320" height="16" classid="CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"> <param name="src" value="' + url + '" /> <param name="autoplay" value="true" /> <param name="loop" value="0" /> <param name="controller" value="1" /><embed name="QTplayer" src="' + url + '" width="320" height="16" loop="false" controller="true" autoplay="1" controller="1" pluginspage="http://www.apple.com/quicktime/" /> </object>');
	} else {
		//alert ('We are loading the Windows Media player because we are NOT on a Mac');
		document.write('<object id="WMplayer" width="320" height="44" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"> <param name="url" value="' + url + '" /> <param name="enabled" value="true" /> <param name="autostart" value="1" /> <param name="showcontrols" value="1" /> <param name="TransparentatStart" value="true" /> <param name="uiMode" value="full" /> <embed name="WMplayer" type="application/x-mplayer2" src="' + url + '" width="320" height="44" autostart="1" showcontrols="1" /> </object>');
	}
}


function tiiMedia_setupPlayer(){	
	this.mediaPlayer=null;	
	if (detectQuickTime()) {
		this.mediaPlayer=mediaPlayers["QuickTime"];
	}else if(!isMac && detectWindowsMedia()){
		this.mediaPlayer=mediaPlayers["WindowsMedia"];
	} else {
		this.errorMessage=getPluginNotInstalledMessage('QuickTime', 'http://www.apple.com');
	}
	if(this.mediaPlayer!=null){
		this.mediaPlayer.setup(this);
	}
}


function displayVideo(wmPath, qtPath){
	var height=256;
	var width=320;
	var mediaObj = new MediaObject('video',height,width);
	if (wmPath != "") {
		mediaObj.addSource("WindowsMedia",wmPath);
	}
	if (qtPath != "") {
		mediaObj.addSource("QuickTime",qtPath);
	}
	mediaObj.write('AVPlayer');		
}



function displayAudio(wmPath, qtPath, mp3Path){
	var height=24;
	var width=320;
	var mediaObj = new MediaObject('audio',height,width);

	if (mp3Path != "") {
		buildPlayer(mp3Path);
	}
	if (wmPath != "") {
		mediaObj.addSource("WindowsMedia",wmPath);
	}
	if (qtPath != "") {
		mediaObj.addSource("QuickTime",qtPath);
	}
	mediaObj.write('AVPlayer');	
	
}

function showCenteredPopup(name, url, features, width, height) {
	// example usage:
	// showCenteredPopup("foo", "http://www.cnn.com", null, 640, 480);
	
	var top = (screen.height / 2) - height / 2;
	var left = (screen.width / 2) - width / 2;

	if (features == null || features == '') {
		features = "scrollbars=yes,toolbar=no,menubar=no,status=no,location=no";
	}

	window.open(url, name, features + ",top=" + top + ",left=" + left + ",width=" + width + ",height=" + height);

}

