// initialize global variables
var detectableWithVB = false;
var pluginFound = false;
var flash = false;
var quickTime = false;
var wmp = false;

function canDetectPlugins() {
	if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
	} else {
	return false;
	}
}

function detectFlash() {
	pluginFound = detectPlugin('Shockwave','Flash'); 
	// if not found, try to detect with VisualBasic
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
	}
	// FLASH VERSION CHECKING
	flash = new Object();
	flash.installed=false;
	flash.version='0.0';
	
	if (navigator.plugins && navigator.plugins.length){
		for (x=0; x<navigator.plugins.length; x++){
			if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1){
				flash.version = navigator.plugins[x].description.split('Shockwave Flash ')[1];
				flash.installed = true;
				break;
			}
		}
	}else if (window.ActiveXObject){
		for (x=2; x<10; x++){
			try{
				oFlash=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+x+"');");
				if (oFlash){
					flash.installed=true;
					flash.version=x+'.0';
				}
			}catch(err){
			}
		}
	}
	flash.intVersion=parseInt(flash.version);
	flash.ver2=(flash.installed && flash.intVersion >= 2) ? true:false;
	flash.ver3=(flash.installed && flash.intVersion >= 3) ? true:false;
	flash.ver4=(flash.installed && flash.intVersion >= 4) ? true:false;
	flash.ver5=(flash.installed && flash.intVersion >= 5) ? true:false;
	flash.ver6=(flash.installed && flash.intVersion >= 6) ? true:false;
	flash.ver7=(flash.installed && flash.intVersion >= 7) ? true:false;
	flash.ver8=(flash.installed && flash.intVersion >= 8) ? true:false;
	flash.ver9=(flash.installed && flash.intVersion >= 9) ? true:false;
	return pluginFound;
}

function detectDirector() { 
	pluginFound = detectPlugin('Shockwave','Director'); 
	// if not found, try to detect with VisualBasic
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
	}
	return pluginFound;
}

function detectQuickTime() {
	pluginFound = detectPlugin('QuickTime');
	// if not found, try to detect with VisualBasic
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectQuickTimeActiveXControl();
	}
	// QUICKTIME VERSION CHECKING
	quickTime = new Object();
	quickTime.installed=false;
	quickTime.version='0.0';
	
	// Quicktime Detection  v1.0
	// documentation: http://www.dithered.com/javascript/quicktime_detect/index.html
	// license: http://creativecommons.org/licenses/by/1.0/
	// code by Chris Nott (chris[at]dithered[dot]com)
	// Modified by William Jamieson
	agent = navigator.userAgent.toLowerCase(); 
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Quicktime plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
	  for (i=0; i < navigator.plugins.length; i++ ) {
		 plugin = navigator.plugins[i];
		 if (plugin.name.indexOf("QuickTime") > -1) {
			quickTime.version = parseFloat(plugin.name.substring(18));
			quickTime.installed = true;
		 }
	  }
	} else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
		quickTimeVersion = '0.0';
		quickTimeInstalled = false;
		
/*		document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('dim obQuicktime \n');
		document.write('set obQuicktime = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1") \n');
		document.write('if IsObject(obQuicktime) then \n');
		document.write('   if obQuicktime.IsQuickTimeAvailable(0) then \n');
		document.write('      quickTimeVersion = CInt(Hex(obQuicktime.QuickTimeVersion) / 1000000) \n');
		document.write('      quickTimeInstalled = True \n');
		document.write('   end if \n');
		document.write('end if \n');
		document.write('</scr' + 'ipt\> \n');//*/
		
		//Create a 'script' element	
		var scrptE = document.createElement("script");
		// Set it's 'language' attribute
		scrptE.setAttribute("language", "VBScript");
		// Set it's 'src' attribute
		scrptE.setAttribute("src", "/js/createActiveXControl.js");
		// Now add this new element to the head tag
		document.getElementsByTagName("head")[0].appendChild(scrptE);

		quickTime.version = quickTimeVersion;
		quickTime.installed = quickTimeInstalled;
	} else {
		// Can't detect in all other cases
	}
	quickTime.intVersion=parseInt(quickTime.version);
	return pluginFound;
}

function detectReal() {
	pluginFound = detectPlugin('RealPlayer');
	// if not found, try to detect with VisualBasic
	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)'));
	}	
	return pluginFound;
}

function detectWindowsMedia() {
	pluginFound = detectPlugin('Windows Media');
	// if not found, try to detect with VisualBasic
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
	}
	// WMP VERSION CHECKING
	wmp = new Object();
	wmp.installed=pluginFound;
	wmp.version='0.0';
	
	if (navigator.appName != "Netscape"){   
		try{ 
			WMP7 = new ActiveXObject('WMPlayer.OCX');
			wmp.version = "7.0 or higher";
		}catch(err){
			if(pluginFound){
				wmp.version = "6.4 or lower";
			}
		}finally{ 
		}
	}
	if(pluginFound && parseInt(wmp.version) < 7){
		wmp.version = "6.4 or lower";
	}
	//alert(wmp.version);
	// Windows Media Player 7 Code
	wmp.intVersion=parseInt(wmp.version);
	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


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