// flag for whether the video is present in the current layout or not
var videoOnScreen = true;

// pop windows
var fileUploadWin;
var webShareWin, screenShareWin, downloadWin;

//////////////

var isNav5 = false;
var isNav4 = false;
var isNav  = false;
var isIE4  = false;
var isIE5  = false;
var isIE   = false;
var isWin  = false;
var isMac  = false;
var isSafari = false;
var hasPopup = false;
setDefaults();

if (navigator.appName == "Netscape") {
	isNav = true;
	if (parseInt(navigator.appVersion) == 4) isNav4 = true;
	if (parseInt(navigator.appVersion) == 5) isNav5 = true;
} else if (navigator.appName == "Microsoft Internet Explorer") {
	isIE = true;
	if (navigator.appVersion.indexOf("MSIE 4") != -1) isIE4 = true;
	if (navigator.appVersion.indexOf("MSIE 5") != -1) isIE5 = true;
}

if (navigator.platform.indexOf("Win") != -1) isWin = true;
if (navigator.platform.indexOf("Mac") != -1) isMac = true;
if (navigator.userAgent.indexOf("Safari") != -1) isSafari = true;

if (ckJava) { checkJava(); }


function setDefaults() {
	if (typeof ckMedia == "undefined") ckMedia = true;
	if (typeof ckSW == "undefined") ckSW = true;
	if (typeof ckFlash == "undefined") ckFlash = true;
	if (typeof ckJava == "undefined") ckJava = false;
	if (typeof ckPopup == "undefined") ckPopup = false;
	if (typeof WMP_version == "undefined") WMP_version = "0";
	if (typeof WMP7_version == "undefined") WMP7_version = 'unknown';
}

function popupWindow() {
	pwin = window.open('about:blank','popup','resizable=no,width=100,height=100,left=10,top=10,screenX=10,screenY=10');
	if (pwin) {
		hasPopup = true;
		setTimeout('pwin.close()',2000);
	}
}

function OpenWindow(theURL,winName,features) {
	avawin = window.open(theURL,winName,features);
	avawin.focus();
}

function CloseWindow() {
	avawin.close();
}

function AvaWinPlayer(theURL,w,h,r) {
	if (typeof w == "undefined") w = 750;
	if (typeof h == "undefined") h = 563;
	if (typeof r == "undefined") r = "yes";
	playerwin =  window.open(theURL,'AvaPlayer','resizable=' + r + ',width=' + w + ',height=' + h + ',left=10,top=10,screenX=10,screenY=10');
	playerwin.focus();
}

function AvaWinPlayer_noresize(theURL) {
	playerwin = window.open(theURL,'AvaPlayer','resizable=no,width=750,height=563,left=10,top=10,screenX=10,screenY=10');
	playerwin.focus();
}

function AvaWinAdmin(theURL) {
	// DJC 05/16/05 Hack for new Safari window sizing bug
	if (isSafari) {
		w = 920; h = 720;
	} else {
		w = 900; h = 700;
	}
	if ((isMac) && (isIE)) {
		adminwin =  window.open(theURL,'AvaAdmin','resizable=yes,width=' + w + ',height=' + h + ',left=5,top=5,screenX=5,screenY=5');
	} else {
		adminwin = window.open(theURL,'AvaAdmin','resizable=no,width=' + w + ',height=' + h + ',left=5,top=5,screenX=5,screenY=5');
	}
	adminwin.focus();
}

function replaceEntryPage(thisUrl) {
	// Dummy function in case this gets called unintentionally
	// used in preflt_bottom.php, where it is also defined locally
}

function AVA_OpenOnUnload(theURL,w,h) {
  winOpts = "width=" + w + ",height=" + h + ",scrollbars=yes,left=10,top=10,screenX=10,screenY=10";
  window.open(theURL,'_blank',winOpts);
}

function ckWinResize(mediaType) {
	if (mediaType == "Real") {
		window.resizeBy(1,1);
	}
	if ((mediaType == "QuickTime") && isWin) {
		setTimeout('window.resizeBy(1,1)', 15000);
	}
}

function redrawVideo( mediaType, left ) {
	if ( !videoOnScreen )
		return;

	if (typeof Layer1.style == "object") {
		//if (mediaType == "Windows Media Player") {
			//video1.fullscreen = true;
			//video1.fullscreen = false;
		//}
		Layer1.style.left = left - 1;
		var s = 'Layer1.style.left = "' + left + '"';
		setTimeout(s, 10);
		//setTimeout("redrawVideo('" + mediaType + "',"+ left + ");", 5);
		window.status = document.body.offsetWidth + " " + document.body.offsetHeight;
	}
}

// a client should call this to let the redrawVideo function know whether or not
// this layout contains video or not. used to handle dynamic layout changes
// added: 1/15/05 RF
function setVideoOnScreen( state ) {
	videoOnScreen = state;
}


// added by RF to handle dynamic layout changes
function handleUpdateVideoSize( x, y, w, h ) {
	if ( document.layers ) {
		var avaLayerObj = document.layers['Layer1'];
	} else {
		var avaLayerObj = document.getElementById("Layer1");
	}
	
	var avaVidObj = document.video1;
	
	if ( avaVidObj != null ) {
	
		if ( w != null ) {
			avaVidObj.width = w;
			avaLayerObj.style.width = w;
		}
		
		if ( h != null ) {
			avaVidObj.height = h;
			avaLayerObj.style.height = h;
		}
		
		if ( x != null ) {
			avaVidObj.left = x;
			avaLayerObj.style.left = x;
		}
		
		if ( y != null ) {
			avaVidObj.top = y;
			avaLayerObj.style.top = y;	
		}
		
	}
	
	//alert("handleUpdateVideoSize() " + w + ", " + h );
}

// UNUSED: added by RF to handle dynamic layout changes
function handleUpdateWindowSize( w, h ) {
	if ( !w ) w = 750;
	if ( !h ) h = 563;
	
	self.resizeTo( w, h );
	
	//alert( "setting size to : " + w + ", " + h );

}




// added by RF, open the slide/file uploader window
function openFileUploadWindow( userID, eventID, component, action ) {	
	//set
	var page = "/cgi-bin/upload.pl?eventID=" + eventID + ";component=" + component + ";action=" + action + ";userID=" + userID;
	fileUploadWin = window.open( page, "Upload", "resizable=no, width=540, height=250, left=400, top=250,screenX=400, screenY=250");
	fileUploadWin.focus();
}


function handleWebSharing( action, url, width, height, top, left, toolbar ) {
	
	if ( action == "close" ) {
		if ( webShareWin != null )
			webShareWin.close();
		
	} else {
		var attributes = getPopupAttributes( width, height, top, left, toolbar );	
		//alert( attributes );
		webShareWin = window.open( url, "WebSharing", attributes );
		//webShareWin.focus();
	}
}

function handleScreenSharing( action, url, width, height, top, left, toolbar ) {
	
	if ( action == "close" ) {
		if ( screenShareWin != null )
			screenShareWin.close();
		
	} else {
		var attributes = getPopupAttributes( width, height, top, left, toolbar );	
		//alert( attributes );
		screenShareWin = window.open( url, "ScreenSharing", attributes );
		//webShareWin.focus();
	}
}


function handleFileSharing( action, url, width, height, top, left, toolbar ) {

	var attributes = getPopupAttributes( width, height, top, left, toolbar );	
	//alert( attributes );
	
	// just pass it the filename so that it will not open multiple windows of the same file
	//var filename = url.substring( url.lastIndexOf("/") + 1, url.length );
	//filename = filename.substring( 0, filename.lastIndexOf(".") );
	//alert( filename );
	
	window.open( url, "", attributes );	
}

function handleDownload( path ) {
	//top.loader.document.location.href = path;
	var attributes = getPopupAttributes( 10, 10, 0, 0, "no" );
	downloadWin = window.open( path, "downloadWin", attributes );	
	
	try {
		setTimeout( "closeDownloadWindow()", 2000 );
	
	} catch ( e ) {
	
	}
}

function closeDownloadWindow() {
	if ( downloadWin != null )
		downloadWin.close();
}


function closePopups() {
	if ( screenShareWin != null )
		screenShareWin.close();
		
	if ( webShareWin != null )
		webShareWin.close();
		
	if ( downloadWin != null )
		downloadWin.close();
		
}




function getPopupAttributes( width, height, top, left, toolbar ) {
	var attributes = "scrollbars=yes, resizable=yes, status=yes, width=" + width + ", height=" + height + ", left=" + left + 
	", top=" + top + ", screenX=" + left + ", screenY=" + top + ", toolbar=" + toolbar;
	
	return attributes;
}


function DisplayConfig(preflightLink, usingRP, usingWMP, usingQT, bgColor, textColor, textSize, textFont, eventID, flashVer) {
	var extraText, statusRP, statusWMP, statusSW, statusFlash, statusJava, statusPopup, statusborderColor;
	borderColor = textColor;
	if (preflightLink == "no") {
		extraText = "&nbsp;&nbsp;&nbsp;&nbsp;";
	} else {
		extraText = 'Make sure you are ready to go! Run your computer through the <a href="/preflight/index.php?eventID=' + eventID + '">Player Preflight tests<\/a>.';
	}
	var installed = '<img src="/graphics/checkmark.gif" width=18 height=15>';
	var notinstalled = '&nbsp;&nbsp;';
	if (ckPopup) {
		popupWindow();
		if (hasPopup) { statusPopup = installed; } else { statusPopup = notinstalled; }
	}
	// has* and javaVer defined in mediacheck.js
	if (hasRP) { statusRP = installed; } else { statusRP = notinstalled; }
	if (hasWMP) { statusWMP = installed; } else { statusWMP = notinstalled; }
	if (WMP_version != "0") {
		statusWMP += 'version: <b>' + WMP_version + '<\/b>';
		if ((WMP_version == "6.4") && (WMP7_version == "unknown")) statusWMP += '+<br>(exact version unknown)';
	}
	if (hasQT) { statusQT = installed; } else { statusQT = notinstalled; }
	if (ckJava && hasJava) {
		statusJava = installed + 'version: <b>' + javaVer + '<\/b>';
	} else {
		statusJava = notinstalled;
	}
	if (hasShockwave) { statusSW = installed; } else { statusSW = notinstalled; }
	statusSW += 'version: <b>' + shockwaveVer + '<\/b>';
	// flashVer defined in flashck.js
	if (typeof flashVer != "undefined") { 
		if (flashVer < 6) { statusFlash = notinstalled; } else { statusFlash = installed; }
		statusFlash += 'version: <b>' + flashVer + '<\/b>';
	} else {
		statusFlash = "unknown";
	}
	var tableStyle;
	tableStyle = 'style="border-style: solid; border-width: 2px;';
	tableStyle += ' border-color: ' + borderColor + ';';
	tableStyle += ' background-color: ' + bgColor + ';';
	tableStyle += '"';
	document.write('<table width=516 cellpadding=2 cellspacing=0 border=0 ' + tableStyle + '>\n');
	document.write('<tr><td width=340>\n');
	document.write('<table width=340 cellpadding=3 cellspacing=0 border=0>\n');
	document.write('<tr><td colspan=2 align=center><b>Browser Status<\/b><\/td><\/tr>\n');
	if (usingRP) document.write('<tr><td align=right valign=top>RealPlayer:<\/td><td>' + statusRP + '<\/td><\/tr>\n');
	if (usingWMP) document.write('<tr><td align=right valign=top>Windows Media:<\/td><td>' + statusWMP + '<\/td><\/tr>\n');
	if (usingQT) document.write('<tr><td align=right valign=top>QuickTime:<\/td><td>' + statusQT + '<\/td><\/tr>\n');
	if (ckFlash) document.write('<tr><td align=right valign=top>Macromedia Flash:<\/td><td>' + statusFlash + '<\/td><\/tr>\n');
	if (ckSW) document.write('<tr><td align=right valign=top>Macromedia Shockwave:<\/td><td>' + statusSW + '<\/td><\/tr>\n');
	if (ckJava) document.write('<tr><td align=right valign=top>Java:<\/td><td>' + statusJava + '<\/td><\/tr>\n');
	if (ckPopup) document.write('<tr><td align=right valign=top>Window Pop-ups:<\/td><td>' + statusPopup + '<\/td><\/tr>\n');
	document.write('<\/table><\/td>\n');
	document.write('<td width=7>&nbsp;&nbsp;<\/td>\n');
	document.write('<td width=146>' + extraText + '<\/td>\n');
	document.write('<td width=7>&nbsp;&nbsp;<\/td>\n');
	document.write('<\/tr><\/table>\n');
	document.write('<br>\n');
}

function DisplayConfigMsg() {
	// Shockwave
	if ((ckSW) && (hasShockwave == false)) {
		var xx;
		if (typeof minShockwaveVer == "undefined") minShockwaveVer = 9;
		if (typeof avaMode == "undefined") avaMode = "player";
		if (avaMode == "player") {xx = "Shockwave Version link";} else {xx = "Avacaster Administrator";}
		document.write('<p>To use the ' + xx + ', you must have Macromedia Shockwave (version ' + minShockwaveVer + ' or greater). Install it by <a href="http://www.macromedia.com/shockwave/download/">clicking here<\/a>.');
		if (isWin && isIE) 
			document.write(' Windows Internet Explorer users: the browser will try to install it automatically if you click a Shockwave link below.)');
		if (isMac) 
			document.write(' OS X: To find out the exact version, check the Preflight page');
		document.write('<\/p>');
	}

	// Flash
	if ((ckFlash) && (actualFlashVersion < 6)) {
		document.write('<p>To use the Flash Version link, you must have Macromedia Flash (version 6.0 or greater). Install it by <a href="http://www.macromedia.com/software/flashplayer/">clicking here<\/a>. (Windows Internet Explorer users: the browser may attempt to install Flash automatically.)<\/p>');
	}

	if (ckMedia) {
		// Real
		if ((usingRP == true) && (hasRP == false)) {
			document.write('<p>RealPlayer was not detected. If you want to use <a href="http://proforma.real.com/real/player/player.html?src=001222realhome_1,010110rpchoice_h1&amp;dc=124123122" target="_blank">RealPlayer<\/a> you must download the latest version.<\/p>');
		}
		// Windows Media
		if ((usingWMP == true) && (hasWMP == false)) {
			var dnld = '<a href="http://www.microsoft.com/windows/windowsmedia/en/download/default.asp" target="_blank">download the latest version<\/a>.';
			if (WMP_version == "0") {
				document.write('<p>Windows Media was not detected. To use this media type, please ' + dnld);
			} else {
				if (isIE && isWin) {
					document.write('<p>You need a newer version of Windows Media (' + minWMPver + '). To use this media type, please ' + dnld);
				} else {
					document.write('<p>Unable to determine whether Windows Media is the minimum required version (' + minWMPver + '). You may need to ' + dnld);
				}
			}
			if (isMac) 
			document.write(' Please note that Windows Media is not recommended on Macs unless using version 9 in Safari on OS 10.3.');
			document.write('<\/p>');
		}
		// QuickTime
		if ((usingQT == true) && (hasQT == false)) {
			document.write('<p>QuickTime was not detected. If you want to use <a href="http://www.apple.com/quicktime/download/" target="_blank">QuickTime<\/a> you must download the latest version.<\/p>');
		}
	}
	
	// Java
	var msg = '';
	var n = 'Update'; var u; var u2; var f;
	if (ckJava) {
		if (hasJava == false) {
			msg = 'Java was not detected. Java is required';
		} else if (javaVer == 'unknown') {
			msg = 'Unable to determine your Java version. Java ' + minJavaVer + ' or higher is recommended';
		} else if (hasMinJava == false) {
			msg = 'A newer version of Java (' + minJavaVer + ' or higher) is recommended';
		} else { msg = ''; }
	}
	if (msg != '') {
		document.write('<p>' + msg + ' if you intend to use the Screen Sharing feature (ask your event coordinator if you are not sure).');
		if (isMac) {
			u = 'http://www.info.apple.com/support/downloads.html';
			u2 = 'http://docs.info.apple.com/article.html?artnum=75097';
			f = 'scrollbars=yes,resizable=yes,width=640,height=480,left=25,top=25,screenX=25,screenY=25';
			document.write(' Visit the Apple web site to download the latest <a href="#" onclick="OpenWindow(\'' + u + '\',\'' + n + '\',\'' + f + '\');return false;">Java OSX Update<\/a> or <a href="#" onclick="OpenWindow(\'' + u2 + '\',\'' + n + '\',\'' + f + '\');return false;">MRJ (OS9 users)<\/a>.');
			if (isIE) document.write(' IE 5.2 users on OS X: Note that it may not be possible to update your Java version.');
		} else {
			u = 'http://www.java.com/en/download/';
			f = 'menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,width=640,height=480,left=25,top=25,screenX=25,screenY=25';
			document.write(' Screen sharing may work fine, but if it doesn\'t, try installing <a href="#" onclick="OpenWindow(\'' + u + '\',\'' + n + '\',\'' + f + '\');return true;">Java from Sun Microsystems<\/a>.');
		}
		document.writeln('<\/p>');
	}
	
	// Popup windows
	if ((ckPopup) && (hasPopup == false)) {
		document.writeln('<p>Pop-up windows are blocked in this browser. You will not be able to use the Avacaster Sharing feature unless you unblock them. Ask your event coordinator if you are not sure whether this feature will be used.<\/p>');
	}
	
}
