
//----------------------------------------------------------------------------------------------------------------------

function InboxScreen() {
	// Get the refresh button element and assign an 'onclick' event to it.
	var self = this;
	
	var refreshButton = document.getElementById( "refresh_inbox_button" );
	refreshButton.onclick = function() {
		 self.onRefreshClicked();
	};

	var prevButton = document.getElementById( "prev_inbox_button" );
	prevButton.onclick = function() {
		 self.onPrevClicked();
	};

	var nextButton = document.getElementById( "next_inbox_button" );
	nextButton.onclick = function() {
		 self.onNextClicked();
	};
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onActivated = function() {

	// Simply refresh the table
	this.onRefreshClicked();
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onRefreshClicked = function() {
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieInboxResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.getMessages("inbox", INBOX_CURRENT_PAGE);
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onPrevClicked = function() {
	
	if(INBOX_CURRENT_PAGE > 1)
		INBOX_CURRENT_PAGE--;
	else
		INBOX_CURRENT_PAGE = 1;

	this.onRefreshClicked();
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onNextClicked = function() {
	
	INBOX_CURRENT_PAGE++;
	
	this.onRefreshClicked();
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onMessageClick = function(id) {

	messageScreen.msg_id = id;
	messageScreen.show = "inbox";

	//activate the message screen
	widgetMenu.activate( Menu.VIEW_MESSAGE_SCREEN );
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onFatBookieInboxResponse = function( response ) {
	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			if(response.periods != undefined) {
				//adjust page overflow
				if( INBOX_CURRENT_PAGE > parseInt(response.max_pages) )
					INBOX_CURRENT_PAGE = parseInt(response.max_pages);

				var inboxTable = document.getElementById( "inbox_list_table" );
				var html = "";

				// Fill in the table.
				for ( var i = 0; i < response.periods.length; i++ ) {
					var v_period = response.periods[i];
					
					html += Helper.createPeriodRow(v_period.period_name);

					if(v_period.period_messages != undefined) {
						for ( var j = 0; j < v_period.period_messages.length; j++ ) {
							var v_message = v_period.period_messages[j];
							
							var v_date = new Date( (v_message.date).replace(/\-/g,"/") );
							
							html += Helper.createInboxRow(	v_message.id, 
															v_message.party, 
															v_message.party_name, 
															v_message.subject,
															v_date.toLocaleDateString(),
															v_message.is_read,
															v_message.flag
															);
						}
					}
				}
				
				inboxTable.innerHTML = html;
			}
			else
			{
				var inboxTable = document.getElementById( "inbox_list_table" );
				var html = "<tr><th>&nbsp;</th></tr>";

				inboxTable.innerHTML = html;
			}
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

InboxScreen.prototype.onFBServiceError = function( status ) {
	// Alert the user that something has gone wrong.
	if(status == "Invalid session")
	{
		alert( StringTable.Code.inboxScreenInvalidSessionError );

		// Load the login screen to allow user to get a new session
		widgetMenu.activate( Menu.LOGIN_SCREEN );
	}
	else
		alert( StringTable.Code.inboxScreenDataError + " (" + status + ")" );
}

//----------------------------------------------------------------------------------------------------------------------
