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

var FirstuseAssistant = Class.create({
	
	appMenuModel: { visible:true, 
					label:$L('Calendar'), 
					items: [ Mojo.Menu.editItem,
							{label:$L('Preferences'), command:Mojo.Menu.prefsCmd, disabled: true},
							{label:$L('Help'), command:Mojo.Menu.helpCmd, disabled:false}]
			},
	initialize: function(showNoThanks){
		this.showNoThanks = showNoThanks;
  },
	
	setup: function() {
		if (this.showNoThanks == false) {
			this.controller.get('f_nothanks_btn').hide();
		}
		this.acctTypesModel.items = getFirstUseManager().getCalendarAcctTypes();
		this.controller.setupWidget('f_acctstypes_picker', this.acctTypesAttrs, this.acctTypesModel);
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, this.appMenuModel);
	},
	
	handleAcctsTypeListTap: function(event) {
		// If the account type is EAS, then we sublaunch Email to their create account scene
		if (event.item.domain == "eas") {
			var args = {
         appId: "com.palm.app.email",
         name: "wizard"
     	};
			
			var params = {
				edit: false,
				id: undefined,
				type: "calendar", 
				title: $L("Add An Account"), 
				protocol: "EAS"
			};
			
			this.controller.stageController.pushScene(args, params);
		} else {
			this.controller.stageController.pushScene("accountlogin", 
																								0 /*acctId*/,
																								undefined /*acctLogin*/,
																								event.item.id, 
																								event.item.serviceIcon, 
																								event.item.name, 
																								"firstuse-with-accts");
		}
	},
	handleCommand:function(event){
		if(event.command == Mojo.Menu.helpCmd){
				getAppManagerService().launchHelp(this.controller);
			}
	},
	activate: function(results) {
		if (results !== undefined) {
			if (results.action == "create") {
				// If the user successfully created a new EAS account in Email,
				// then we show the other First Use scene that lists the accounts
				// the user is logged in
				this.controller.stageController.swapScene("firstuse-with-accts");
				return;
			}
		}
		
		this.noThanksBtnTapHandler = this.goToDayView.bindAsEventListener(this);
		this.controller.get('f_nothanks_btn').observe(Mojo.Event.tap, this.noThanksBtnTapHandler);
		this.acctsTypeListTapHandler = this.handleAcctsTypeListTap.bindAsEventListener(this);
		this.controller.get("f_acctstypes_picker").observe(Mojo.Event.listTap, this.acctsTypeListTapHandler);
	},
	
	deactivate: function() {
		this.controller.get('f_nothanks_btn').stopObserving(Mojo.Event.tap, this.noThanksBtnTapHandler);
		this.controller.get("f_acctstypes_picker").stopObserving(Mojo.Event.listTap, this.acctsTypeListTapHandler);
	},
	
	// *** EVENT HANDLERS ***
	
	goToDayView: function(event) {
		// The user is done setting up for first use... 
		finishedFirstUse();
		
		// Go to the Day View
		var sController = this.controller.stageController;
		sController.popScene();
		sController.pushScene('day');
	},
	
	// *** LIST ATTRS & MODELS ***
	acctTypesAttrs : {
		itemTemplate: 'firstuse/accountTypeRow',
	},
	
	acctTypesModel : {
		items: [],
	},
});