
var needAxDetection = false;
var pluginFound = false;

//quickly testing the browser to know if we must detect by using activeX or not
//to need ActiveX detection, we need to be on windows and use Internet Explorer (take care of Opera
// which could be seen as Internet Explorer
if(navigator.userAgent.toLowerCase().indexOf('msie') != -1 && navigator.userAgent.toLowerCase().indexOf('opera')
	&& navigator.platform.toLowerCase().indexOf('win' != -1)) {
	needAxDetection = true;
}

	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 goURL(daURL) {
	    // if the browser can do it, use replace to preserve back button
	    if(javascriptVersion1_1) {
			//alert( "1. Redirect to" + daURL );
	    	window.location.replace(daURL);
	    } 
		else {
			//alert( "2. Redirect to" + daURL );
	    	window.location = daURL;
	    }
	    return;
	}
	
	//the real proptotype of the function is function(args[] string)
	function checkForPlugin() {
		//allow for multiple checks in a single pass
		//if one of the plugin name is detected, the function returns true
		var args = this.checkForPlugin.arguments;
		var found = false;
		if (navigator.plugins && navigator.plugins.length > 0) {
			var pluginsArrLength = navigator.plugins.length;
			var i = 0;
			while (i < pluginsArrLength && !found) {
				j = 0;
				while (j < args.length && !found) {
					if ((navigator.plugins[i].name.indexOf(args[j]) != -1) ||
						(navigator.plugins[i].description.indexOf(args[j]) != -1)) {
						return navigator.plugins[i];
					}
					j++;
				}
				i++;
			}
		}
		return;
	}

	function detectWindowsMedia(redirectURL, redirectIfFound){
		pluginFound = false;
		if(needAxDetection){
			try{
				axo = new ActiveXObject('wmplayer.ocx');
			}
			catch(e){}
			
			//the new wmplayer plugin failed to load, try the old one
			if(!axo){
				try{
					axo = new ActiveXObject('MediaPlayer.MediaPlayer.1');
				}
				catch(e){}
			}
			
			if(axo){
				pluginFound = true;
				delete axo;
			}
		}
		else{
			//looking for a plugin in plugins' browser array that supports wmp content
			mimes = navigator.mimeTypes;
			for(i = 0; i < mimes.length; i++){
				if(mimes[i].type.indexOf("x-ms-wmp") != -1) {
					//found the best plugin possible, break the loop
					mime = mimes[i].type;
					nsPlugin = mimes[i].enabledPlugin;
					break;
				}
				else if(mimes[i].type.indexOf("x-ms-wmv") != -1) {
					//found a plugin, but maybe not the best one, so continue the loop
					mime = mimes[i].type;
					nsPlugin = mimes[i].enabledPlugin;
				}
			}
			
			nsPlugin = this.checkForPlugin('Windows Media Player', 'Flip4Mac'); //add 'VLC' ???
			if(nsPlugin){
				pluginFound = true;
			}
		}
		
		return redirectCheck(pluginFound, redirectURL, redirectIfFound);
	}
	
	function detectFlash(redirectURL, redirectIfFound){
		pluginFound = false;
		if(needAxDetection){
			try{
				axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.1');
			}
			catch(e){}
			
			if(axo){
				pluginFound = true;
				delete axo;
			}
		}
		else{
			//looking for the plugin in plugins' browser array
			nsPlugin = this.checkForPlugin('Flash'); //add 'VLC' ???
			if(nsPlugin){
				pluginFound = true;
			}
		}
		
		return redirectCheck(pluginFound, redirectURL, redirectIfFound);
	}
	
	function detectReal(redirectURL, redirectIfFound){
		pluginFound = false;
		if(needAxDetection){
			var definedControls = [  
				'rmocx.RealPlayer G2 Control',  
				'rmocx.RealPlayer G2 Control.1',  
				'RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)',  
				'RealVideo.RealVideo(tm) ActiveX Control (32-bit)',  
				'RealPlayer'  
			];
			for(i = 0; i < definedControls.length; i++){
				try{
					axo = new ActiveXObject(definedControls[i]);
				}
				catch(e){ continue; }
				
				if(axo){
					break;
				}
			}
			
			if(axo){
				pluginFound = true;
				delete axo;
			}
		}
		else{
			//locking for the version plugin in plugins' browser array
			nsPlugin = this.checkForPlugin('RealPlayer Version Plugin');
			if(nsPlugin){
				pluginFound = true;
			}
			else{
			//looking for the regular plugin and trying to get version information
				nsPlugin = this.checkForPlugin('RealPlayer');
				if(nsPlugin){
					pluginFound = true;
				}
			}
		}
		
		return redirectCheck(pluginFound, redirectURL, redirectIfFound);
	}
	
	function detectSilverlight(redirectURL, redirectIfFound){
		pluginFound = false;
		if(needAxDetection){
			try{
				axo = new ActiveXObject('AgControl.AgControl');	
			}
			catch(e){}
			if(axo){
				pluginFound = true;
				delete axo;
			}
		}
		else{
			//moonlight can be installed twice (version 1 and version 2 preview, might change in the final one)
			//so we need to look for mimeTypes instead of looking for the plugin name.
			mimes = navigator.mimeTypes;
			for(i = 0; i < mimes.length; i++){
				if(mimes[i].type.indexOf("x-silverlight-2") != -1) {
					//found the best plugin possible, break the loop
					mime = mimes[i].type;
					nsPlugin = mimes[i].enabledPlugin;
					break;
				}
				else if(mimes[i].type.indexOf("x-silverlight") != -1) {
					//found a plugin, but maybe not the best one, so continue the loop
					mime = mimes[i].type;
					nsPlugin = mimes[i].enabledPlugin;
				}
			}
			
			if(nsPlugin){
				pluginFound = true;
			}
		}
		
		return redirectCheck(pluginFound, redirectURL, redirectIfFound);
	}
	
	function detectQuickTime(redirectURL, redirectIfFound){
		pluginFound = false;
		if(needAxDetection){
			try {
				axo = new window.ActiveXObject("QuickTime.QuickTime");
			}
			catch (ex) { }
			
			if(axo){
				pluginFound = true;
				delete axo;
			}
		}
		else{
			//looking for the plugin in plugins' browser array
			nsPlugin = this.checkForPlugin('QuickTime'); //MoonLight???
			if(nsPlugin){
				pluginFound = true;
			}
		}
		
		return redirectCheck(pluginFound, redirectURL, redirectIfFound);
	}