
//----------------------------------------------------------------------------------------------------------------------

function InviteFriendsScreen() {
	// Get the find button element and assign an 'onclick' event to it.
	var self = this;
	
	var findButton = document.getElementById( "find_button" );
	findButton.onclick = function() {
		 self.onFindClicked();
	};

	var resetButton = document.getElementById( "reset_invite_friends_button" );
	resetButton.onclick = function() {
		 self.onResetClicked();
	};

	var finishButton = document.getElementById( "finish_invite_friends_button" );
	finishButton.onclick = function() {
		 self.onFinishClicked();
	};
	
	self.textbox_find = document.getElementById( "textbox_find" );
	self.friendsTable = document.getElementById( "search_friends_table" );
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onActivated = function() {
	var self = this;

	self.onResetClicked();
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onResetClicked = function() {
	var self = this;

	// Initialize search screen
	self.textbox_find.value = "";
	self.friendsTable.innerHTML = "<tr><th>&nbsp;</th></tr>";
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onFinishClicked = function() {
	if(confirm( StringTable.Code.inviteFriendsConfirmFinish ))
	{
		// Load startup page
		Helper.activateHomeScreen();
	}
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onFindClicked = function() {
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieSearchFriendsResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.searchFriends(self.textbox_find.value);
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onFatBookieSearchFriendsResponse = function( response ) {
	var self = this;

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			if(response.friends != undefined) {
				var html = "";

				// Fill in the table.
				for ( var i = 0; i < response.friends.length; i++ ) {
					var v_friend = response.friends[i];

					html += Helper.createFriendRow(v_friend.id, v_friend.username, v_friend.name);
				}
				
				self.friendsTable.innerHTML = html;
			}
			else
			{
				var html = "<tr><th>&nbsp;</th></tr>";

				self.friendsTable.innerHTML = html;
			}
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onInviteClicked = function(friend_id) {
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieInviteFriendsResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.inviteFriendsToGame(publishGameScreen.game_id, friend_id);
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onFatBookieInviteFriendsResponse = function( response ) {
	var self = this;

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			alert( StringTable.Code.inviteFriendsScreenSuccessInvite );
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

InviteFriendsScreen.prototype.onFBServiceError = function( status ) {
	// Alert the user that something has gone wrong.
	if(status == "Invalid session")
	{
		alert( StringTable.Code.inviteFriendsScreenInvalidSessionError );

		// Load the login screen to allow user to get a new session
		widgetMenu.activate( Menu.LOGIN_SCREEN );
	}
	else
		alert( StringTable.Code.inviteFriendsScreenDataError + " (" + status + ")" );
}

//----------------------------------------------------------------------------------------------------------------------
