var globalCurrentSession = 0;
var sessionSaver = new Array(new Object(), new Object(), new Object());

for(var i = 0; i < 3; i++){
	sessionSaver[i]['cur'] = 0;
}

function sessionRoll(which, state, skin) {
	if(which != globalCurrentSession){
		if(state){
			document.images['sess' + which + 'icon'].src = skin + '/sess_roll.gif';
		} else {
			document.images['sess' + which + 'icon'].src = skin + '/sess_off.gif';
		}
	}
}

function showSessionFrame(which){
	if(sessionSaver[which]['cur'] == 0){
		document.all['session' + which].style.display = 'block';
	} else {
		sessionSaver[which]['sub' + sessionSaver[which]['cur']].style.display = 'block';
	}
}

function hideSessionFrame(){
	if(sessionSaver[globalCurrentSession]['cur'] == 0){
		document.all['session' + globalCurrentSession].style.display = 'none';
	} else {
		sessionSaver[globalCurrentSession]['sub' + sessionSaver[globalCurrentSession]['cur']].style.display = 'none';
	}
}

function sessionChg(which, skin){
	if(which != globalCurrentSession){
		document.images['sess' + globalCurrentSession + 'icon'].src = skin + '/sess_off.gif';
		document.images['sess' + which + 'icon'].src = skin + '/sess_on.gif';
		hideSessionFrame();
		globalCurrentSession = which;
		showSessionFrame(which);
	}
}

function popupSession(page){
	var newSessionNum = sessionSaver[globalCurrentSession]['cur'] + 1;
	var newSession = document.createElement("iframe");
	newSession.src = page;
	newSession.border = 0;
	newSession.frameBorder = 0;
	newSession.width = '100%';
	newSession.height = '100%';
	newSession.name = 'sess' + globalCurrentSession + 'sub' + newSessionNum;
	newSession.id = 'sess' + globalCurrentSession + 'sub' + newSessionNum;
	newSession.style.display = 'none';
	sessionSaver[globalCurrentSession]['sub' + newSessionNum] = newSession;
	document.all['hiddenSessionContainer'].appendChild(sessionSaver[globalCurrentSession]['sub' + newSessionNum]);
	hideSessionFrame();
	sessionSaver[globalCurrentSession]['cur']++;
	showSessionFrame(globalCurrentSession);
}

function callPassAlongFunction(passAlong){
	if(sessionSaver[globalCurrentSession]['cur'] == 0){
		window.frames['session' + globalCurrentSession].gotPassAlong(passAlong);
	} else {
		window.frames['sess' + globalCurrentSession + 'sub' + sessionSaver[globalCurrentSession]['cur']].gotPassAlong(passAlong);
	}
}

function finishSession(passAlong){
	if(sessionSaver[globalCurrentSession]['cur'] > 0){
		document.all['hiddenSessionContainer'].removeChild(sessionSaver[globalCurrentSession]['sub' + sessionSaver[globalCurrentSession]['cur']])
		sessionSaver[globalCurrentSession]['sub' + sessionSaver[globalCurrentSession]['cur']] = null;
		sessionSaver[globalCurrentSession]['cur']--;
		showSessionFrame(globalCurrentSession);
		callPassAlongFunction(passAlong);
	}
}

function clearSessions(newloc){
	if(sessionSaver[globalCurrentSession]['cur'] > 0){
		for(var i = sessionSaver[globalCurrentSession]['cur']; i > 0; i--){
			document.all['hiddenSessionContainer'].removeChild(sessionSaver[globalCurrentSession]['sub' + i]);
			sessionSaver[globalCurrentSession]['sub' + i] = null;
		}
		sessionSaver[globalCurrentSession]['cur'] = 0;
		showSessionFrame(globalCurrentSession);
		if(newloc){
			getTopSession().location = newloc;
		} else {
			getTopSession().gotPassAlong(null);
		}
	} else {
		if(newloc){
			getTopSession().location = newloc;
		}
	}
}

function clearOneSession(newloc){
	if(sessionSaver[globalCurrentSession]['cur'] > 0){
		document.all['hiddenSessionContainer'].removeChild(sessionSaver[globalCurrentSession]['sub' + sessionSaver[globalCurrentSession]['cur']])
		sessionSaver[globalCurrentSession]['sub' + sessionSaver[globalCurrentSession]['cur']] = null;
		sessionSaver[globalCurrentSession]['cur']--;
		showSessionFrame(globalCurrentSession);
		if(newloc){
			getTopSession().location = newloc;
		} else {
			getTopSession().gotPassAlong(null);
		}
	}
}

function getTopSession(){
	var cur = sessionSaver[globalCurrentSession]['cur'];
	if(cur > 0){
		return document.frames['sess' + globalCurrentSession + 'sub' + cur];
	} else {
		return document.frames['session' + globalCurrentSession];
	}
}