var detectableWithVB = false;
var pluginFound = false;

function getBrowserStatus(browser) {
	if ( navigator.appName.toLowerCase().indexOf('microsoft') > -1 ) ie = true;
	else ie = false;		
		
	if (browser=="ie" && ie) return true;
	else if (browser=="netscape" && !ie) return true;
	else return false;	
}

function isMac() {
	if ( navigator.platform.indexOf ( "Mac" ) != -1 ) 
		return true;
	else
		return false;
}

function detectReal(redirectURL, redirectIfFound) {
	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)'));
    }   
	return pluginFound;
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
	pluginFound = detectPlugin('Windows Media Player');
	if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
	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++ ) {
			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) ) {
					numFound++;
				}   
			}
			if(numFound == daPlugins.length) {
				pluginFound = true;			
				break;
			}
		}
	}

	return pluginFound;
}


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('<\/script>');
}

function getFastCastType(PlayerType) {
	if ( PlayerType == 2 ) {
		return "Windows Media";
	} else if ( PlayerType == 3 ) {
		return "Both";
	} else if ( PlayerType == 1 ) {
		return "Real";
	}
}

var ie = getBrowserStatus("ie");
var netscape = getBrowserStatus("netscape");
var wma = detectWindowsMedia();
var real = detectReal();

function getFinalURL(PlayerType, FCU, RU) {
	var fcType = getFastCastType(PlayerType);	
	if ( isMac() ) {
		return null;		
	} else if ( ie && fcType=="Windows Media" ) {
		return FCU;
	} else if ( ie && fcType=="Both" ) {
		if ( wma ) return FCU;
		else if ( real ) return RU;
		else return null;		
	} else if ( netscape && fcType=="WindowsMedia" ) {
		return null;
	} else if ( netscape && fcType=="Both" ) {
		return RU;
	} else if ( fcType=="Real" ) {
		return RU;
	}	
}

function showFastcast(PlayerType,FCU,RU,WindowHeight,NewWindow) {
	var fcType = getFastCastType(PlayerType);	
	var url;	
	if ( ie && fcType=="Windows Media" ) {
		if ( wma ) url = FCU;
		else url = "/gpimgs/fastcast/shared/wmp.htm";		
	} else if ( ie && fcType=="Both" ) {
		if ( wma ) url = FCU;
		else if ( real ) url = RU;
		else url = "/gpimgs/fastcast/shared/wmp.htm";		
	} else if ( netscape && fcType=="Windows Media" ) {
		url = "/gpimgs/fastcast/shared/msn.htm";
	} else if ( netscape && fcType=="Both" ) {
		if ( real ) url = RU;
		else url = "/gpimgs/fastcast/shared/real.htm";
	} else if ( fcType=="Real" ) {
		url = RU;
	} else if ( isMac() ) {
		url = "/gpimgs/fastcast/shared/mac.htm";
	}			

	if ( url != FCU && url != RU) WindowHeight = 400;		
	
	if ( NewWindow )
		window.open(url,'wndFastcast','width=700,height=' + WindowHeight + ',menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,location=no');		
	else
		location = url;
}

// Used when we want to open the FC in a new window
function getFastcast(PlayerType, FCU, RU, WindowHeight) {
	showFastcast(PlayerType,FCU,RU,WindowHeight,true);	
}
// Used when we want to open the FC in the same window
function getFastcastSameWindow(PlayerType,FCU,RU,WindowHeight) {
	showFastcast(PlayerType,FCU,RU,WindowHeight,false);	
}