
//----------------------------------------------------------------------------------------------------------------------

function SettingsScreen() {
	// Get the Save button element and assign an 'onclick' event to it.
	var self = this;
	var saveButton = document.getElementById( "save_button" );
	saveButton.onclick = function() {
		 self.onSaveClicked();
	};
	
	// Get all the UI elements that we can interact with.
	this.dropRefresh = document.getElementById( "drop_refresh" );

	this.cbRememberMe = document.getElementById( "setings_remember_me" );
	// Click handling via styles for touch screens.
	this.cbRememberMe.onclick = function() {
		Helper.handleCustomControlClick( self.cbRememberMe );
	};		
	
	//------

	this.radioUpdates = document.getElementById( "radio_updates" );
	// Click handling via styles for touch screens.
	this.radioUpdates.onclick = function() {
		Helper.handleCustomControlClick( self.radioUpdates );
		Helper.handleCustomControlClick( self.radioNewgame );
		Helper.handleCustomControlClick( self.radioInbox );
		Helper.handleCustomControlClick( self.radioOutbox );
	};		
	
	this.radioNewgame = document.getElementById( "radio_newgame" );
	// Click handling via styles for touch screens.
	this.radioNewgame.onclick = function() {
		Helper.handleCustomControlClick( self.radioUpdates );
		Helper.handleCustomControlClick( self.radioNewgame );
		Helper.handleCustomControlClick( self.radioInbox );
		Helper.handleCustomControlClick( self.radioOutbox );
	};		

	this.radioInbox = document.getElementById( "radio_inbox" );
	// Click handling via styles for touch screens.
	this.radioInbox.onclick = function() {
		Helper.handleCustomControlClick( self.radioUpdates );
		Helper.handleCustomControlClick( self.radioNewgame );
		Helper.handleCustomControlClick( self.radioInbox );
		Helper.handleCustomControlClick( self.radioOutbox );
	};		

	this.radioOutbox = document.getElementById( "radio_outbox" );
	// Click handling via styles for touch screens.
	this.radioOutbox.onclick = function() {
		Helper.handleCustomControlClick( self.radioUpdates );
		Helper.handleCustomControlClick( self.radioNewgame );
		Helper.handleCustomControlClick( self.radioInbox );
		Helper.handleCustomControlClick( self.radioOutbox );
	};		
}

//----------------------------------------------------------------------------------------------------------------------

SettingsScreen.prototype.setRefreshDropDown = function( refresh ) {
	if ( refresh != null ) {
		for (var i = 0; i < this.dropRefresh.options.length; ++i) {
			var value = this.dropRefresh.options[i].value;
			if ( value == refresh ) {
				this.dropRefresh.selectedIndex = i;
				break;
			}
		}
	}
}

//----------------------------------------------------------------------------------------------------------------------

SettingsScreen.prototype.onSaveClicked = function() {
	// Save the data to the storage.
	widget.setPreferenceForKey( this.dropRefresh.options[this.dropRefresh.selectedIndex].value, SettingsScreen.KEY_REFRESH );
	widget.setPreferenceForKey( this.cbRememberMe.checked.toString(), SettingsScreen.KEY_REMEMBER_ME );
	
	if(this.radioNewgame.checked) 
		widget.setPreferenceForKey( SettingsScreen.NEWGAME_PAGE , SettingsScreen.KEY_STARTUP_PAGE );
	else if(this.radioInbox.checked) 
		widget.setPreferenceForKey( SettingsScreen.INBOX_PAGE , SettingsScreen.KEY_STARTUP_PAGE );
	else if(this.radioOutbox.checked) 
		widget.setPreferenceForKey( SettingsScreen.OUTBOX_PAGE , SettingsScreen.KEY_STARTUP_PAGE );
	else
		widget.setPreferenceForKey( SettingsScreen.UPDATES_PAGE , SettingsScreen.KEY_STARTUP_PAGE );
	
	// Notify user that the changes were saved.
	alert( StringTable.Code.settingsSavedAlert );
}

//----------------------------------------------------------------------------------------------------------------------

SettingsScreen.prototype.onActivated = function() {
	// Load the stored settings and set the UI. Do this when the screen is activated
	// so that it reflects changes from the login page.
	var refresh = widget.preferenceForKey( SettingsScreen.KEY_REFRESH );
	var rememberMe = widget.preferenceForKey( SettingsScreen.KEY_REMEMBER_ME );
	var startupPage = widget.preferenceForKey( SettingsScreen.KEY_STARTUP_PAGE );

	// Assign to UI controls.
	this.setRefreshDropDown( refresh );
	this.cbRememberMe.checked = (rememberMe == "true");
	// We need to update our custom checkbox to reflect the checked state manually.
	Helper.handleCustomControlClick( this.cbRememberMe );

	if (startupPage == SettingsScreen.NEWGAME_PAGE) 
		this.radioNewgame.checked = true;
	else if (startupPage == SettingsScreen.INBOX_PAGE) 
		this.radioInbox.checked = true;
	else if (startupPage == SettingsScreen.OUTBOX_PAGE) 
		this.radioOutbox.checked = true;
	else
		this.radioUpdates.checked = true;
	
	// We need to update our custom checkbox to reflect the checked state manually.
	Helper.handleCustomControlClick( this.radioUpdates );
	Helper.handleCustomControlClick( this.radioNewgame );
	Helper.handleCustomControlClick( this.radioInbox );
	Helper.handleCustomControlClick( this.radioOutbox );
}

//----------------------------------------------------------------------------------------------------------------------

SettingsScreen.KEY_REFRESH = "com.fatbookie.widget.refresh";
SettingsScreen.KEY_REMEMBER_ME = LoginScreen.KEY_REMEMBER_ME;
SettingsScreen.KEY_STARTUP_PAGE = "com.fatbookie.widget.startup";

SettingsScreen.UPDATES_PAGE = "updates_page";
SettingsScreen.NEWGAME_PAGE = "newgame_page";
SettingsScreen.INBOX_PAGE = "inbox_page";
SettingsScreen.OUTBOX_PAGE = "outbox_page";

//----------------------------------------------------------------------------------------------------------------------
