/*
 * com.palm.app.photos, version 1.0
 * Copyright 2009 Palm, Inc.  All rights reserved.  (c) Palm, Inc.
 */

Photos.Scene.Base = Class.create(Class.create(), Photos.Common.AutoListener, {
	initialize: function(params) {
		Mojo.Menu.prefsItem.label = $L("Preferences & Accounts");
		this.enablePrefs = true;
		this.params = params || {};
		this.params.applicationRoot = this.params.applicationRoot || "";
		this.templatePrefix = this.params.applicationRoot ?
			(this.params.applicationRoot + 'app/views/') : "";
		this.sceneModel = this.params.model || new Photos.Model.Scene();
	},

	setup: function() {
		this.sceneModel.setController(this.controller);

		// disable accounts for cross-loaded scene pushes.
		if (this.params.applicationRoot) {
			this.disablePrefsMenu();
		}
	},

	cleanup: function() {
		this.removeAutoListeners();
	},

	aboutToActivate: function(callback) {
		this.aboutToActivateCallback = callback;
		this.sceneModel.aboutToActivate(this.controller.window);
	},

	activate: function() {
		if (this.aboutToActivateCallback) {
			delete this.aboutToActivateCallback;
		}
	},

	pushWithTemplate: function(event, name, params) {
		if (this.params.onSelect && event && event.item) {
			this.params.onSelect(event.item);
			return;
		}
		var pushParams;
		var sceneParams = params || {};
		if (typeof name === "object") {
			pushParams = name;
		} else if (typeof name === "string") {
			pushParams = {name: name};
		} else {
			throw "pushWithTemplate has invalid params.";
		}
		pushParams.assistantConstructor =
			Photos.Scene[pushParams.name.capitalize()];
		pushParams.sceneTemplate =
			this.templatePrefix + pushParams.name +
					'/' + pushParams.name + '-scene';
		sceneParams.applicationRoot = this.params.applicationRoot;
		this.controller.stageController.pushScene(
						pushParams, sceneParams);
	},

	handleCommand: function(event) {
		// a command is sent out to check for enable/disable
		if (event.type == Mojo.Event.commandEnable &&
			((this.enablePrefs &&
					event.command == Mojo.Menu.prefsCmd) ||
			event.command == Mojo.Menu.helpCmd)) {
			event.stopPropagation();
			return;
		}

		// only deal with menu-command events
		if (event.type != Mojo.Event.command) {
			return;
		}

		if (event.command == Mojo.Menu.prefsCmd) {
			this.pushWithTemplate(null, 'accounts');
		} else if (event.command == Mojo.Menu.helpCmd) {
			var helpUrl = 'http://help.palm.com/photos/index.html';
			this.controller.serviceRequest(
				'palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {
						id: 'com.palm.app.help',
						params: {target: helpUrl}
					}
			});
		}
	},

	disablePrefsMenu: function() {
		this.enablePrefs = false;
	},

	ready: function() {
		if (this.aboutToActivateCallback) {
			this.aboutToActivateCallback();
			delete this.aboutToActivateCallback;
		}
	}
});

