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

var PrefsAccountTypeListAssistant = Class.create({
   initialize: function(params){
        this.appController = Mojo.Controller.getAppController();
        this.Messaging = this.appController.Messaging;      
        this.params = params || {}
		this.handleAccountTypesListTap = this.handleAccountTypesListTap.bindAsEventListener(this);
    },
	
    setup: function(){
        this.accountTypesModel = {items: []};
        this.controller.setupWidget('accountTypes', {itemTemplate:'prefsAccountTypeList/accountTypeRow'}, this.accountTypesModel);
        
		this.renderAccountTypesList(this.Messaging.SupportedIMAccountTypes.data);
		MessagingMojoService.getAccountTypes(this.controller, this.renderAccountTypesList.bind(this));
		
        this.controller.listen('accountTypes', Mojo.Event.listTap, this.handleAccountTypesListTap, false);     
    },
	
	handleAccountTypesListTap: function(e) {
        MessagingUtils.simpleListClick(this.controller.get(e.originalEvent.target),"palm-button",function(targetRow){
            var newParams = {
	            accountTypeId: targetRow.getAttribute('accountTypeId'),
	            domain: targetRow.getAttribute('domain'),
				serviceIcon: targetRow.getAttribute('serviceIcon')				
			};
			Object.extend(newParams,this.params);

            this.controller.stageController.pushScene('prefsSetupAccount', newParams);            
          }.bind(this),false);    		
	},
	
    activate: function(){
    },
	
    deactivate: function(){
    },
	
	cleanup: function() {
		this.controller.stopListening('accountTypes', Mojo.Event.listTap, this.handleAccountTypesListTap, false);  
	},
	
	renderAccountTypesList: function(data){
		this.accountTypesModel.items = [];
		for (var i = 0; i < data.list.length; i++) {
			var item = data.list[i];
			if (item.disabled !== true) {
				var transport = this.Messaging.transports.getTransportByName(item.domain);
				item.accountDisplayName = transport.displayName;
				item.serviceIcon = transport.largeIcon;
				this.accountTypesModel.items.push(item);
			}
		}
		this.controller.modelChanged(this.accountTypesModel, this);
	}
});