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

Photos.Scene.Selectaccount = Class.create(Photos.Scene.Base, {
	setup: function($super) {
		$super();

		this.list = this.controller.get('select_account_list');
		this.controller.setupWidget('select_account_list', {
			itemTemplate:
				this.templatePrefix + 'selectaccount/entry',
			swipeToDelete: false,
			reorderable: false
		}, {items: []});

		this.autoListen(this.list, Mojo.Event.listTap,
			this.handleSelectAccount.bind(this));
		this.disablePrefsMenu();
	},

	aboutToActivate: function($super, callback) {
		$super(callback);

		function responseOkay(response) {
			this.list.mojo.noticeUpdatedItems(0, response.list);
			this.list.mojo.setLength(response.count);
			this.ready.bind(this).defer();
		}

		function responseFail(response) {
			Mojo.Log.error("Failed to get accounts for upload!");
			this.list.mojo.setLength(0);
			this.ready.bind(this).defer();
		}

		this.controller.serviceRequest('palm://com.palm.accounts', {
			method: 'getAllAccountTypesForService',
			parameters: {
				subscribe: true,
				service: "PHOTO_UPLOAD"
			},
			onSuccess: responseOkay.bind(this),
			onFailure: responseFail.bind(this)
		});
	},

	handleSelectAccount: function(event) {
		if (!event || !event.item) {
			return;
		}
		this.pushWithTemplate(null, 'accountlogin', {
			model: new Photos.Model.Accountlogin(event.item)
		});
	}
});

