/******************************************************************/
/* © DMC 01 Consulting & Development GmbH - gerald.urbas@dmc01.at */
/******************************************************************/
/* Detection code based on example	                         */
/******************************************************************/
/* getPlugins() recieve an object {pluginname:false,p2:false}     */
/* returns Object with true or false                              */
/* while detecting fills array detected_plugin_versions           */
/* adds basic debugging functiosn (create clear and data div      */
/******************************************************************/

function dbg(str) { // in case of debugging
	if (pdebug==false) {
		$j("#videodebugclear").hide();
		$j("#videodebug").hide();
	} else {
		if ($j("#videodebugclear").html() == null) {
			initDebug();
		}
		$j("#videodebugclear").show();
		$j("#videodebug").show();
		$j("#videodebug").append(str+"<br />");
	}
};

function initDebug() {
	$j('#infopane').after('<p id="videodebugclear">Debugausgabe l&ouml;schen</p><p id="videodebug" style=""></p>');
	var cleareobj = $j("#videodebugclear"); // generate Clear button for info and help on bottom
	cleareobj.bind("click", function(){ // bind click
		$j("#videodebug").empty();
	});	
}

function printJsError(e) {
	for (i in e) {
		this.dbg("JS Error: "+i+"="+e[i]);
	}
}
/*function showSupportedPlayers(players) {
	try {
		if (players.length > 0)
			document.getElementById("format_container").style.display = "inline";
		for (var i = 0; i < players.length; i++)
			document.getElementById("player"+players[i]).style.display = "";
	} catch (e) {
	
	}
}*/

var detectableWithVB = false;
var pluginFound = false;
var cancelFunction = false;

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
		return true;
    } else {
		return false;
    }
}

function detectFlash(redirectURL, redirectIfFound) {
	pluginFound = detectPlugin('Shockwave','Flash');
	detected_plugin_versions["flashmedia"] = detectPluginVersion('Shockwave','Flash');
	for (var i=1; i<=15; i++){
		if(!pluginFound && detectableWithVB) {
			pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.'+i);
			detected_plugin_versions["flashmedia"] = detectActiveXControlVersion;
		}
	}
	if (!pluginFound && $.browser.msie && parseInt(jQuery.browser.version)>=7) {
		pluginFound='Wahr';
		detected_plugin_versions["flashmedia"] = 15;
	}
	if (pluginFound=='false' || pluginFound =='False') {
		return false;
	}
	return pluginFound;
}

function detectQuickTime(redirectURL, redirectIfFound) {
	pluginFound = detectPlugin('QuickTime');
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectQuickTimeActiveXControl();
	}
	if (pluginFound=='false' || pluginFound =='False') {
		return false;
	}
	return pluginFound;
}

function detectReal(redirectURL, redirectIfFound) {
	pluginFound = hasRP;
	if (hasRP && rpversion && rpversion!='') {
		detected_plugin_versions["realmedia"] = parseInt(rpversion);
	}
	if(!pluginFound) {
		pluginFound = detectPlugin('RealPlayer');
	}
	if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
	}
	if (pluginFound=='false' || pluginFound =='False') {
		return false;
	}
	return pluginFound;
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
	pluginFound = detectPlugin('Windows Media');
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
	}
	if (pluginFound=='false' || pluginFound =='False') {
		return false;
	}
	return pluginFound;
}

function detectPlugin() {	
	var daPlugins = detectPlugin.arguments;
	var pluginFound = false;
	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) ) {
					// 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;
}

function detectPluginVersion() {	
	var daPlugins = detectPluginVersion.arguments;
	var pluginFound = false;
	var version_info = false;
	
	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++) {
				rex_plugin=/\d{1,2}/;
				version_info=rex_plugin.exec(navigator.plugins[pluginsArrayCounter].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 version_info;
}

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('detectActiveXControlVersion = 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('     VersionInfo = CreateObject(activeXControlName).GetVariable("$version")');
	document.writeln('     Set regEx = New RegExp');
	document.writeln('     regEx.Pattern = "[0-9]{1,2}"');
	document.writeln('     Set Matches = regEx.Execute(VersionInfo)');
	document.writeln('     For Each Match in Matches');
	document.writeln('    	detectActiveXControlVersion=Match');
	document.writeln('     Next');
	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>');
}

var detected_plugins = new Array();
var detected_plugin_versions = new Array();

function getPlugins() {
	detected_plugins = arguments[0];
	detected_plugins.length = 0;
	plug = detected_plugins["realmedia"];
	if (typeof plug!="undefined" && plug==false && detectReal("", false)) {
		detected_plugins.realmedia = true;
		detected_plugins.length++;
	}
	plug = detected_plugins["microsoftmedia"];
	if (typeof plug!="undefined" && plug==false && detectWindowsMedia("", false)) {
		detected_plugins.microsoftmedia = true;
		detected_plugins.length++;
	}
	plug = detected_plugins["quicktime"];
	if (typeof plug!="undefined" && plug==false && detectQuickTime("", false)) {
		detected_plugins.quicktime = true;
		detected_plugins.length++;
	}

	plug = detected_plugins["flashmedia"];
	dbg("Detection: flash 1 "+plug);
	if (typeof plug!="undefined" && plug==false && detectFlash("", false)) {
		detected_plugins.flashmedia = true;
		detected_plugins.length++;
		dbg("Detection: flash 2 "+plug);
	}
	dbg("Detection: flash 1 "+detected_plugins);
	
	return detected_plugins;
}


