// Largement inspiré de : 
// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

function goURL(daURL) {
	window.location.replace(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;
    }
}

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

function detectQuickTime(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectReal(redirectURL, redirectIfFound) {
    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 redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectVLC(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('VLC');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('VLC.VLC.1');
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

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>');
}
function includeJS(script_file)
	{
	document.write('<script type="text/javascript" src="' + script_file + '"></' + 'script>'); 
	}

// Start main
//
media_player = "";
// Test pour plugins spécifiques de Mozilla
for(var i=0; i<navigator.plugins.length; i++)
	{
	if (navigator.plugins[i].name.substr(0,5).toUpperCase() == 'TOTEM')
		{
		media_player = "";
		// media_player = "totem";
		// Ne morche pô..
		//break;
		}
	if (navigator.plugins[i].name.substr(0,3).toUpperCase() == 'VLC')
		{
		media_player = "vlc";
		break;
		// Notre chouchou...
		}
	}
// Test pour WINDOWS (Active X)
if(media_player == "" && canDetectPlugins())
	{
	if (detectQuickTime())
		{
		media_player = "qt";
		}
	else
		{
		if (detectWindowsMedia())
			{
			// P'têt ça marche sous Windows ?
			// (en tout cas sous GNU/Linux oui)
			media_player = "win";
			}
		else
			{
			if (detectReal())
				{
				// media_player = "real";
				// Reste à faire
				media_player = "";
				}
			else
				{
				if (detectFlash())
					{
					// media_player = "flash";
					// marche pas encore
					media_player = "";
					}
				}
			}
		}
	}

// Default stream 
var stream_url = "http://modulix.org:8000/libre.ogg";

if (media_player != "")
	{
	$(document).ready(function() {
		$("#mpeg2").attr("src", media_player + "_mpeg2.png");
		$("#mpeg4").attr("src", media_player + "_mpeg4.png");
		$("#ogg").attr("src", media_player + "_ogg.png");
		$("#current_url").html(stream_url);
	        $("#tele_libre").load("player_" + media_player + ".html");
		$("#current_url").html("URL à utiliser directement dans votre lecteur multimédia : " + stream_url);
		// Changement de flux
		$(".stream_url").click(function(event) {
			event.preventDefault();
			stream_url = this.href;
			$("#tele_libre").load("player_" + media_player + ".html");
			$("#current_url").html("URL à utiliser directement dans votre lecteur multimédia : " + stream_url);
			});
		// Reste à faire (ou pas)
		// Construire la liste d'après ce que l'utilisateur possède vraiment...
		// $("#availables_plugins").html('<a class="available_plugin"...');
		// Changement de plugin
		$(".available_plugin").click(function(event) {
			event.preventDefault();
			media_player = this.pathname.replace("/","");
			// Maj icones des flux
			$("#mpeg2").attr("src", media_player + "_mpeg2.png");
			$("#mpeg4").attr("src", media_player + "_mpeg4.png");
			$("#ogg").attr("src", media_player + "_ogg.png");
			// Maj vidéo
			//stream_url = "http://modulix.org:8000/libre.ogg";
			$("#tele_libre").load("player_" + media_player + ".html");
			// Maj Affichage URL
			$("#current_url").html("URL à utiliser directement dans votre lecteur multimédia : " + stream_url);
			});
		});
	}
else
	{
	document.writeln('<h2>Pas de plugin pour lire la vidéo !</h2>');
	document.writeln('<p>Malheureusement, votre navigateur ne possède aucun plugin permettant de lire les flux vidéos de notre</p><h3 style="text-align:center;">Télévision Libre</h3>');
	// mini-tele
	document.writeln('<p>Vous pouvez <a href="/quicktime/download/">télécharger <a href="http://www.videolan.org/vlc">VLC</a> et installer leur plugin. Attention, pour cela, vous devez sélectionner l\'installation complète (Full) après avoir validé la Licence Libre au moment de l\'installation.</p>');
	document.writeln('<p>&nbsp;</p>');
	document.writeln('Vous pouvez aussi <a href="/quicktime/download/">télécharger QuickTime</a> qui propose aussi un plugin capable de lire de la vidéo en streaming.</p><p><a href="http://www.apple.com/quicktime/download/"><img src="http://images.apple.com/global/elements/quicktime/getquicktime.gif" alt="Télécharger QuickTime" border="0"></a></p>');
	}


