

// 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 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);
	}
}