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

var AppAssistant = Class.create({
	baseScene: {
		name: "home",
		assistantConstructor: Photos.Scene.Home
	},

	initialize: function() {
	},

	handleLaunch: function(params) {
		Mojo.Log.info("Handle launch", Object.toJSON(params));
		if (!AppAssistant.stageController) {
			Mojo.Log.info("Deferring params until stage is made.");
			AppAssistant.deferredParams = params || {};
			return;
		}

		if (params) {
			AppAssistant.prototype.dealWithParams(params, true);
			return;
		}

		AppAssistant.stageController.activate();
	},
	
	dealWithDeferredParams: function(stageController) {
		Mojo.Log.info("Using the deferred params.");
		AppAssistant.stageController = stageController;
		AppAssistant.prototype.dealWithParams(
						AppAssistant.deferredParams);
	},

	dealWithParams: function(params, deferActivate) {
		Mojo.Log.info("Dealing with params.");
		if (!AppAssistant.stageController.topScene()) {
			AppAssistant.stageController.pushScene(this.baseScene);
		} else {
			AppAssistant.stageController.popScenesTo(
							this.baseScene.name);
		}

		var photoRoll = $L('Photo roll');
		if (undefined !== params.imageIndex) {
			if (!params.albumID) {
				params.albumID = -1;
			}
			AppAssistant.stageController.pushScene('thumbs', {
				model: new Photos.Model.LocalThumbs(
						params.albumID, photoRoll)
			});
			AppAssistant.stageController.pushScene('fullscreen', {
				deferActivate: deferActivate,
				model: new Photos.Model.LocalFullscreen(
					params.imageIndex,
					params.albumID,
					photoRoll)
			});
		} else if (params.albumID && params.albumName) {
			AppAssistant.stageController.pushScene('thumbs', {
				deferActivate: deferActivate,
				model: new Photos.Model.LocalThumbs(
					params.albumID, params.albumName)
			});
		} else if (params.imageUrl && params.imageName) {
			AppAssistant.stageController.pushScene('fullscreen', {
				deferActivate: deferActivate,
				model: new Photos.Model.SingleFullscreen(
					params.imageUrl,
					params.imageThumbUrl,
					params.imageName)
			});
		}
	}
});

var StageAssistant = Class.create({
	initialize: function(stageController) {
		Mojo.Log.info("Creating default stage.");
		AppAssistant.prototype.dealWithDeferredParams(stageController);
	}
});

