
//----------------------------------------------------------------------------------------------------------------------

function PublishGameScreen() {
	var self = this;

	var backButton = document.getElementById( "back_publish_game_button" );
	backButton.onclick = function() {
		 self.onBackClicked();
	};

	var publishButton = document.getElementById( "publish_game_button" );
	publishButton.onclick = function() {
		 self.onPublishClicked();
	};

	// Get all the UI elements that we can interact with.
	this.p_gameSubjectLabel		= document.getElementById( "p_gameSubjectLabel" );
	this.p_gameChipValueLabel	= document.getElementById( "p_gameChipValueLabel" );
	this.p_gameOption1Label		= document.getElementById( "p_gameOption1Label" );
	this.p_gameOption1Labe2		= document.getElementById( "p_gameOption1Labe2" );
	this.p_gameOption1Labe3		= document.getElementById( "p_gameOption1Labe3" );
	this.p_gameOption1Labe4		= document.getElementById( "p_gameOption1Labe4" );
	this.p_gameOption1Labe5		= document.getElementById( "p_gameOption1Labe5" );
}

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.game_id;

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.onActivated = function() {
	var self = this;

	self.game_id = null;

	// present data for review
	self.p_gameSubjectLabel.innerHTML	= newgameScreen.game_subject.value;
	self.p_gameChipValueLabel.innerHTML	= newgameScreen.game_chip_value.value;
	self.p_gameOption1Label.innerHTML	= "1. " + gameOptionsScreen.game_option1.value;
	self.p_gameOption1Labe2.innerHTML	= "2. " + gameOptionsScreen.game_option2.value;
	self.p_gameOption1Labe3.innerHTML	= "3. " + gameOptionsScreen.game_option3.value;
	self.p_gameOption1Labe4.innerHTML	= "4. " + gameOptionsScreen.game_option4.value;
	self.p_gameOption1Labe5.innerHTML	= "5. " + gameOptionsScreen.game_option5.value;
}

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.onBackClicked = function() {

	widgetMenu.activate( Menu.GAME_OPTIONS_SCREEN );
}

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.onPublishClicked = function() {
	var self = this;

	// Add event listeners.
	fbService.onSuccess = function( response ) { self.onFatBookiePublishGameResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.publishGame( newgameScreen.draft_id );
}

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.onFatBookiePublishGameResponse = function( response ) {
	var self = this;

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			self.onFBServiceError(response.body);
		else
		{
			if(response.game != undefined) {
				self.game_id = parseInt(response.game);
				alert(StringTable.Code.successfulPublishMessage);

				//reset wizard options
				gameOptionsScreen.game_option1.value = "";
				gameOptionsScreen.game_option2.value = "";
				gameOptionsScreen.game_option3.value = "";
				gameOptionsScreen.game_option4.value = "";
				gameOptionsScreen.game_option5.value = "";

				//jump to next wizard step
				widgetMenu.activate( Menu.INVITE_FRIENS_SCREEN );
			}
			else
				alert( StringTable.Code.emptyServerResponseWarning );
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

PublishGameScreen.prototype.onFBServiceError = function( status ) {
	// Alert the user that something has gone wrong.
	if(status == "Invalid session")
	{
		alert( StringTable.Code.publishGameScreenInvalidSessionError );

		// Load the login screen to allow user to get a new session
		widgetMenu.activate( Menu.LOGIN_SCREEN );
	}
	else
		alert( StringTable.Code.publishGameScreenDataError + " (" + status + ")" );
}

//----------------------------------------------------------------------------------------------------------------------
