var msie_windows=0;

if( (navigator.userAgent.indexOf('MSIE') != -1 ) && ( navigator.userAgent.indexOf('Win') != -1 ) ){

	var RealstartTagFix='</';

	msie_windows=1;
	document.writeln('<script language="VBscript">');
	document.writeln('Dim Realdetect_through_vb');
	document.writeln('Realdetect_through_vb = 0');
	document.writeln('If ScriptEngineMajorVersion >= 2 then');
	document.writeln('  Realdetect_through_vb = 1');
	document.writeln('End If');
	document.writeln('Function RealactiveXDetect(activeXname)');
	document.writeln('  on error resume next');
	document.writeln('  If ScriptEngineMajorVersion >= 2 then');
	document.writeln('     RealactiveXDetect = False');
	document.writeln('     RealactiveXDetect = IsObject(CreateObject(activeXname))');
	document.writeln('     If (err) then');
	document.writeln('        RealactiveXDetect = False');
	document.writeln('     End If');
	document.writeln('   Else');
	document.writeln('     RealactiveXDetect = False');
	document.writeln('   End If');
	document.writeln('End Function');
	document.writeln(RealstartTagFix+'script>');
	}

function RealpluginDetect(plugindescription,pluginxtension,pluginmime,activeXname){

	var i,plugin_undetectable=0,detected=0, daPlugin=new Object();

	if(msie_windows&&Realdetect_through_vb){
		plugin_undetectable=0;
		}
	else{
		plugin_undetectable=1;
		}

	if(navigator.plugins){
		numPlugins=navigator.plugins.length;
		
		if(numPlugins>1){

			if(navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin  ){

				if(  (navigator.appName=='Netscape') && (navigator.appVersion.indexOf('4.0') != -1) ){
				
					for(i in navigator.plugins){


						if( (navigator.plugins[i].description.indexOf(plugindescription) != -1 ) || ( i.indexOf(plugindescription) != -1 ) ){
							detected=1;
							break;
							}
						}
					}
				else{
					for(i=0; i<numPlugins; i++){


						daPlugin = navigator.plugins[i];

						if( ( daPlugin.description.indexOf(plugindescription) != -1) || ( daPlugin.name.indexOf(plugindescription) != -1 )  ){
							detected=1;
							break;
							}
						}
					}
				if (navigator.mimeTypes[pluginmime]==null){
					detected=0;
					}
			}
			return detected;
		}
		else if( (msie_windows ==1 )  &&  !plugin_undetectable ){
			return RealactiveXDetect(activeXname);
			}
		else{
			return 0;
			}
		}
	else{
		return 0;
		}
	}

function RealDetect(whichPlugin){

	var isItThere=0;

	if( whichPlugin=='realplayer' ){
		isItThere = ( RealpluginDetect('RealPlayer','rpm','audio/x-pn-realaudio-plugin','RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ) || ( RealpluginDetect('RealPlayer','rpm','audio/x-pn-realaudio-plugin','rmocx.RealPlayer G2 Control') ) || ( RealpluginDetect('RealPlayer','rpm','audio/x-pn-realaudio-plugin','RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ) ||  ( RealpluginDetect('RealPlayer','rpm','audio/x-pn-realaudio-plugin','RealVideo.RealVideo(tm) ActiveX Control (32-bit)') );
		if(isItThere == true){
			return true;
			}
		}
	else{
		return false;
		}
	}	


function setSlides(slideURL){
	var insertEl = (typeof 'slideshow' == "string" ) ? document.getElementById('slideshow') : 'slideshow';

	insertEl.innerHTML = "";
	insertEl.innerHTML = slideURL; // destroy current slideshow

	var fo = new SWFObject("/flash/trainingcamp/slideshow.swf", "slideshowPlayer", "355", "290", "8", "#ffffff", true);
	fo.addParam("quality", "high"); fo.addParam("allowScriptAccess", "always"); fo.addParam("swliveconnect", "true");
	fo.addVariable("slideCategory", slideURL);
	fo.write("slideshow");    

	}

function simpleProxy(flashPlayerName, listenerID){
	var fo = new SWFObject("/flash/mediaplayer/simpleProxy.swf", flashPlayerName + "_proxy", "0", "0", "8", "#ffffff", true);
	fo.addParam("quality", "high"); fo.addParam("allowScriptAccess", "always"); fo.addParam("swliveconnect", "true");
	fo.addVariable("lcID", listenerID);
	fo.write("proxyContainer");    
	}

function realUIMsg(){ // same as realUIMSG(functionName, functionArguments..){
	if(realReady){
		var functionNameToCall = arguments[0];
		var serializedArguments = arguments[1];

		if (realUIMsg.arguments.length > 2){
			for (i=2;i < realUIMsg.arguments.length;i++){
				serializedArguments += '|' + realUIMsg.arguments[i];
				}
			}

		callASFunction("flashUI_proxy", 'functionToCall', functionNameToCall + '|' + serializedArguments);
		}
	}

function callASFunction(proxyName, functionName, functionArguments){
	var proxyMovie=getFlashObject(proxyName);
	if (proxyMovie){
		var serializedArguments = arguments[2];
		for (i=3;i < callASFunction.arguments.length;i++){
			serializedArguments += '|' + callASFunction.arguments[i];
			}
		proxyMovie.SetVariable("/:" + functionName, serializedArguments);
		}
	}

function initRealPlayerWindow(isRealReady){
	if (isRealReady == false){
		hide("realWindow");
		hide("realUI");
		}
	else{
		show("realError");
		}
	}

function getFlashObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
		}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
    	if (document.embeds && document.embeds[movieName])
      		return document.embeds[movieName];
		}
	else{ // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName);
		}
	}

var slideshowReady = true;
var singleImageReady = true;
var flashReady = true;
var realReady = RealDetect("realplayer");
initRealPlayerWindow(realReady);

var flashProxy;

// start Real's Image Window Detection

// end Real's Image Window Detection

var bPlaying = false;
var oBufferInterval;
var oElapsedInterval;

function updateBufferTime(){
	if(realReady && document.NYJets.GetPlayState()){
		switch (document.NYJets.GetPlayState()) { 
		case 0 : // stopped
			if(bPlaying) playStream();// force the video to start playing if it's supposed to
			onBufferComplete();
			break;
		case 1 : // contacting server
			realUIMsg('broadcastMessage', 1);
			break; 
		case 2 : // buffering
			realUIMsg('broadcastMessage', 2);
			break; 
		case 5 : // seeking
			realUIMsg('broadcastMessage', 5);
			break; 
		case 6 : // loading
			realUIMsg('broadcastMessage', 6);
			break; 
		default : // any other messages - like 'play' etc.
			onBufferComplete();
			break; 
			} 
		}
	}
	
function onBufferComplete(){
	stopStreamInterval();
	show('realWindow');
	hide('flashVideo');
	realUIMsg('onBufferComplete');
	clearInterval(oBufferInterval);
	startStreamInterval();
	}
	
function startStream(){
	oBufferInterval = setInterval('updateBufferTime()', 200);
	document.NYJets.DoPlay();
	bPlaying = true;
	}	
	
function startStreamInterval(){
	oElapsedInterval = setInterval('updateTimeElapsed()', 200);
	}
	
function stopStreamInterval(){
	bPlaying = false;
	clearInterval(oElapsedInterval);
	clearInterval(oBufferInterval);
	}
	
function updateTimeElapsed(){

	if(realReady){

		switch (document.NYJets.GetPlayState()) { 
			case 1 : // contacting server
				realUIMsg('broadcastMessage', 1);
				break; 
			case 2 : // buffering
				realUIMsg('broadcastMessage', 2);
				break; 
			case 3 : // playing
				var iLength = document.NYJets.GetLength();
				var iPosition = document.NYJets.GetPosition();
				realUIMsg('onUpdateElapsedTime', iPosition + "|" + iLength );
				break; 
			case 5 : // seeking
				var iLength = document.NYJets.GetLength();
				var iPosition = document.NYJets.GetPosition();
				realUIMsg('onUpdateElapsedTime', iPosition, iLength);
				break; 
			case 6 : // loading
				realUIMsg('broadcastMessage', 6);
				break; 
			default : // paused etc.
				stopStreamInterval();
				break; 
			}
		}
	}
	
function getVideoLength(){
	var iLength = document.NYJets.GetLength();
	realUIMsg('setVideoLength', iLength);
	}
		
function setVolume(iNewValue){
	document.NYJets.SetVolume(Number(iNewValue));
	}

function setStreamPosition(iNewPos){
	document.NYJets.SetPosition(iNewPos);
	realUIMsg('playPause');
	}
	
function playStream(){
	if(document.NYJets.CanPlay()){
		startStream();
		}
	}
	
function pauseStream(){
	if(document.NYJets.CanPause()){
		document.NYJets.DoPause();
		stopStreamInterval();
		}	
	}

function stopStream(){
	document.NYJets.DoStop();
	stopStreamInterval();
	}

/* added for hide show items */

function initMediaPlayer(){

	var mediaTypeList = arguments[0];
	mediaPlayers = mediaTypeList.split('#');
	var initURL = arguments[1];
	var showRelatedLinks;
	var initMediaLabel = "video";

	if (initMediaPlayer.arguments.length > 2) showRelatedLinks = arguments[2]; 
	if (initMediaPlayer.arguments.length > 3) initRealURL = arguments[3]; 

	if (mediaTypeList != ''){

		var baseurl = location.href;

		var piece = baseurl.split('/');

		pieces = (piece[0] + '/' + piece[1] + '/' + piece[2] );

		thisBaseURL = baseurl.replace( pieces , '');
		if (showRelatedLinks == 'relatedMedia'){
	  		var fo = new SWFObject("/flash/trainingcamp/relatedMedia.swf", "moreLinks", "355", "68", "8", "#ffffff", true);
			  fo.addParam("quality", "high"); fo.addParam("loop", "false"); fo.addParam("allowScriptAccess", "sameDomain");
			  fo.addVariable("relatedCategory", thisBaseURL);
			  fo.write("relatedMedia");    
			}

					if(mediaPlayers[0] == "realaudio"){
						initMediaLabel = "audio";
						}

		for (var i=0; i < mediaPlayers.length; i++){

			switch (mediaPlayers[i]){

				case 'image':
				case 'slideshow':
					if(mediaPlayers[0]=="slideshow" || mediaPlayers[0]=="image"){
						setSlides(initURL);
						}
					break;

				case 'realaudio':
						//initMediaLabel = "audio";
				case 'real':

					if ( realReady ){
						flashProxy = new FlashProxy(); // used for instantiating function that receives js calls from flashUI;

						simpleProxy("flashUI", "nyjets");

						var sEmbedRealImageWindow = ''

							+'<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" '
							+'width="320" height="240" id="NYJets">'
	
							+'<param name="src" value="' + initRealURL + '" />'
							+'<param name="controls" value="Imagewindow" />'
							+'<param name="autostart" value="false" />'
							+'<param name="console" value="one" />'
							+'<param name="nologo" value="true">'
							+'<param name="nojava" value="false">'
	
							+'<embed src="' + initRealURL + '" id="NYJets" name="NYJets" width="355" height="240"'
							+'nojava="false" controls="ImageWindow" console="one" autostart="false"'
							+'backgroundcolor="black" nologo="true" />'
	
							+'</object>';

						var insertEl = (typeof 'realWindow' == "string" ) ? document.getElementById('realWindow') : 'realWindow';

						insertEl.innerHTML = "";
						insertEl.innerHTML = sEmbedRealImageWindow;

						var fo = new SWFObject("/flash/trainingcamp/flashUI.swf", "flashUI", "355", "50", "8", "#ffffff", true);
						fo.addParam("quality", "high");  fo.addParam("allowScriptAccess", "always"); fo.addParam("swliveconnect", "true");
						fo.addVariable("lcId", "nyjets");
						fo.addVariable("initMediaType", initMediaLabel);
						fo.write("realUI");    
						}
					else{
						realReady=false;
						}
					
					// now continue on to create flashPlayer instance

				case 'flash':
					flashReady = true;
					var fo = new SWFObject("/flash/trainingcamp/mediaPlayer.swf", "flashvid", "355", "290", "8", "#ffffff", true);

					fo.addParam("quality", "high"); fo.addParam("allowScriptAccess", "always"); fo.addParam("swliveconnect", "true");
					fo.addVariable("baseURL", thisBaseURL);
					fo.addVariable("initMediaType", initMediaLabel);
					fo.write("flashVideo");    
					break;
				}

			if (i == 0){
				switchPlayer(mediaPlayers[0], '' ,'firstTime'); // need to message function that this might be first time displaying Real + Flash Screencap
				}
			}
		}
	}

var oDoc=document;
var bDom = Boolean(oDoc.getElementById);
var bIE = Boolean(oDoc.all);
var bIE4 = Boolean(bIE && !bDom), bNS4=Boolean(oDoc.layers);

function hide(ElName){
	var oStyle;
	if (bDom){
		if(oDoc.getElementById(ElName)){
			oStyle=oDoc.getElementById(ElName).style;
			}
		}
	else if (bIE4){
		oStyle=oDoc.all[ElName].style;
		}
	else if (bNS4){
		oStyle=oDoc.layers[ElName];
		}
	else{
		return;
		}

	if(oStyle){
		oStyle.visibility = (!bNS4)?'hidden':'hide';
		}
	}

function show(ElName){
	var oStyle;
	if (bDom){
		oStyle=oDoc.getElementById(ElName).style;
		}
	else if (bIE4) oStyle=oDoc.all[ElName].style;
	else if (bNS4) oStyle=oDoc.layers[ElName];
	else return;
	oStyle.visibility = (!bNS4)?'visible':'show';
	}

function switchPlayer(){

	mediaPlayer = switchPlayer.arguments[0];
	
	if(mediaPlayer == 'real'){

		if (realReady){
			if (switchPlayer.arguments.length == 1 && realReady){
				show('realWindow');
				}

			show('realUI');
			show('realPlayer');
			}
		else show('realError');
		}


	if(mediaPlayer == "realaudio"){
		if (realReady){
			if (switchPlayer.arguments.length == 1 && realReady){
				show('realWindow');
				}
			show('realUI');
			show('realPlayer');
			}
		else{
			show('realError');
			}
		}

	if (mediaPlayer == 'flash' && flashReady){
		show('flashVideo');
		hide("realWindow");
		hide('realError');
		}
	else if (switchPlayer.arguments.length > 2 && realReady && (mediaPlayer == 'real' || mediaPlayer == 'realaudio')){
		show('flashVideo'); // need to show 
		}
	else{
		hide('flashVideo');
		}

	// realPlayer must be tested after flashVideo for initialization purposes

	if (   (mediaPlayer == 'slideshow' || mediaPlayer == 'image') && slideshowReady ){
		show("flashVideo");
		show('slideshow');
		}
	else hide('slideshow');

	}

function doChangeMedia(mediaURL ,mediaFormat, mediaType, mediaDescr){
	if(realReady){
		realUIMsg('resetUI', mediaType);
		hide("realWindow");
		hide("realUI");
		stopStream();
		}

	switch(mediaFormat){

		case "image":
		case "IMAGE":
		case "slideshow":

			setSlides(mediaURL);
			switchPlayer('slideshow');
			break;

		case "real":
		case "REAL":
			switchPlayer('real');
			if(realReady){
				document.NYJets.SetSource(mediaURL);
				startStream();
				}
			break;

		case "flash":
		case "FLV":
			switchPlayer('flash');
			break;
		}
	}

// Flash Proxy as Defined by ActionScript Standard Method Calls

function FlashProxy(){}

FlashProxy.prototype.call = function(){}

FlashProxy.callJS = function(){
    var functionToCall = eval(arguments[0]);
    var argArray = new Array();
    for (var i = 1; i < arguments.length; ++i){
        argArray.push(arguments[i]);
    	}
    functionToCall.apply(functionToCall, argArray);
	}
