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

var AddToLauncherAssistant = Class.create({
	initialize: function(controller, callback, model) {
		this.controller = controller;
		this.callback = callback;
		this.model = model;
		this.firstName = model.firstName;
		this.lastName = model.lastName;
	},
	
	setup: function(widget) {
		this.widget = widget;
		this.updateNameFn = this.updateName.bind(this);
		this.firstNameDiv = this.controller.get("launcher-first-name");
		this.lastNameDiv = this.controller.get("launcher-last-name");
		this.nameModel = {
			firstName: this.firstName,
			lastName: this.lastName
		}
		this.controller.setupWidget("add-to-launcher-firstname", {
			modelProperty: "firstName",
			textCase: Mojo.Widget.steModeTitleCase,
			autoReplace: false
		}, this.nameModel);
		this.controller.setupWidget("add-to-launcher-lastname", {
			modelProperty: "lastName",
			textCase: Mojo.Widget.steModeTitleCase,
			autoReplace: false
		}, this.nameModel);
		
		this.controller.get('do-add-to-launcher').observe(Mojo.Event.tap, this.finish.bind(this));
		this.controller.get('add-to-launcher-firstname').observe('keyup', this.updateNameFn.bindAsEventListener(this, true));
		this.controller.get('add-to-launcher-lastname').observe('keyup', this.updateNameFn.bindAsEventListener(this, false));
		this.controller.get('cancel-add-to-launcher').observe(Mojo.Event.tap, this.widget.mojo.close.bind(this.widget.palm));
	},
	
	finish: function() {
		this.callback(this.firstName, this.lastName);
		this.widget.mojo.close();
	},
	
	updateName: function(e, isFirst) {
		if (isFirst) {
			this.firstNameDiv.innerHTML = e.target.value;
			this.firstName = e.target.value;
		} else {
			this.lastNameDiv.innerHTML = e.target.value;
			this.lastName = e.target.value;
		}
	}
});
