// Version minimale de Flash supportée
var flashRequired = new Array(6,0,0,0); 

// Debug = true; Affiche des infos utiles en fonte courrier pour tester
var Debug = false; 
var sDebug = "sDebug: "; 

// Affichage = true; Affiche l'info à l'usager
var Affichage = false; 
var sMsg = "Vous utilisez...";




/* *** GLOBAL VARIABLES *** */
var fDetectFlash = false;
var detectableWithVB = false;
var pluginFound = false;
var fFlashRequirement = false;
var flashVersion = 0;
flashVersion_DONTKNOW = -1;
var sFlashRequired = flashRequired.join();
var iFlashRequired = flashRequired[0] + "." + flashRequired[1] + flashRequired[2] + flashRequired[3];
sDebug = sDebug + "<li>sFlashRequired : " + sFlashRequired;

/* *** LIB FUNCTIONS *** */
function detectFlash(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

/* --- FLASH VERSION DETECT --- */
// Flash Version Detector  v1.1.5
// http://www.dithered.com/javascript/flash_detect/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)
// with VBScript code from Alastair Hamilton

function getFlashVersion() {
	var agent = navigator.userAgent.toLowerCase();
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') {
			if (flashPlugin.description.indexOf('7.') != -1)
			{ 
			  flashVersion = 7;
			}
			else if (flashPlugin.description.indexOf('6.') != -1)
			{ 
			  flashVersion = 6;
			  var isVersion = navigator.plugins["Shockwave Flash 6.0"] ? " 6.0" : "";
			  var flashFloatVersion = navigator.plugins["Shockwave Flash" + isVersion].description;
			}
			else if (flashPlugin.description.indexOf('5.') != -1)
			{
			  flashVersion = 5;
			  var isVersion = navigator.plugins["Shockwave Flash 5.0"] ? " 5.0" : "";
			  var flashFloatVersion = navigator.plugins["Shockwave Flash" + isVersion].description;
			}
			else if (flashPlugin.description.indexOf('4.') != -1)
			{
			  flashVersion = 4;
			  var isVersion = navigator.plugins["Shockwave Flash 4.0"] ? " 4.0" : "";
			  var flashFloatVersion = navigator.plugins["Shockwave Flash" + isVersion].description;
			}
			else if (flashPlugin.description.indexOf('3.') != -1)
			{
			  flashVersion = 3;
			  var isVersion = navigator.plugins["Shockwave Flash 3.0"] ? " 3.0" : "";
			  var flashFloatVersion = navigator.plugins["Shockwave Flash" + isVersion].description;
			}
		}
		var iFloatVersion = flashPlugin.description.indexOf('.') - 1;
		flashVersion = flashPlugin.description.substr(iFloatVersion);
	}
	// IE+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && agent.indexOf("win")!=-1) {
	   document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('dim obFlash \n');
		document.write('set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.7") \n');
		document.write('if IsObject(obFlash) then \n');
		document.write('flashVersion = 7 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.6") end if \n');
		document.write('if flashVersion < 7 and IsObject(obFlash) then \n');
		document.write('flashVersion = 6 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.5") end if \n');
		document.write('if flashVersion < 6 and IsObject(obFlash) then \n');
		document.write('flashVersion = 5 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.4") end if \n');
		document.write('if flashVersion < 5 and IsObject(obFlash) then \n');
		document.write('flashVersion = 4 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.3") end if \n');
		document.write('if flashVersion < 4 and IsObject(obFlash) then \n');
		document.write('flashVersion = 3 \n');
		document.write('end if');
		document.write('</scr' + 'ipt\> \n');
	}
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;
	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;
	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
	}
	return flashVersion;
}

/* --- FLASH RELEASE DETECTION JUST FOR IE (VERY EVIL) --- */
// In IE since only the one digit major release can be obtained, there is no choice but to try via cab versioning
function getFlashUsingIE(iMajor,iMinor)
{
  document.writeln('<di' + 'v style=display:none;>');
  document.writeln(' <scr' + 'ipt language=VBscript>');
  document.writeln(' on error resume next');
  document.writeln(' Sub detectFlash_FSCommand(ByVal command, ByVal args)');
  document.writeln(' call detectFlash_DoFSCommand(command, args,' + iMajor + ')');
  document.writeln(' end sub');
  document.writeln(' </scr' + 'ipt>');
  document.writeln(' <OBJECT CLASSID=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
  document.writeln(' CODEBASE=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + iMajor + ',0,' + iMinor + ',0');
  document.writeln(' WIDTH=1 HEIGHT=1 ID=detectFlash> <PARAM NAME=src VALUE=detectFlash.swf>');
  document.writeln(' <EMBED SRC=detectFlash.swf');
  document.writeln(' PLUGINSPAGE=http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash');
  document.writeln(' WIDTH=1 HEIGHT=1 NAME=detectFlash.swf swLiveConnect=true TYPE=application/x-shockwave-flash PLUGINSPAGE=http://www.macromedia.com/go/getflashplayer>');
  document.writeln(' </EMBED></OBJECT>');
  document.writeln('</di' + 'v>');
}	

function detectFlash_DoFSCommand(command, args, iMajor) 
{
  var detectFlashObj = (navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1) ? detectFlash : document.detectFlash;

  if (eval(args) >= flashRequired[2])
  {
	UpdateMyCookie('ECPTELE','fl:1');
  }
  else
  {
    // sArgs = args;
	sArgs = sArgs + "Plugiciel Flash requis:\n" + sFlashRequired;
	sArgs = sArgs + "\n";
	sArgs = sArgs + "Plugiciel Flash détecté:\n" + iMajor + ",0," + args + ",0";
	if (Debug == true) { alert(sArgs) };
  }
  
}

function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if( redirectURL && ((pluginFound && redirectIfFound) || 
	(!pluginFound && !redirectIfFound)) ) {
	// go away
	if (Debug == false) { 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;
    }
}

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

function AfficheMsg(Msg)
{  
  document.write(Msg);
}

/* *** LOAD : MSIE OBJECTS *** */
// VBScript Object Creation writelines for plugin detection
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>');
}

/* --- PLUGIN DETECT --- */

sMsg = sMsg + "<br>Shockwave Flash ";
if (detectFlash())
{
  UpdateMyCookie('ECPTELE','fl:1');
  var flashVersion = getFlashVersion();
  if (flashVersion)
  {
    if (flashVersion.length > 1) 
    {
	  // Extract the release number as a number for further testing
      sVer1 = flashVersion.substr(0,3);
      sVer2 = flashVersion.substr(flashVersion.indexOf('r') + 1);
      iFlashDetected = sVer1 + sVer2;
	  
	  // Extract the release number as a string for the display
  	  sFlashDetected1 = flashVersion.substr(0,1);
   	  sFlashDetected2 = flashVersion.substr(2,1);
  	  sFlashDetected3 = sVer2
   	  sFlashDetected4 = "0";
	  sFlashDetected = sFlashDetected1 + "," + sFlashDetected2 + "," + sFlashDetected3 + "," + sFlashDetected4;
	  
	  sMsg = sMsg + sFlashDetected;
    }
    else // We must be in IE since only the one digit major release could be obtained
    {
      iFlashDetected = flashVersion;
	  // No choice but to redirect via cab versioning
	  if (iFlashDetected < Math.abs(iFlashRequired))
	  {
  	    getFlashUsingIE(flashRequired[0],flashRequired[2]);
        sMsg = sMsg + iFlashDetected + " ";
	  }
	  else
	  {
        sMsg = sMsg + iFlashDetected + " version périmée!";
	  }
	  var fFlash = true;
    }
  }
}
else
{
  sMsg = sMsg + "... non détecté!";
  UpdateMyCookie('ECPTELE','fl:0');
}

if (Debug == true) { AfficheMsg("<hr><tt>" + sDebug + "</tt><hr>")}
if (Affichage == true) { AfficheMsg(sMsg)}