/* Copyright 2009 Palm, Inc.  All rights reserved. */

var SettingsAssistant = Class.create({
	initialize: function(settings, themes, onThemeChange) {
		this.appController = Mojo.Controller.getAppController();
		
		this.settings = settings;
		this.themes = themes;
		this.onThemeChange = onThemeChange;
		
		this.onThemeSelect = this.onThemeSelect.bind(this);
		this.onRingerSwitchSet = this.onRingerSwitchSet.bind(this);
		
		this.initializeSettings();
		
		this.onKeyPress = this.onKeyPress.bind(this);
		
		this.easterString = "";
		
	},
	
	// VERY IMPORTANT: UI for ringer switch has OPPOSITE MEANING of variable
	// it's reversed here for display and must be reversed back when saving
	initializeSettings: function() {
		this.settingsModel = { };
		this.settingsModel.ringerSwitchObeyed = !(this.settings.ringerSwitchObeyedGet());
	},
	
	setup: function() {
		//this.controller.document.body.removeClassName("clockview");
		var appMenuModel = {
			visible: true,
			items: [
				//{label: $L('Preferences'), command:'cmdSettings'},
				Mojo.Menu.editItem,
				{label: $L('Preferences'), command:'cmdThemes', disabled:true},
				{label:$L('Help'), command:Mojo.Menu.helpCmd, disabled:false}
			]
		};
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, appMenuModel);
		
		this.ringerSwitchAttributes = {
			modelProperty: "ringerSwitchObeyed",
			trueLabel: $L('YES'),
			falseLabel: $L('NO')
		};
		
		this.controller.setupWidget('ringerSwitchToggle', this.ringerSwitchAttributes, this.settingsModel);
		this.controller.listen('ringerSwitchToggle', Mojo.Event.propertyChange, this.onRingerSwitchSet);
		
		this.controller.get('theme_set').observe(Mojo.Event.tap, this.onThemeSelect);
		
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keypress, this.onKeyPress);

	},
	
	aboutToActivate: function() {
		// load theme data into UI
		this.themeUpdate()
	},
	
	cleanup: function() {
		//this.controller.document.body.addClassName("clockview");
		
	},
	
	onThemeSelect: function() {
		this.controller.stageController.pushScene("themes", this.themes, this.onThemeChange);	
	},
	
	// VERY IMPORTANT: UI for ringer switch has OPPOSITE MEANING of variable
	// it's reversed back here when saving
	onRingerSwitchSet: function() {
		this.settings.ringerSwitchObeyedSet(!(this.settingsModel.ringerSwitchObeyed));
	},
	
	themeUpdate: function() {
		var theme = this.themes.getCurrentTheme();
		this.controller.get('theme_name').textContent = this.themes.getNicename(theme.name);
		this.controller.get('theme_icon').className = "icon right " + theme.name;
	},
	
	onKeyPress: function(e) {
		this.easterString += String.fromCharCode(e.originalEvent.charCode);
		Mojo.Log.info(this.easterString);
		if (this.easterString.length === 8) {
			if (this.easterString === "sixtyten") {
				Mojo.Log.info("toggling date picker interval");
				if (this.settings.timePickerIntervalGet() !== 5) {
					this.settings.timePickerIntervalSet(5);
				} else {
					this.settings.timePickerIntervalSet(1);
				}
				
				this.appController.playSoundNotification("alerts", "/usr/palm/sounds/notification.wav");
				this.easterString = "";
				
			} else {
				this.easterString = "";
			}
		} else if (this.easterString.length === 4) {
			if (this.easterString === "4231") {
				Mojo.Log.info("toggling dashboard");
				this.settings.dashboardHideSet(!(this.settings.dashboardHideGet()));
				this.easterString = "";
				this.appController.playSoundNotification("alerts", "/usr/palm/sounds/notification.wav");
			}
		}
	}
});