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

var RecoverPasswordAssistant = Class.create({
	initialize: function(){
		$('resendEmail').observe(Mojo.Event.tap,this.resendEmail.bindAsEventListener(this));
		$('next').observe(Mojo.Event.tap,this.showIntro.bindAsEventListener(this));
		FirstUseUtil.addDashboardSmall($('target'));
		saveCurrentState("recover-password");
	},
	
	setup: function() {
		var data = {
			email: Account.email
		};
		var message = $L("To recover your password, you must reset your password at the link emailed to #{email}.").interpolate(data);
		$('recoverInfo').innerHTML="<p>"+$L(message)+"</p>";
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));		
	},
	
	handleCommand: function (event) {
		
	},
	
	activate: function() {
		
	},
	
	resendEmail: function() {
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": this.requestPasswordResetEmail.bind(this)
			});
			return;
		}
		this.requestPasswordResetEmail();	
	},
	
	requestPasswordResetEmail: function () {
		AppAssistant.accountService.requestPasswordResetEmail(Account.email, 
						this.resetEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);
	},
	
	resetEmailSuccess: function(response) {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('resetemail');
	},
	
	resetEmailFailure: function(response) {
		Mojo.Log.error("reset email failure = %o"+$H(response));
	},
	
	showIntro: function() {
		if(Account.isInvalidToken == true) {
			Mojo.Controller.stageController.pushScene('invalid-token');
			return;
		}
		Mojo.Controller.stageController.pushScene('intro');
	}
	
});	
