
//----------------------------------------------------------------------------------------------------------------------

function UpdatesScreen() {
	// Get the refresh button element and assign an 'onclick' event to it.
	var self = this;
	
	var refreshButton = document.getElementById( "refresh_updates_button" );
	refreshButton.onclick = function() {
		 self.onRefreshClicked();
	};
}

//----------------------------------------------------------------------------------------------------------------------

UpdatesScreen.prototype.onActivated = function() {
	// Simply refresh the table
	this.onRefreshClicked();
}

//----------------------------------------------------------------------------------------------------------------------

UpdatesScreen.prototype.onRefreshClicked = function() {
	// Add event listeners.
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieUpdatesResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }

	// Start the request.
	fbService.getUpdatesList();
}

//----------------------------------------------------------------------------------------------------------------------

UpdatesScreen.prototype.onFatBookieUpdatesResponse = function( response ) {

	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			if(response.updates != undefined) {
				var updatesTable = document.getElementById( "updates_list_table" );
				var html = "";

				// Fill in the table.
				for ( var i = 0; i < response.updates.length; i++ ) {
					var v_period = response.updates[i];
					
					html += Helper.createPeriodRow(v_period.period_name);
					
					if(v_period.period_updates != undefined) {
						for ( var j = 0; j < v_period.period_updates.length; j++ ) {
							var v_update = v_period.period_updates[j];
							
							html += Helper.createUpdatesRow(v_update.from, v_update.from_name, v_update.body);
						}
					}
				}
				
				updatesTable.innerHTML = html;
			}
			else
			{
				var updatesTable = document.getElementById( "updates_list_table" );
				var html = "<tr><th>&nbsp;</th></tr>";

				updatesTable.innerHTML = html;
			}
		}
	}
	else
		alert( StringTable.Code.fatalAPIError );
}

//----------------------------------------------------------------------------------------------------------------------

UpdatesScreen.prototype.onFBServiceError = function( status ) {

	// Alert the user that something has gone wrong.
	if(status == "Invalid session")
	{
		alert( StringTable.Code.updatesScreenInvalidSessionError );

		// Load the login screen to allow user to get a new session
		widgetMenu.activate( Menu.LOGIN_SCREEN );
	}
	else
		alert( StringTable.Code.updatesScreenDataError + " (" + status + ")" );
}

//----------------------------------------------------------------------------------------------------------------------

