
var hostURL;

var viewerType; // = 'keypad';
var viewerTypeError;

//var pageNames = ['main_page', 'launch_page', 'inProgress_page', 'keypadClosed_page'];
//var launchPageSections = ['no_roomId', 'login', 'gameSelector', 'contacting_server', 'launch_error'];

function LWAPW_RefreshLaunchPage() {
	
	if (! roomId) {
		LWAPW_ShowPage('main_page');
		return;
	}
	
	if (LWAPW_sessionWindowOpen()) {
		LWAPW_ShowPage('inProgress_page');
		// $('inProgress_headerText').innerHTML = viewModeString+" Launch Sucessful";
		if (viewerType == 'Keypad') {
			$('inProgress_headerText').innerHTML = "Keypad Launch Sucessful";
			$('inProgress_msgText').innerHTML = "Your Keypad is now in the lower-right area of your screen. Use it to answer questions during the game.";
			$('windowWarning_img').src = "site_files/launchWindow_kp.png";
			$('windowWarning_img').width = 245;
			$('windowWarning_img').height = 115;
		} else {
			$('inProgress_headerText').innerHTML = "Game Viewer Launch Sucessful";
			$('inProgress_msgText').innerHTML = "Your Game Viewer is now in the upper-left area of your screen. Use it to view the game.";
			$('windowWarning_img').src = "site_files/launchWindow_fullPlayer.png";
			$('windowWarning_img').width = 280;
			$('windowWarning_img').height = 230;
		}
		return;
	}
	
	LWAPW_ShowPage('launch_page');
	
	$('sessid_display').firstChild.data  = "Welcome to Room "+roomId;
	
	if (viewerType == undefined) {
		if (viewerTypeError == null) {
			LWAPW_ShowLaunchSection('contacting_server');
			var args = { command:'get_viewerType' };
			args['muRoomInviteCode'] = roomId;
			LWAPW_StartAjaxRequest(launchPageDataURL, args);
		} else {
			LWAPW_ShowLaunchSection('launch_error');
		}
		return;
	}
	
	/*if (! loginState) {
		LWAPW_ShowLaunchSection('login');
		if (loginErrorMsg != '') {
			$('loginError').style.display = 'block';
			$('loginErrorText').innerHTML = loginErrorMsg;
		} else {
			$('loginError').style.display = 'none';
		}
		return;
	}*/
	
	LWAPW_ShowLaunchSection('viewerLaunch');
	
	
	if (viewerType == 'Keypad') {
		sessionWindowSize = kpWindowSize;
		$('viewerLaunch_msg').innerHTML = "To launch the keypad, click this button:";
		
		//$('inProgress_msgText').innerHTML = "Your Game Host is now in the upper-left area of your screen. Use it to control the game.";
		//$('windowWarning_img').src = "site_files/game_window_warning.png";
	} else {
		sessionWindowSize = playerWindowSize;
		$('viewerLaunch_msg').innerHTML = "To launch the game viewer, click this button:";
		
		//$('inProgress_msgText').innerHTML = "Your Game Host is now in the upper-left area of your screen. Use it to control the game.";
		//$('windowWarning_img').src = "site_files/game_window_warning.png";
	}
	
	
}

function LWAPW_launchViewer() {
	var qString = "room="+roomId;
	// to hide the parameters, use a form
	//  NOT: hostURL = "host.php?"+qString;
	if (viewerType == 'Keypad') {
		windowURL =  baseURL + "kpWindow.php";
	} else {
		windowURL =  baseURL + "viewerWindow.php";
	}
	var formHTML = "<html><head></head><body>";
	formHTML += "<form id='LWAPW_LaunchWindow' method='post' action='" + windowURL + "'>";
	formHTML += "<input type='hidden' name='qString' value='" + qString + "'/>";
	formHTML += "</form>";
	formHTML += "<script type='text/javascript'>document.getElementById(\"LWAPW_LaunchWindow\").submit()</script>";
	formHTML += "</body></html>";
	//
	var w = sessionWindowSize.width;
	var h = sessionWindowSize.height;
	
	windowURL += "?"+qString;
	
	sessionWindow = window.open(windowURL,"AllPlayWeb","status=0,toolbar=0,titlebar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=0,height="+h+",width="+w);
	if(sessionWindow) {
		// sessionWindow.document.write(formHTML);
		LWAPW_LaunchConfirmed();
	} else {
		alert("We have detected that you are using popup blocking software.\The game will appear in a new window. Please enable popups and ensure that ActiveX is allowed.");
	}
}




// AJAX.Request Success, Failure, Error

function LWAPW_AjaxRequest_Success(command, jsonObj) {
	switch (command) {
		case 'get_viewerType':
			viewerType = jsonObj.viewerType;
			LWAPW_RefreshLaunchPage();
			break;
	}
}

function LWAPW_AjaxRequest_Failure(command, errorMsg) {
	switch (command) {
		case 'get_viewerType':
			alert(errorMsg);
			viewerTypeError = errorMsg;
			LWAPW_RefreshLaunchPage();
			break;
	}
}

function LWAPW_AjaxRequest_Error(command, errorMsg) {
	switch (command) {
		case 'get_viewerType':
			loginState = false;
			loginErrorMsg = errorMsg;
//			/LWAPW_RefreshLaunchPage();
			break;
	}
}