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

var RecoverEmailAssistant = Class.create({
	
	initialize: function(sceneAssistant, myProfile) {
		this.myProfile = myProfile;
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;
	},
	
	setup : function(widget) {
		this.widget = widget;
		
		var data = {
			email: this.myProfile.email
		};
		var message = $L("To recover your password, you must reset your password at the link emailed to #{email}.").interpolate(data);
		$('recoverInfo').innerHTML=$L(message);
		
		$('resendPasswordEmail').addEventListener(Mojo.Event.tap, this.resendPasswordEmail.bindAsEventListener(this));
		$('next').addEventListener(Mojo.Event.tap, this.showLogin.bindAsEventListener(this));
	},
		
	resendPasswordEmail: function(event) {
		this.resetRequest = AppAssistant.accountService.requestPasswordResetEmail(this.myProfile.email, 
			this.sendEmailSuccess.bind(this), this.resetEmailFailure.bind(this), this.controller);
	},
	
	sendEmailSuccess:function(response) {
		this.controller.showBanner({
				icon: "images/tap-info.png",
				messageText: ProfileUtil.label_email_sent
				}, 
				{}, 
				'pubsubbanner');	
	},
	
	resetEmailFailure: function(response) {
		//TODO What to do if we can't send email
		this.widget.mojo.close();
		Mojo.Controller.errorDialog($L("We could not send an email to reset your password. Visit palm.com/support for more help."));
	},
	
	showLogin: function(event) {
		this.widget.mojo.close();
		
		if (isConnectionAvailable === false) {
			ProfileUtil.showConnectionError(this.controller);
		}
		else {
			this.controller.showDialog({
				template: 'account/password-dialog',
				assistant: new PasswordAssistant(this)
			});
		}	
	}
	
});
