








// various logout URLs
var logoutMyBlink = 'http://blink-prod.ucsd.edu/MyBlink/0,1555,,Deactivate.html?target=';
var logoutBlinkCM = 'http://blink-prod.ucsd.edu/Blink/Internal/Logout/0,1157,,00.html?target=';
var logoutJlink   = 'https://www-act.ucsd.edu/security/logout?url=';

// initialize variables
var jlinkServer = '';
var jlinkSessionId = '';
var vgn_creds = '';
var blink_ident = '';
var loginStatus = 'FFF';
var a4retries = 3;

// get the final values for these variables after page rendering is complete
function reinitialize() {

	// get the authentication cookies
	window.jlinkServer = getCookieData('jlinkauthserver');
	window.jlinkSessionId = getCookieData('jlinksessionidx');
	window.vgn_creds = getCookieData('vgn_creds');
	window.blink_ident = getCookieData('Blink_Ident');

// alert('Blink_Ident = '+window.blink_ident+'\nvgn_creds = '+window.vgn_creds+'\njlinksessionidx = '+window.jlinkSessionId+'\njlinkauthserver = '+window.jlinkServer)

	// set the authentication server of the logout URL
	window.logoutJlink = window.logoutJlink.replace('AUTHSERVER',window.jlinkServer);

	// create a summary of the login status
	// three boolean positions indicating: myblink, BlinkCM, jlink
	window.loginStatus = '';
	if (window.eduUcsdActLoggedin) loginStatus += 'T';
	else loginStatus += 'F';
	if (window.vgn_creds != '') loginStatus += 'T';
	else loginStatus += 'F';
	if (window.blink_ident != '') loginStatus += 'T';
	else loginStatus += 'F';
}

// client side cookie parsing
function getCookieData(label) {
	var cookieArray = document.cookie.split(';');
	var cookieLabel, cookieValue;
	for (i in cookieArray) {
		cookieLabel = cookieArray[i].split('=')[0].replace(/(^\s|\s$)/g,'');
		cookieValue = unescape(cookieArray[i].split('=')[1]);
		if (cookieLabel == label) return cookieValue;
	}
	return '';
}

// used to set the bug images and the logout button image
function loadImages(repeat) {

	// guarantee that the a4 js bug variable is defined
	if (typeof(window.eduUcsdActLoggedin) == 'undefined' && window.jlinkServer != '' && window.jlinkSessionId != '') {

// only show this error message to dylan	
if (getCookieData('Blink_Ident') == escape('dfdoxey@ucsd.edu')) alert('A4 status check attempt ' + parseInt(window.a4retries - repeat + 1) + ' of ' + window.a4retries + '.	');

		// if A4 status is still unknow then wait and try this function call again later
		repeat = parseInt(repeat - 1);
		if (repeat > 0) {
			setTimeout('loadImages('+ repeat +');',100);
			return;
		} else {
			// if last wait cylcle has elapsed then assume A4 status is "false"
			window.eduUcsdActLoggedin = false;
		}
	}
	
	// set global variables after A4 status is defined
	reinitialize();

	// for backward compatibility
	window.jlink_active = window.eduUcsdActLoggedin;
	
	// if the user is authenticated via a4 and has a jlink auth-server then set the bug and logout images
	if(window.eduUcsdActLoggedin && window.jlinkServer != '') {
		// jlink status bug
		(new Image()).src = 'http://'+jlinkServer+'.ucsd.edu:82/jlinkservices/ka?ts='+(new Date()).getTime();
		// a4 status bug
		// (new Image()).src = 'https://a4.ucsd.edu/a4/bug.do?type=img?ts='+(new Date()).getTime();
		// header logout image
		// document.images['logoutImage'].src = '/Blink/Images/signoff.gif';
		// add the logout link with some fancy DOM stuff
		var logoutUL = document.getElementById('acctctrls');
		if (logoutUL) {
			var logoutLI = logoutUL.getElementsByTagName('li');
			if (logoutLI && logoutLI[0]) {
				var logoutAnchor = document.createElement('a');
				logoutAnchor.setAttribute('href','javascript:completeLogout(\'general\');');
				logoutAnchor.appendChild(document.createTextNode('Logout'));
				logoutLI[0].setAttribute('id','signout');
				logoutLI[0].appendChild(logoutAnchor);
			}
		}
	}
	// alert('A4 status is: "'+ window.eduUcsdActLoggedin+'"');
}

// invoked by client to initiate proper logout sequence
function FULLLOGOUT(logoutType) {

	// get the logout URL appropriate for this authentication status
	var logoutURL = composeLogoutURL(logoutType);
	
// textWin = window.open('/Dylan/JavaScriptTextWindow','textWin','toolbar=0,scrollbar=1');
// textWinText = window.loginStatus + ' :: ' + logoutType + '\n' + logoutURL;

// if (confirm(window.loginStatus + ' => ' + logoutURL)) window.location = logoutURL;

	window.location = logoutURL;
}

// Composes the correct Logout url based on current authentication status
// A logout URL will probably be a sequence of URLs which indicate a series of redirects.
// For example, logging out of Blink content manager and MyBlink looks like this:
//	http://blink.ucsd.edu/Blink/Internal/Logout/0,1157,,00.html?target=http%3A//blink.ucsd.edu/MyBlink/0%2C1555%2C%2CDeactivate.html%3Ftarget%3D
//	Where "http://blink.ucsd.edu/Blink/Internal/Logout/0,1157,,00.html" is the target URL
//	after completing processing "http://blink.ucsd.edu/Blink/Internal/Logout/0,1157,,00.html".
function composeLogoutURL(logoutType) {
	var default_logout = 'http://blink-prod.ucsd.edu/';
	var myblink_logout;
	var general_logout;
	// Provide some response URL to all possible authentication stati
	// LOGOUT URL NOTES:
	//   º Because Blink cannot redirect to HTTPS type urls
	//		all JLink logouts must occur before any Blink type logouts.
	//   º MyBlink on blink-dev requires BlinkCM level authentication,
	//		therefore MyBlink logout must occur before any BlinkCM logout.
	//   º Terminating URLs for each logout sequence:
	//		- if JLink is the only service being logged out then the terminating
	//		  URL must be specified. Using 'default_logout' is suggested.
	//		- both MyBlink and BlinkCM logouts default to Blink home page as
	//		  terminating URL if none is specified.
	//   º Invalid States:
	//		These are noted with "N/A" and are given the 'default_logout'. These are 
	//		states that shouldn't ever occur because a logout button is unavailable under
	//		those circumstances.
	switch (window.loginStatus) {
		case 'FFF':
			// N/A (no authentication implies that no logout is necessary)
			myblink_logout = default_logout;
			general_logout = default_logout;
			break;
		case 'FFT':
			//                      MyBlink
			myblink_logout = window.logoutMyBlink;
			// N/A
			general_logout = default_logout;
			break;
		case 'FTF':
			// N/A
			myblink_logout = default_logout;
			//                      BlinkCM
			general_logout = window.logoutBlinkCM;
			break;
		case 'FTT':
			//                      MyBlink                       BlinkCM
			myblink_logout = window.logoutMyBlink + escape(window.logoutBlinkCM);
			//                      BlinkCM
			general_logout = window.logoutBlinkCM;
			break;
		case 'TFF':
			// N/A
			myblink_logout = default_logout;
			//                      JLink
			general_logout = window.logoutJlink + escape(default_logout);
			break;
		case 'TFT':
			//                      JLink                       MyBlink
			myblink_logout = window.logoutJlink + escape(window.logoutMyBlink);
			//                      JLink
			general_logout = window.logoutJlink + escape(default_logout);
			break;
		case 'TTF':
			// N/A
			myblink_logout = default_logout;
			//                      JLink                       BlinkCM
			general_logout = window.logoutJlink + escape(window.logoutBlinkCM);
			break;
		case 'TTT':
			//                      JLink                       MyBlink                       BlinkCM
			myblink_logout = window.logoutJlink + escape(window.logoutMyBlink + escape(window.logoutBlinkCM));
			//                      JLink                       BlinkCM
			general_logout = window.logoutJlink + escape(window.logoutBlinkCM);
			break;
		default:
			// N/A (error condition)
			myblink_logout = default_logout;
			general_logout = default_logout;
			break;
	}
	var logoutURL;
	if (logoutType == 'myblink') logoutURL = myblink_logout;
	else if (logoutType == 'general') logoutURL = general_logout;
	else logoutURL = default_logout;
// if (window.blink_ident == 'dfdoxey@ucsd.edu') alert(window.loginStatus + ' -> ' + logoutURL);
	return logoutURL;
}

// Wait long enough to allow the page to finish rendering:
// · Set the "Logout" button graphic in the header (attempt 10 times if necessary)
setTimeout('loadImages(a4retries);',10);
