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

var ChoosetransportAssistant = Class.create({
	initialize: function(popto, firstUseScene, headerText, doneText, hideFacebook){
		this.headerText = headerText || $L("Welcome to contacts!");
		this.doneText = doneText || $L("Go to my contact list");
		this.accountsAttributes ={
			itemTemplate : "choosetransport/accountTypeRow"
		}
		this.accountsModel = {
						items : []
		}
		this.popto = popto;
		this.firstUseScene = firstUseScene;
		this.hideFacebook = hideFacebook;
	},
	setup: function(){
		this.controller.get("nonFirstUseStuff").show();
		//this.controller.get("choosetransportHeader").update(this.headerText);
		// this.controller.get("DoneButton").update(this.doneText)
		var that = this;
		this.controller.serviceRequest('palm://com.palm.accounts/', {
			method: 'getAllAccountTypesForService',
			parameters: { service: 'CONTACTS' },
			onSuccess: this.renderExistingAccountTypes.bind(this)
		});
		this.controller.setupWidget("ExistingAccountTypes", this.accountsAttributes, this.accountsModel);
		var popto = this.popto;
		this.controller.get("ExistingAccountTypes").observe(Mojo.Event.listTap, this.handleAccountsListTap.bind(this));
		//this.controller.get("DoneButton").observe(Mojo.Event.tap, function(e) {
			//this.controller.stageController.popScenesTo(popto);
		//})
	},
	
	handleAccountsListTap: function(event) {
		if (event.item.domain == "eas") {
			var args = {
				appId: "com.palm.app.email",
				name: "wizard"
			};
      
			var params = {
				edit: false,
				id: undefined,
				type: "contacts", 
				title: $L("Add An Account"), 
				protocol: "EAS"
			};
			
			this.controller.stageController.pushScene(args, params);
		} else if (event.item.domain === "att") {
			var loginParams = {
				acctId: 0,
				acctLogin: undefined,
				acctTypeId: event.item.id,
				acctDomain: event.item.domain,
				acctName: event.item.name,
				iconClass: event.item.iconClass,
				numberOfTimesToPopScene: 2
			}
			this.controller.stageController.pushScene({
				appId: "com.palm.app.attsyncpanel",
				name: "validation"
			}, loginParams);
		} else {
			var loginParams = {
				acctId: 0,
				acctLogin: undefined,
				acctTypeId: event.item.id,
				acctDomain: event.item.domain,
				acctName: event.item.name,
				iconClass: event.item.iconClass,
				numberOfTimesToPopScene: 2
			};
			
			this.controller.stageController.pushScene("accountlogin", loginParams);
		}
	},
	
	aboutToActivate:function(aboutToActivateCallback){
		if(this.accountsReturned){
			aboutToActivateCallback();
		} else {
			this.aboutToActivateCallback = aboutToActivateCallback;
		}
	},
	
	
	activate:function(results){
		if (results !== undefined) {
			if (results.action === "create") {
				this.controller.stageController.popScene();
				return;
			}
		}
	},
	
	cleanup: function(){
		if(this.firstUseScene){
			AppAssistant.contactsService.setFirstUseToFalse(this.controller);
		}
	},
	
	renderExistingAccountTypes: function(resp){
		// Cache accounts list locally to minimize property lookups:
		var list = resp.list;

		if (this.hideFacebook) {
			list = list.findAll(function(a) {
				return (a.domain !== "facebook" && a.domain !== "linkedin" && a.domain !== "yahoo")
			});
		}
		
		//localize each name
		list.each(function(elem) {
			elem.name = $L(elem.name);
		});

		// Sort accounts list in ascending alphabetical order if it contains 2 or more items:
		if (list.length > 1) {
			list.sort(function(a, b) {
				return a.name.localeCompare(b.name);
			});
		}

		// Store the locally cached accounts list in the Accounts Model:
		this.accountsModel.items = list;

		this.controller.modelChanged(this.accountsModel);
		if (this.aboutToActivateCallback) {
			this.aboutToActivateCallback();
			delete this.aboutToActivateCallback;
		} else {
			this.accountsReturned = true;
		}
	}
})
