// js for wrapper

var gwSessionId = null;
var gwMainLobbyWin = null;
var gwLoginComplete = false;
var gwWrapperLoaded = false;
var gwUserInfo = null;

var gwFlashLoadTimeoutInterval = 25000;

function init(defaultTab) {
	var flashVersion = flashLoader.getFlashVersion();
	var flashClientNeedsVersion = "9,0,45,0";
	
	// if have strange exception, log it
	if (flashVersion.join[","] == flashLoader.ACTIVE_X_EXCEPTION_FLASH_VERSION) {
		gaTrackEvent('lobby flash ActiveX exception');
	}
	// if no flash or flash is too old to support inline, go to upgrade page
	else if (! flashLoader.checkFlashVersion(flashLoader.INLINE_UPGRADE_VERSION, flashVersion)) {
		gaTrackEvent('lobby flash ActiveX exception');
		document.location.href = gUpdateFlashUrl + flashVersion;
	}
	// if flash version supports inline upgrade, use that
	else if (! flashLoader.checkFlashVersion(flashClientNeedsVersion, flashVersion)) {
		var upgradeMovie = document.getElementById("upgradeMovie");
		upgradeMovie.style.display = "block";
	}
	// we have a good version of Flash, can proceed
	else {
		setupFadeLayer("modallayer");
		loadMainLobby();
	}
	
	gwWrapperLoaded = true;	
}

var gProgressTick = 1;
function loadProgress(id, code, msg, flashVersion) {
	if (code < 0) {
		// for -3, -5, or -11, refresh
    	document.getElementById('progress').style.borderColor = 'red';
		lobbyWrapperApi.ajaxLogError(id, code, msg, flashVersion, navigator.userAgent.replace('#','HASH'));
    	mlWrapper.showLoadingErrorAlert(code);
	} else {
		if( gProgressTick <= 9)
			document.getElementById('progress'+gProgressTick++).style.visibility = 'visible';
	}
}

function hideLoadingAnimation() {
	var loadingElt = document.getElementById("loadinganimation");
	loadingElt.style.display = "none";
	var timeoutMessage = document.getElementById("flashloadtimeoutmessage");
	timeoutMessage.style.display = "none";
	var modallayer = document.getElementById("modallayer");
	modallayer.style.display = "none";
}

function showFlashLoadTimeoutMessage() {
	if (! gwSessionId) {
		var timeoutMessage = document.getElementById("flashloadtimeoutmessage");
		timeoutMessage.style.display = "block";
		gaTrackClick('lobby flash load timeout shown');
	}
}

function loadMainLobby() {
	var lobbyarea = document.getElementById("mainlobby");
	var lobbyIframe = '<iframe width="1207" height="2500" noresize scrolling=no frameborder=0 marginheight=0 marginwidth=0 src="' 
		+ gMainLobbyUrl + '"></iframe>';
	lobbyarea.innerHTML = lobbyIframe;
}	

// called from gCloseEmbeddedWindow when the "little window" flow completes
function closeChild(iframewindow) { 
	var dlog = document.getElementById("loginzone");
	dlog.style.display = "none";
	var modallayer = document.getElementById("modallayer");
	modallayer.style.display = "none";

	gwLoginComplete = true;
	joinToRevealLobby();

	return false;
}

function saveSessionId(sessionId) {
	gwSessionId = sessionId;
	loadProgress("saveSessionId");
	joinAuthSession();
}

function showLittleWindow(url) {
	if (url && (url.length > 0)) {
		var modallayer = document.getElementById("modallayer");
		modallayer.style.display = "block";

		var wrapper = document.getElementById("loginzone");
		var loginarea = document.getElementById("loginarea");
		loginarea.src = url;
		wrapper.style.display = "block";
	}
}

function hideFakeLobby() {
	var mainlobby = document.getElementById("mainlobby");
	mainlobby.style.left = "0px";
	var fakebg = document.getElementById("fakebg");
	fakebg.style.display = "none";
	loadProgress("hideFakeLobby");
}

// called from MainLobby iframe child

function showLoginWindow() {
	showLittleWindow(gLoginUrl);
}

function showChooseScreenNameWindow() {
	showLittleWindow(gChooseScreenNameUrl);
}

function finishedLoadingLobby(childWin) {
	gwMainLobbyWin = childWin;
	gwMainLobbyWin.processDeferredFlashActions();
	
	joinToRevealLobby();
}

function joinToRevealLobby() {
	if (gwWrapperLoaded && gwLoginComplete && gwMainLobbyWin) {
		lobbyWrapperApi.fetchUserInfo();
	}
}

function joinAuthSession() {
	if (gwUserInfo && gwSessionId) {
		lobbyWrapperApi.ajaxAuthSession(gwSessionId);
	}
}

function authComplete(userInfo) {
	loadProgress("authComplete");
	hideLoadingAnimation();
	
	// another userStatusBar update in case currency update happened
	gwUserInfo = userInfo;
	gwMainLobbyWin.openMainLobby(userInfo);
}

function userInfoAvailable(userInfo) {
	gwUserInfo = userInfo;
	gwMainLobbyWin.loginUser(gwUserInfo);

	// start the timer for the "still loading" animation
	setTimeout(showFlashLoadTimeoutMessage, gwFlashLoadTimeoutInterval);

	joinAuthSession();
}

///////////////////////////////////////////////////////
// lobbyWrapperApi holds the client-server functions
// that trigger AJAX messages.

lobbyWrapperApi = {};

lobbyWrapperApi.ajaxAuthSession = function(sessionId) {
	return;
};

lobbyWrapperApi.ajaxAuthSessionResponse = function(json) {
	if( json.action == "login" ) {
		showLoginWindow();
	}
	else if( json.action == "chooseScreenName" ) {
		showChooseScreenNameWindow();
	}
	else if( json.action == "alert" ) {
		alert(json.error);
	}
	else if( json.action == "refresh" ) {	// got dup session error
		if (gwMainLobbyWin)
			gwMainLobbyWin.flashClientAction({msgId: "application_terminated"});
		else {	// this case should never happen since dup session comes back from MainLobby child
			alert("You have logged in somewhere else and can no longer play poker in this window.\n\nPlease close the window and try again."); 
		}
	}
	else if (json.action == "bannedUser") {
		mlWrapper.gotoBannedPage();
	}
	else if (json.success == true) {
		authComplete(json.userInfo);
	}
};

lobbyWrapperApi.fetchUserInfo = function() {
	// client calls this to request userinfo from server,
	// server will then call lobbyWrapperApi.userInfoResponse() or lobbyWrapperApi.showLittleWindow(url)
	return;
};

lobbyWrapperApi.showLittleWindow = function(url) {
	showLittleWindow(url);
};

lobbyWrapperApi.userInfoResponse = function(info) {
	loadProgress("userInfoResponse");
	userInfoAvailable(info);
};

lobbyWrapperApi.ajaxLogError = function() {
	return;
};
