
//----------------------------------------------------------------------------------------------------------------------

function MiniView() {

}

//----------------------------------------------------------------------------------------------------------------------

MiniView.prototype.onActivated = function() {
	// Get the refresh rate.
	var refresh = parseInt( widget.preferenceForKey( SettingsScreen.KEY_REFRESH ) );
	if ( isNaN( refresh ) ) {
		refresh = 60000; // Default to 1 minute.
	}

	// Start the timer which refreshes the miniview.
	var self = this;
	this.timerId = setInterval( function(){
		self.refresh();
	}, refresh );
	
	// Timer will trigger after the refresh period so refresh manually now.
	this.refresh();
}

//----------------------------------------------------------------------------------------------------------------------

MiniView.prototype.refresh = function() {
	var self = this;
	fbService.onSuccess = function( response ) { self.onFatBookieUpdatesResponse( response ) }
	fbService.onError = function( status ) { self.onFBServiceError( status ) }
	
	fbService.getUpdatesList();
}

//----------------------------------------------------------------------------------------------------------------------

MiniView.prototype.onDeactivated = function() {
	// Stop timer.
	clearInterval( this.timerId );	
}

//----------------------------------------------------------------------------------------------------------------------

MiniView.prototype.onFatBookieUpdatesResponse = function( response ) {
	
	if(response.retcode != undefined) {
		if(parseInt(response.retcode) == 1)
			this.onFBServiceError(response.body);
		else
		{
			// Fill in dummy data here.
			var updatesTable = document.getElementById( "mini_view_table" );
			var html = Helper.createTableHeader( StringTable.Code.miniViewCaption );

			if(response.updates != undefined) {
				// Fill in the table.
				for ( var i = 0; i < response.updates.length && i < 3; i++ ) {
						var v_period = response.updates[i];
						
						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;
		}
	}
}

//----------------------------------------------------------------------------------------------------------------------

MiniView.prototype.onFBServiceError = function( status ) {
	var updatesTable = document.getElementById( "mini_view_table" );
	updatesTable.innerHTML = StringTable.Code.miniViewError;
}

//----------------------------------------------------------------------------------------------------------------------
