
/* MEDIA PLAYER DETECTION */

// TODO: compile to format similar to swfobject
// set some globals so the detection only happens once per page load
var MEDIAALREADYDETECTED = false;
var FLIPDETECTED = false;
var MEDIAPLAYERDETECTED = false;
var QUICKTIMEDETECTED = false;
var VBDETECTIONWRITTEN = false;

function DetectPlayer(id, ftype, width, height, error, params, istest){
	
	// browser detection
	this.UserAgent = navigator.userAgent.toLowerCase();
	this.IsInternetExplorer  = (this.UserAgent.indexOf("msie") != -1);
	this.IsNetscape  = (navigator.appName.indexOf("Netscape") != -1);
	this.IsWindows = ((this.UserAgent.indexOf("win")!=-1) || (this.UserAgent.indexOf("32bit")!=-1));
	this.IsVista = (this.UserAgent.indexOf("windows nt 6")!=-1);
	
	// set defaults for plugin detection
	this.HasFlip = false;
	this.HasMediaPlayer = false;
	this.HasQuickTime = false;

	try{
		
		if(MEDIAALREADYDETECTED == true){
			this.HasFlip = FLIPDETECTED;
			this.HasMediaPlayer = MEDIAPLAYERDETECTED;
			this.HasQuickTime = QUICKTIMEDETECTED;
		
			// display details of test
			if(istest){
				alert("Already detected:\n\nFlip - " + this.HasFlip + "\nMedia Player - " + this.HasMediaPlayer + "\nQuick Time - " + this.HasQuickTime);
			}
			
		}else{

			if(this.IsInternetExplorer && this.IsWindows){
				this.setupVbScript(id, istest);
				if(this.IsVista){
					this.HasMediaPlayer = this.detectWindowsMedia();
					this.HasQuickTime = this.detectQuickTime();
				}
				if(!this.HasMediaPlayer){
					this.HasMediaPlayer = this.detectIE("MediaPlayer.MediaPlayer.1", "Windows Media Player");
				}
				if(!this.HasQuickTime){
					this.HasQuickTime = this.detectIE("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime");
				}
			}else{
				this.HasMediaPlayer = this.detectNS("application/x-mplayer2", "Windows Media Player");
				this.HasQuickTime = this.detectNS("video/quicktime", "QuickTime");
			}
			QUICKTIMEDETECTED = this.HasQuickTime;
			MEDIAPLAYERDETECTED = this.HasMediaPlayer;
			
			this.HasFlip = this.detectFlip();
			FLIPDETECTED = this.HasFlip;
			
			MEDIAALREADYDETECTED = true;
		
			// display details of test
			if(istest){
				alert("Player detected:\n\nFlip - " + this.HasFlip + "\nMedia Player - " + this.HasMediaPlayer + "\nQuick Time - " + this.HasQuickTime);
			}
		
		}
		
		this.LoadPlayer(id, ftype, width, height, error, params, istest);
		
	}catch(ex){
		
		// display details of test
		if(istest){
			alert("DETECTPLAYER ERROR: " + ex + "\n\n" + ex.description + "\n\n" + ex.linenumber);
		}

		// an error occurred so don't load players
		this.HasFlip = false;
		FLIPDETECTED = false;
		this.HasMediaPlayer = false;
		MEDIAPLAYERDETECTED = false;
		this.HasQuickTime = false;
		QUICKTIMEDETECTED = false;
		
		MEDIAALREADYDETECTED = true;
		
	}
	
}

DetectPlayer.prototype.LoadPlayer = function(id, ftype, width, height, error, params, istest){

	var canLoadPlayer = false;
		
	try {
	
		// check if a player was found
		if(this.HasMediaPlayer || this.HasQuickTime){
		
			// determine if its a WMA or WMV file
			var isWmvOrWma = this.isWmvWmaFile(ftype);
			
			// determine if its a MOV or MP4 file
			var isMovOrMp4 = this.isMovOrMp4File(ftype)
			
			// check for windows media specific files
			if(isWmvOrWma){
				// check for media player or quicktime with flip plugin
				if(this.HasMediaPlayer || (this.HasQuickTime && this.HasFlip)){
	
					// display details of test
					if(istest){
						alert("This is a MediaPlayer file.\nLoaded: I either have MediaPlayer or QuickTime with Flip.");
					}
					
					//return true;
					//this.HasPlayer = true;
					canLoadPlayer = true;
				}
				// neither player was detected
				else{
	
					// display details of test
					if(istest){
						alert("This is a MediaPlayer file, but couldn't load.\nReasons: I don't have MediaPlayer or QuickTime with Flip.");
					}
					
					//return false;
					//this.HasPlayer = false;
					canLoadPlayer = false;
				}
			}else{
			
				// check for quicktime specific files
				if(isMovOrMp4){
	
					// display details of test
					if(istest){
						alert("This is a QuickTime file.\nLoaded: I have QuickTime.");
					}
					
					//return this.HasQuickTime;
					//this.HasPlayer = this.HasQuickTime;
					canLoadPlayer = this.HasQuickTime;
				}
				
				// no player specific file detected, lets just see if a player is detected (media player is the dominate player)
				else{
	
					// display details of test
					if(istest){
						alert("Attempting to load media file.\nCan I load it? " + (this.HasMediaPlayer || this.HasQuickTime));
					}
					
					//return (this.HasMediaPlayer || this.HasQuickTime);
					//this.HasPlayer = (this.HasMediaPlayer || this.HasQuickTime);
					canLoadPlayer = (this.HasMediaPlayer || this.HasQuickTime);
				}
				
			}
			
		}else{
		
			// display details of test
			if(istest){
				alert("I don't have MediaPlayer or QuickTime installed.");
			}
			
			//return false;
			//this.HasPlayer = false;
			canLoadPlayer = false;
		}
		
	}catch(ex){
		
		// display details of test
		if(istest){
			alert("LOADPLAYER ERROR: " + ex + "\n\n" + ex.description);
		}
		
		//return false;
		//this.HasPlayer = false;
		canLoadPlayer = false;
	}
	
	if(canLoadPlayer){
		this.buildPlayerCode(id, ftype, width, height, params, istest);
	}else{
		var strPlayer = "";
		if(error){
			if(typeof CustomMediaError !== 'undefined'){
				strPlayer = '<div class="Media_Error Player_Not_Supported Custom_Error">' + CustomMediaError + '</div>' + "\n";
			}else{
				strPlayer = '<div class="Media_Error Player_Not_Supported">' + "\n";
				strPlayer += '<table cellpadding="2" cellspacing="0" border="0" width="' + width + '">' + "\n";
				strPlayer += '<tr>' + "\n";
				strPlayer += '<td colspan="3" align="center"><h3>Player Not Found:</h3></td>' + "\n";
				strPlayer += '</tr>' + "\n";
				if(this.isWmvWmaFile(ftype)){
					if(this.HasQuickTime && !this.HasFlip){
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td><img src="/SiteCM3/i/flip4mac_icon.jpg" alt="Flip4Mac" width="34" height="34" border="0" align="left" hspace="2" vspace="2" /></td>' + "\n";
						strPlayer += '<td nowrap="true">Flip4Mac (QuickTime plug-in):</td>' + "\n";
						strPlayer += '<td><a href="http://www.apple.com/downloads/macosx/video/flip4macwindowsmediacomponentsforquicktime.html" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
					}else{
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td rowspan="4" align="center">Windows<br />Media Player<br /><img src="/SiteCM3/i/mediaplayer_icon.jpg" alt="Windows Media Player" width="34" height="34" border="0" hspace="2" vspace="2" /></td>' + "\n";
						strPlayer += '<td nowrap="true">Microsoft Windows:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/default.mspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mac OS X:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/player/mac/mp9/default.aspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mac OS 8.1 to 9.x:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/player/mac/mp71/default.aspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mozilla Firefox:</td>' + "\n";
						strPlayer += '<td><a href="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
					}
				}else{
					if(!this.HasMediaPlayer){
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td rowspan="4" align="center">Windows<br />Media Player<br /><img src="/SiteCM3/i/mediaplayer_icon.jpg" alt="Windows Media Player" width="34" height="34" border="0" hspace="2" vspace="2" /></td>' + "\n";
						strPlayer += '<td nowrap="true">Microsoft Windows:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/default.mspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mac OS X:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/player/mac/mp9/default.aspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mac OS 8.1 to 9.x:</td>' + "\n";
						strPlayer += '<td><a href="http://www.microsoft.com/windows/windowsmedia/player/mac/mp71/default.aspx" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td nowrap="true">Mozilla Firefox:</td>' + "\n";
						strPlayer += '<td><a href="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
					}
					if(!this.HasQuickTime){
						strPlayer += '<tr>' + "\n";
						strPlayer += '<td><img src="/SiteCM3/i/quicktime_icon.jpg" alt="QuickTime" width="34" height="34" border="0" align="left" hspace="2" vspace="2" /></td>' + "\n";
						strPlayer += '<td nowrap="true">Apple QuickTime:</td>' + "\n";
						strPlayer += '<td><a href="http://www.apple.com/quicktime/download" target="_blank">download</a></td>' + "\n";
						strPlayer += '</tr>' + "\n";
					}
				}
				strPlayer += '<tr>' + "\n";
				strPlayer += '<td colspan="3"><p align="center">Please install a supported media player and reload the page.</p></td>' + "\n";
				strPlayer += '</tr>' + "\n";
				strPlayer += '</table>' + "\n";
				strPlayer += '</div>' + "\n";
			}
		}
		getObjStyle("SiteCM_Media_" + id + "_Container").display = "block";
		getObj("SiteCM_Media_" + id + "_Container").innerHTML = strPlayer;
	}
	
}

DetectPlayer.prototype.detectIE = function(ClassID, name){
    document.writeln('<scr' + 'ipt language="vbscript">');
    document.writeln('on error resume next');
    document.writeln('result = IsObject(CreateObject("' + ClassID + '"))');
    document.writeln('</scr' + 'ipt>');
	if(result){
		return true;
	}
	return false;
}

DetectPlayer.prototype.detectNS = function(ClassID, name){
	var nse = "";
	for (var i=0;i<navigator.mimeTypes.length;i++){
		nse += navigator.mimeTypes[i].type.toLowerCase();
	}
	if(nse.indexOf(ClassID) != -1){
		if(navigator.mimeTypes[ClassID].enabledPlugin != null){
			return true;
		}
	}
	return false;
}

DetectPlayer.prototype.detectQuickTime = function() {
    var foundPlugin = this.detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!foundPlugin && isDetectableWithVB){
		foundPlugin = this.detectQuickTimeActiveXControl();
    }
    return foundPlugin;
}

DetectPlayer.prototype.detectFlip = function(){
    return this.detectPlugin('Flip4Mac');
}

DetectPlayer.prototype.detectWindowsMedia = function(){
    var foundPlugin = this.detectPlugin('Windows Media', 'Pl');
    // if not found, try to detect with VisualBasic
    if(!foundPlugin && isDetectableWithVB){
		foundPlugin = this.detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return foundPlugin;
}

DetectPlayer.prototype.detectPlugin = function(){
    // allow for multiple checks in a single pass
    var daPlugins = this.detectPlugin.arguments;
    // consider foundPlugin to be false until proven true
    var foundPlugin = 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){
				foundPlugin = true;
				// if we've found the plugin, we can stop looking through at the rest of the plugins
				break;
		    }
		}
    }
    return foundPlugin;
}

DetectPlayer.prototype.isWmvWmaFile = function(filetype){
	if(filetype == 'wma' || filetype == 'wmv'){
		return true;
	}
	return false;
}

DetectPlayer.prototype.isMovOrMp4File = function(filetype){
	if(filetype == 'mov' || filetype == 'm3u' || filetype == 'mp4' || filetype == 'm4a'){
		return true;
	}
	return false;
}

var isDetectableWithVB = false;

DetectPlayer.prototype.setupVbScript = function(id, istest){
	if(!VBDETECTIONWRITTEN){
		var vbScript = "";

		vbScript = '<scr' + 'ipt language="vbscript">' + "\n";

		vbScript += '\'do a one-time test for a version of VBScript that can handle this code' + "\n";
		vbScript += 'isDetectableWithVB = False' + "\n";
		vbScript += 'If ScriptEngineMajorVersion >= 2 Then' + "\n";
		vbScript += '  isDetectableWithVB = True' + "\n";
		vbScript += 'End If' + "\n";

		vbScript += '\'this next function will detect most plugins' + "\n";
		vbScript += 'Function detectActiveXControl(activeXControlName)' + "\n";
		vbScript += '  on error resume next' + "\n";
		vbScript += '  detectActiveXControl = False' + "\n";
		vbScript += '  If isDetectableWithVB Then' + "\n";
		vbScript += '     detectActiveXControl = IsObject(CreateObject(activeXControlName))' + "\n";
		vbScript += '  End If' + "\n";
		vbScript += 'End Function' + "\n";

		vbScript += '\'and the following function handles QuickTime' + "\n";
		vbScript += 'Function detectQuickTimeActiveXControl()' + "\n";
		vbScript += '  on error resume next' + "\n";
		vbScript += '  detectQuickTimeActiveXControl = False' + "\n";
		vbScript += '  If isDetectableWithVB Then' + "\n";
		vbScript += '    detectQuickTimeActiveXControl = False' + "\n";
		vbScript += '    hasQuickTimeChecker = False' + "\n";
		vbScript += '    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")' + "\n";
		vbScript += '    If IsObject(hasQuickTimeChecker) Then' + "\n";
		vbScript += '      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then' + "\n";
		vbScript += '        detectQuickTimeActiveXControl = True' + "\n";
		vbScript += '      End If' + "\n";
		vbScript += '    End If' + "\n";
		vbScript += '  End If' + "\n";
		vbScript += 'End Function' + "\n";

		vbScript += '</scr' + 'ipt>' + "\n";

		getObj("SiteCM_Media_" + id + "_VB_Detection").innerHTML = vbScript;
		
		VBDETECTIONWRITTEN = true;
		
		// display details of test
		if(istest){
			alert("VB detection script written.");
		}
	}
}

DetectPlayer.prototype.buildPlayerCode = function(id, ftype, width, height, params, istest){
	var strPlayer = "";
	
	try {
		
		// determine if its a WMA or WMV file
		var isWmvOrWma = this.isWmvWmaFile(ftype);
		
		// determine if its a MOV or MP4 file
		var isMovOrMp4 = this.isMovOrMp4File(ftype)

		strPlayer = '<object' + "\n";
		strPlayer += ' id="SiteCM_Media_' + id + '"' + "\n";
		strPlayer += ' width="' + width + '"' + "\n";
		if(this.HasMediaPlayer && !isMovOrMp4){
			// Media Player parameters
			strPlayer += ' classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95"' + "\n";
			strPlayer += ' codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902"' + "\n";
			strPlayer += ' type="application/x-oleobject"' + "\n";
			if(this.IsInternetExplorer){
				strPlayer += ' height="' + (height+40) + '' + "\n";
			}else{
				if(this.HasQuickTime && this.HasFlip){
					// Quicktime parameters
					strPlayer += ' height="' + (height+15) + '"' + "\n";
				}else{
					strPlayer += ' height="' + (height+65) + '"' + "\n";
				}
			}
		}else{
			// Quicktime parameters
			strPlayer += ' height="' + (height+15) + '"' + "\n";
			strPlayer += ' classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' + "\n";
			strPlayer += ' codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0"' + "\n";
		}
		strPlayer += '>' + "\n";
		strPlayer += '<param name="volume" value="' + params.volume + '" />' + "\n";
		if(this.HasMediaPlayer && !isMovOrMp4){
			// Media Player parameters
			strPlayer += '<param name="filename" value="' + params.filename + '" />' + "\n";
			if(this.IsInternetExplorer){
				strPlayer += '<param name="autostart" value="' + params.autostart + '" />' + "\n";
			}else{
				strPlayer += '<param name="src" value="' + params.filename + '" />' + "\n";
				strPlayer += '<param name="autostart" value="';
				if(params.autostart == "true"){
					strPlayer += '1';
				}else{
					strPlayer += '0';
				}
				strPlayer += '" />' + "\n";
			}
		}else{
			// Quicktime parameters
			strPlayer += '<param name="src" value="' + params.filename + '" />' + "\n";
			strPlayer += '<param name="autoplay" value="' + params.autostart + '" />' + "\n";
		}
		strPlayer += '<embed' + "\n";
		strPlayer += ' src="' + params.filename + '"' + "\n";
		strPlayer += ' name="SiteCM_Media_' + id + '"' + "\n";
		strPlayer += ' volume="' + params.volume + '"' + "\n";
		strPlayer += ' width="' + width + '"' + "\n";
		if(this.HasMediaPlayer && !isMovOrMp4){
			// Media Player parameters
			strPlayer += ' type="application/x-mplayer2"' + "\n";
			strPlayer += ' pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"' + "\n";
			if(this.IsInternetExplorer){
				strPlayer += ' height="' + (height+40) + '"' + "\n";
				strPlayer += ' autostart="' + params.autostart + '"' + "\n";
			}else{
				if(this.HasQuickTime && this.HasFlip){
					// Quicktime parameters
					strPlayer += ' height="' + (height+15) + '"' + "\n";
				}else{
					strPlayer += ' height="' + (height+65) + '"' + "\n";
				}
				strPlayer += ' autostart="';
				if(params.autostart == "true"){
					strPlayer += '1';
				}else{
					strPlayer += '0';
				}
				strPlayer += '"' + "\n";
			}
		}else{
			// Quicktime parameters
			strPlayer += ' pluginspage="http://www.apple.com/quicktime/download/"' + "\n";
			strPlayer += ' height="' + (height+15) + '"' + "\n";
			strPlayer += ' autoplay="' + params.autostart + '"' + "\n";
		}
		strPlayer += ' />' + "\n";
		strPlayer += '</object>' + "\n";
	
		if(istest){
			alert(strPlayer);
		}
		getObjStyle("SiteCM_Media_" + id + "_Container").display = "block";
		getObj("SiteCM_Media_" + id + "_Container").innerHTML = strPlayer;
	
	}catch(ex){
		
		// display details of test
		if(istest){
			alert("BUILDPLAYERCODE ERROR: " + ex + "\n\n" + ex.description);
		}
		
		return null;
	
	}
	
}

