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

var PoundTTYAssistant = Class.create({
	
	onOffToggleOpt: {
        modelProperty: 'currentTTYMode',
        enableProp: 'enabled',        
    },
	
	ttyChoices: [
			{label: "HCO", value: "hco"},
			{label: "VCO", value: "vco"},
			{label: "FULL", value: "full"},
			{label: "OFF", value: "off"}
		],
    	
	//Model for tty Mode Selection List
	TTYModel: {currentTTYMode: "off"},
	
	initialize: function(sceneAssistant) {
		this.sceneAssistant = sceneAssistant;
		this.controller = this.sceneAssistant.controller;
		// FIXME
		TelephonyCommands.ttyQuery(this.onTtyQueryDone.bind(this));		
	},
	
	onTtyQueryDone: function(success, status, mode) {
		if (success) {
			this.TTYModel.currentTTYMode = mode || "off";
		} else {			
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: $L(status) || $L("Error occurred."),
		    preventCancel:true,
		    choices:[
	        {label: $L('Done'), value:true, type:'dismiss'}    
		    ]
		  	});	  
		}
		this.controller.modelChanged(this.TTYModel,this);
	},
	
	setup: function(parent){
		this.parent = parent;
		
		this.controller.setupWidget('ttyMode', {
			label: "TTY Mode",
            choices: this.ttyChoices,
            modelProperty: 'currentTTYMode'
        }, this.TTYModel);
		Mojo.Event.listen(this.controller.get('ttyMode'), 'mojo-property-change', this.selectorChanged.bindAsEventListener(this), false);
				
		this.controller.get('saveButton').observe(Mojo.Event.tap,this.saveActivated.bind(this));
		this.controller.get('closeButton').observe(Mojo.Event.tap,this.doneActivated.bind(this));
	},
	
	selectorChanged: function(event) {
		
		if(event.value)
			this.saveSelection = event.value;
		else
			this.saveSelection = 'off';
		
	},
	
	
	saveActivated: function() {
		TelephonyCommands.ttySet(this.saveSelection, this.onSaveDone.bind(this));		
	},
	
	onSaveDone: function(success, status) {
		if (success) {
			QDLogger.log( "PoundTTYAssistant::onSaveDone","saved");
			var appController = Mojo.Controller.getAppController();
			//Mojo.Controller.stopEditing();
			if(this.saveSelection == 'full')
				appController.showBanner($L('TTY is enabled'), {banner: $L('TTY is enabled')});
			else if(this.saveSelection == 'off')
				appController.showBanner($L('TTY is disabled'), {banner: $L('TTY is disabled')});
		} else {
			QDLogger.log( "PoundTTYAssistant::onSaveDone Error",status);
		}
		this.doneActivated();
	},
	
	doneActivated: function() {
		this.parent.mojo.close();
	},
	
});
