//version 2.0.2.1
// initialize global variables
var detectableWithVB = false;
var pluginFound = false;
var javascriptVersion1_1 = true;

var flashNames = new MediaPlayerName(new Array("Shockwave", "Flash"), new Array("ShockwaveFlash.ShockwaveFlash.1") );
var realNames = new MediaPlayerName(new Array("RealPlayer"), new Array("rmocx.RealPlayer G2 Control", "RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)", "RealVideo.RealVideo(tm) ActiveX Control (32-bit)"));
var directorNames = new MediaPlayerName(new Array("Shockwave", "Director"), new Array("SWCtl.SWCtl.1"));
var quicktimeNames = new MediaPlayerName(new Array("QuickTime Plug-in"), new Array("QuickTimeCheckObject.QuickTimeCheck.1"));
var mediaplayerNames = new MediaPlayerName(new Array("Windows Media Player Plug-in Dynamic Link Library"), new Array("MediaPlayer.MediaPlayer.1"));
var vlcNames = new MediaPlayerName(new Array("VLC"), new Array());
var activeXNames = new MediaPlayerName(new Array("ActiveX"), new Array());
var activeXPlugin4FirefoxNames = new MediaPlayerName(new Array("Mozilla ActiveX control and plugin support"), new Array());
var wmpPlugin4FirefoxNames = new MediaPlayerName(new Array("Windows Media Player Firefox Plugin"), new Array());

/**
    Change current browser url
*/
function goURL(daURL) {
    // if the browser can do it, use replace to preserve back button
    if (javascriptVersion1_1) {
        window.location.replace(daURL);
    } else {
        window.location = daURL;
    }
    return;
}
function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if (redirectURL && ((pluginFound && redirectIfFound) || (!pluginFound && !redirectIfFound))) {
    // go away
        goURL(redirectURL);
        return pluginFound;
    } else {
    // stay here and return result of plugin detection
        return pluginFound;
    }
}
function canDetectPlugins() {
    if (detectableWithVB || (navigator.plugins && navigator.plugins.length > 0)) {
        return true;
    } else {
        return false;
    }
}

/**
    Method looks in the navigator.plugins array, to see if the plugin name (in parameter) is present.
    return true if plugun name is present.
    several plugin name can be passed in parameter : allow for multiple checks in a single pass
*/
function getPlugin(arguments) {
    var daPlugins = arguments;
    var pluginFound = false;

    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
        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 ((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)) {
                    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) {
                return navigator.plugins[pluginsArrayCounter];
                break;
            }
        }
    }
    return null;
}

function getPlayerDescription(arg) {
    var plugin = getPlugin(arg);
    if (plugin == null){
        
    }
    else {
        return plugin.name + " <br/> " + plugin.description;
    }
}

/**
    Here we write out the VBScript block for MSIE Windows.
    Uses VBScript to test for the COM object.
    Make a supp action for quickTime : IsQuickTimeAvailable.
*/
if ((navigator.userAgent.indexOf("MSIE") != -1) && (navigator.userAgent.indexOf("Win") != -1)) {
    document.writeln("<script language=\"VBscript\">");
    document.writeln("'do a one-time test for a version of VBScript that can handle this code");
    document.writeln("detectableWithVB = False");
    document.writeln("If ScriptEngineMajorVersion >= 2 then");
    document.writeln("  detectableWithVB = True");
    document.writeln("End If");
    document.writeln("'this next function will detect most plugins");
    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("'and the following function handles QuickTime");
    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>");
}

/***************************************************************/
/*    Functions for each player                                */
/***************************************************************/
/**
    namesToCheck : an object containing plugins names and activex names, to make the research
*/
function getPlayerDescription(namesToCheck, redirectURL, redirectIfFound) {
    var plugin = getPlugin(namesToCheck.pluginNames);
    if (plugin != null){
        return plugin.name + " " + plugin.description;
    }
    else{
        if (detectableWithVB) {
            for (i = 0; i < namesToCheck.activeXNames.length; i++) {
                namesToCheck.activeXNames[i];
            }
        }
    }
    return "";
}

/**
    namesToCheck : an object containing plugins names and activex names, to make the research
*/
function detectPlayer(namesToCheck, redirectURL, redirectIfFound) {
    pluginFound = getPlugin(namesToCheck.pluginNames) != null;
    // if not found, try to detect with VisualBasic
    if (!pluginFound && detectableWithVB) {
        for (i = 0; i < realNames.activeXNames.length; i++) {
            if (detectActiveXControl(namesToCheck.activeXNames[i])){
                pluginFound = true;
            }
        }
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectVlc(redirectURL, redirectIfFound) {
    return detectPlayer(vlcNames, redirectURL, redirectIfFound);
}
function detectFlash(redirectURL, redirectIfFound) {
    return detectPlayer(flashNames, redirectURL, redirectIfFound);
}
function detectReal(redirectURL, redirectIfFound) {
    return detectPlayer(realNames, redirectURL, redirectIfFound);
}
function detectDirector(redirectURL, redirectIfFound) {
    return detectPlayer(directorNames, redirectURL, redirectIfFound);
}
function detectWindowsMedia(redirectURL, redirectIfFound) {
    return detectPlayer(mediaplayerNames, redirectURL, redirectIfFound);
}
function detectActiveX(redirectURL, redirectIfFound) {
    if (navigator.appName == "Microsoft Internet Explorer")
        return true;
    return detectPlayer(activeXNames, redirectURL, redirectIfFound);
}
function detectQuickTime(redirectURL, redirectIfFound) {
    var pluginFound = detectPlayer(quicktimeNames, redirectURL, false);
    if (!pluginFound && detectableWithVB) {
        pluginFound = detectQuickTimeActiveXControl();
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}
function detectFirefoxWMPPlugin(redirectURL, redirectIfFound) {
	return detectPlayer(wmpPlugin4FirefoxNames,redirectURL, redirectIfFound);
}
function detectActiveXPlugin(redirectURL, redirectIfFound) {
	return detectPlayer(activeXPlugin4FirefoxNames,redirectURL, redirectIfFound);
}

function getVlcDescription() {
    return getPlayerDescription(vlcNames);
}
function getFlashDescription() {
    return getPlayerDescription(flashNames);
}
function getRealDescription() {
    return getPlayerDescription(realNames);
}
function getDirectorDescription() {
    return getPlayerDescription(directorNames);
}
function getWindowsMediaDescription() {
    return getPlayerDescription(mediaplayerNames);
}
function getQuickTimeDescription() {
    return getPlayerDescription(quicktimeNames);
}
function getActiveXDescription() {
    return getPlayerDescription(activeXNames);
}
function getFirefoxWMPPluginDescription() {
    return getPlayerDescription(wmpPlugin4FirefoxNames);
}
function getFirefoxActiveXPluginDescription() {
    return getPlayerDescription(activeXPlugin4FirefoxNames);
}
function isMediaConfigurationOK() {
	return false;
}

function infoConfig(idDiv, playerType) {
	video_msg(idDiv,"", infoConfiguration(playerType));
}

function infoConfigGenerale(idDiv) {
	video_msg(idDiv,"", infoConfigurationGenerale());
}

function infoPlayers(idDiv, playerType) {
	video_msg(idDiv,"Players and plugins", infoPlayersConfiguration(playerType));	
}


function infoConfigurationGenerale() {

	var configStr = "";

	configStr+= "Operating system : " + browser.os + " - " + browser.winver + "<br/>";
	configStr+= "Browser : " + browser.browserName + " - " + browser.fullVersion + "<br/>";

	return configStr;
}

function infoConfiguration(playerType) {
	if (playerType != "wmv" && playerType != "mp4"){
		playerType = "wmv";
	}	
	var configStr = "";

	configStr+= "Windows Media Player : " + (detectWindowsMedia() ? "installed" : "not detected") + " (version " + getWindowsMediaPlayerVersion() + ")" + "<br/>";
	configStr+= "QuickTime : " + (detectQuickTime() ? "installed" : " not detected")+ " (version " + getQuickTimeVersion() + ")" + "<br/>";
	if (browser.browserName != "Microsoft Internet Explorer") {
		configStr+= "Mozilla ActiveX control and plugin support : " + (detectActiveXPlugin() ? "installed" : " not detected") + "<br/>";
		configStr+= "Windows Media Player Firefox Plugin : " + (detectFirefoxWMPPlugin() ? "installed" : " not detected") + "<br/>";
	}
	   
	return configStr;
}

function infoPlayersConfiguration(playerType) {
	if (playerType != "wmv" && playerType != "mp4"){
		playerType = "wmv";
	}	
	var playersStr = "";
	if (browser.os == "Windows") {
		if (browser.browserName == "Microsoft Internet Explorer") {
			if (playerType == "wmv" && !detectWindowsMedia()) {
				playersStr += "You should install the recommended version of Windows Media Player, according to your operating system, see on <a href='http://www.microsoft.com/windows/windowsmedia/player/versions.aspx' target='_blank' >Microsoft website</a><br/>";
			}
			else if (playerType == "mp4" && !detectQuickTime()) {
				playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
			}
		}
		else if (browser.browserName == "Firefox") {
			if (browser.majorVersion == "1") {
				playersStr += "You should install last version of Firefox, see on <a href='http://www.mozilla-europe.org/en/products/firefox/' target='_blank' >Mozilla website</a><br/>";
				playersStr += "You may have to install additional plugins<br/>"
			}
			else if (browser.majorVersion == "2") {
				if (playerType == "wmv") {
					if (!detectWindowsMedia()) {
						playersStr += "You should install the recommended version of Windows Media Player, according to your operating system, see on <a href='http://www.microsoft.com/windows/windowsmedia/player/versions.aspx' target='_blank' >Microsoft website</a><br/>";
					}
					if (!detectActiveXPlugin())	{
						playersStr += "You should install ActiveX plugin for Firefox, see this <a href='http://forums.mozillazine.org/viewtopic.php?t=206213#FF_1' target='_blank' >guide</a><br/>";
					}
				}
				if (playerType == "mp4" && !detectQuickTime()) {
					playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
				}
			}
			else if (browser.majorVersion >= "3") {
				if (playerType == "wmv") {
					if (!detectFirefoxWMPPlugin()) {
					playersStr += "You should install Windows Media Player plugin for Firefox, download it <a href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx' target='_blank'>here</a><br/>";
					}
				}
				if (playerType == "mp4" && !detectQuickTime()) {
					playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
				}
			}
		}
		else if (browser.browserName == "Safari") {
			if (playerType == "wmv") {
				if (!detectFirefoxWMPPlugin()) {
				playersStr += "You should install Windows Media Player plugin for Firefox, download it <a href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx' target='_blank'>here</a><br/>";
				}
			}
			if (playerType == "mp4" && !detectQuickTime()) {
				playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
			}
		}
		else if (browser.browserName == "Chrome") {
			if (playerType == "wmv") {
				if (!detectFirefoxWMPPlugin()) {
				playersStr += "You should install Windows Media Player plugin for Firefox, download it <a href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx' target='_blank'>here</a><br/>";
				}
			}
			if (playerType == "mp4" && !detectQuickTime()) {
				playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
			}
		}
		else if (browser.browserName == "Opera") {
			if (playerType == "wmv") {
				if (!detectFirefoxWMPPlugin()) {
				playersStr += "You should install Windows Media Player plugin for Firefox, download it <a href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx' target='_blank'>here</a><br/>";
				}
			}
			if (playerType == "mp4" && !detectQuickTime()) {
				playersStr += "You should install QuickTime, see on <a href='http://www.apple.com/quicktime/download/' target='_blank' >Apple website</a><br/>";
			}
		}
	}
	else if (browser.os == "Mac") {
			playersStr += "Mac OS : message to be defined";
	}
	else {
		playersStr += "Other browser : message to be defined";
	}
	if (playersStr == "")
		playersStr = "Your configuration seems fine";
	return playersStr;
	
}
function getWindowsMediaPlayerVersion() {
	if (detectWindowsMedia()) {
		var v_mediaplayer;
		var a_mediaplayer;
		
		var i_mediaplayer_viamm;
		var a_mediaplayer_viamm;
		try {
		if ("ActiveXObject" in window) {
			i_mediaplayer_viamm = new ActiveXObject("MediaPlayer.MediaPlayer.1");
			a_mediaplayer_viamm = " via ActiveXObject";
		} else if ("GeckoActiveXObject" in window) {
			i_mediaplayer_viamm = new GeckoActiveXObject("MediaPlayer.MediaPlayer.1");
			a_mediaplayer_viamm = " via GeckoActiveXObject";
		}
		} catch(e) {
		//alert(e);
		}
		var i_mediaplayer_viamo;
		var a_mediaplayer_viamo;
		try {
		if ("ActiveXObject" in window) {
			i_mediaplayer_viamo = new ActiveXObject("WMPlayer.OCX.7");
			a_mediaplayer_viamo = " via ActiveXObject";
		} else if ("GeckoActiveXObject" in window) {
			i_mediaplayer_viamo = new GeckoActiveXObject("WMPlayer.OCX.7");
			a_mediaplayer_viamo = " via GeckoActiveXObject";
			v_mediaplayer = i_mediaplayer_viamo.versionInfo;
		}
		} catch(e) {
		//alert(e);
		}
		var i_mediaplayer6_viaci;
		var a_mediaplayer6_viaci;
		try {
		if ("GeckoActiveXObject" in window) {
			i_mediaplayer6_viaci = new GeckoActiveXObject("{22D6F312-B0F6-11D0-94AB-0080C74C7E95}");
			a_mediaplayer6_viaci = " via GeckoActiveXObject";
		}
		} catch(e) {
		//alert(e);
		}
		var i_mediaplayer9_viaci;
		var a_mediaplayer9_viaci;
		try {
		if ("GeckoActiveXObject" in window) {
			i_mediaplayer9_viaci = new GeckoActiveXObject("{6BF52A52-394A-11d3-B153-00C04F79FAA6}");
			a_mediaplayer9_viaci = " via GeckoActiveXObject";
		}
		} catch(e) {
		//alert(e);
		}	
		
		return v_mediaplayer;
	}
	else
		return false;
}

function getQuickTimeVersion() {
	if (detectQuickTime()){
		var v_quicktime;
		if (navigator.plugins && navigator.plugins.length > 0) {
	        var pluginsArrayLength = navigator.plugins.length;
	        for (pluginsArrayCounter = 0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++) {
	            // loop through all desired names and check each against the current plugin name
	            var numFound = 0;
	            
	            if (navigator.plugins[pluginsArrayCounter].name.indexOf(quicktimeNames) >= 0) {
	                v_quicktime = navigator.plugins[i].name.substring(17, navigator.plugins[i].name.length);
	                break;
	            }
	        }
	        return v_quicktime;
	    }
	}
	else
		return false;
}

function throwConfigurationException() {
	throw "ConfigurationException";
}
/**
    Class to store plugin and activex names of the players 
*/
function MediaPlayerName(pluginNames, activeXNames){
    this.pluginNames = pluginNames;
    this.activeXNames = activeXNames;
}

/**
 * This method can be used to know the current plugins.
 */
function buildDebugMessageOnDetectedPlayer(){
	var debugMessage = "";
	debugMessage += "\n - detectWindowsMedia() = " + detectWindowsMedia();
	debugMessage += "\n - detectActiveX() = " + detectActiveX();
	debugMessage += "\n - detectQuickTime() = " + detectQuickTime();
	debugMessage += "\n - detectActiveXPlugin() = " + detectActiveXPlugin();
	debugMessage += "\n - detectFirefoxWMPPlugin() = " + detectFirefoxWMPPlugin();
	return debugMessage;
}

