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

var InvalidTokenAssistant = Class.create({
	
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	setup: function(){
		Mojo.Log.info("Is set up called again?");
						
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		saveCurrentState("invalid-token");
		
		this.deviceMemoryModel = {buttonLabel: $L("Erase all data"), buttonClass: "negative"};
        this.controller.setupWidget('device-erase-button', {}, this.deviceMemoryModel);
		Mojo.Event.listen($('device-erase-button'),'mojo-tap', this.showDialog.bind(this));
		
		this.restartMemoryModel = {buttonLabel: $L("Just restart")};
        this.controller.setupWidget('restart-button', {}, this.restartMemoryModel);
		Mojo.Event.listen($('restart-button'),'mojo-tap', this.restart.bind(this, true));
		
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	},
	
	restart: function(){
		Mojo.Log.info('rebooting...');
			
		this.restartMemoryModel.disabled = true;
		this.controller.modelChanged(this.restartMemoryModel);
		this.controller.get('scrim').show();
		this.deviceInfoRequest = AppAssistant.accountService.reboot(this.controller);
    },
	
	showDialog: function(full, event) {
		
		var infoMessage = $L("If you start over, all your data will be erased and your phone will be restored to its factory default. You can copy data stored on the USB drive to your desktop computer by plugging in a USB cable now."); 	
		
		var dialog = this.controller.showAlertDialog({
			title: $L("Erase all data?"),
		    onChoose: this.eraseHandler.bind(this),
		    message: infoMessage,
		    choices:[
		        {label:$L("Back"), value:"keep", type: 'dismiss'},
				{label:$L("Erase All Data"), value:"erase", type:'negative'}
		    ]
		  });
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
    },
	
	eraseHandler: function(value) {		
		if (value == "erase") {
			this.deviceMemoryModel.disabled = true;
			this.controller.modelChanged(this.deviceMemoryModel);

		   	Mojo.Log.info('beginning full erase');
			this.controller.get('scrim').show();
			this.deviceInfoRequest = AppAssistant.accountService.eraseAll(this.controller);
		}
    }
	
	
	
});

