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

var NameAssistant = Class.create({
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
	},
	
	setup: function(){
		
		this.nameAttributes = {
			modelProperty: 'name',
			multiline: false,
			maxLength: 100, 
			focusMode: Mojo.Widget.focusInsertMode,
			changeOnKeyPress: true,
			requiresEnterKey: true,
			textCase: Mojo.Widget.steModeTitleCase,
			autoReplace: false,
		};
		this.nameModel = {
			'name' : Account.name,
			disabled: false
		};

		this.controller.setupWidget('name', this.nameAttributes, this.nameModel);
		this.nameFocusHandler = this.nameHandler.bind(this);
		Mojo.Event.listenForFocusChanges($('name'), this.nameFocusHandler);
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: true,
			buttonLabel : $L('Next'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('submitName', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('submitName'), Mojo.Event.tap, this.submitName.bindAsEventListener(this));
								
		this.toggleDisabled();
		
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
				
		$('name').observe(Mojo.Event.propertyChange, this.nameChanged.bind(this));	
		
		this.nameField = this.controller.get("name");	
		this.keyPressEventHandler =  FirstUseUtil.handleKeyPressEvent.bind(this);
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		
		this.isNameInFocus = false;
		$('name').focus();
	},
	
	nameHandler: function (event) {
		this.isNameInFocus = !this.isNameInFocus;
		this.toggleListener(this.isNameInFocus);
	},
	
	toggleListener: function (flag) {
		if(flag == true) {
			this.controller.stopListening(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		} else {
			this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, this.keyPressEventHandler);
		}
	},
		
	toggleDisabled: function() {
		
       if(this.nameModel.name.length > 0) {
		  	this.buttonModel.disabled = false;
			this.controller.modelChanged(this.buttonModel);
	   } else {
	   		this.buttonModel.disabled = true;
			this.controller.modelChanged(this.buttonModel);
	   }
    },
	
	nameChanged: function(event) {
	 	this.nameModel.name = this.capitalizeName(event.value);
		this.controller.modelChanged(this.nameModel, this);
		this.toggleDisabled();
		// If the name field has focus and Enter is pressed then simulate tapping on "Next"
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			
			if (this.buttonModel.disabled == false) {
				this.nameField.blur();
				this.submitName();
				Event.stop(event);
			} else {
				this.nameField.mojo.focus();
			}
		}
	},
	
	capitalizeName: function (value) {
		return value.replace(/\S+/g, function(value){
        	return value.charAt(0).toUpperCase() + value.substr(1);
    	});
	},
			
	hideErrorMessages: function() {
		$('nameError').hide();
		$('lastNameError').hide();
	},
			
	submitName: function(event){
		this.buttonModel.disabled=true;
		this.controller.modelChanged(this.buttonModel);
		
		this.hideErrorMessages();
		
		var contactData = new Contact();
		contactData.parseName(this.nameModel.name);
		
		Account.name = this.nameModel.name;
		
		Account.firstName = contactData.getFirstName();
		
		var lastName = "";
		
		if(contactData.getMiddleName()) {
			lastName += contactData.getMiddleName();
		}
				
		if(contactData.getLastName()) {
			lastName += " "+contactData.getLastName();
		}
				
		if(contactData.getSuffix()) {
			lastName += " "+contactData.getSuffix();
		}
		
		Account.lastName = lastName.strip();
		Mojo.Log.info("push password scene - ", Account.firstName, ";", Account.lastName);
		
		if (Account.name == "" || Account.name == undefined) {
			$('nameError').show();
			this.controller.get('name').mojo.focus.delay(0);
		} else if (Account.lastName == "" || Account.lastName == undefined) {
			$('lastNameError').show();
			this.controller.get('name').mojo.focus.delay(0);
		}
		
		else {
			Mojo.Controller.stageController.pushScene('password');
		}	
	}
});	

