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

*/


AppAssistant = Class.create({
	/**
	 * Constructor: Defined in AppAssistant I/F.
	 * 
	 * @param {Object} controller  Application controller.
	 */
	initialize: function(controller){
		llog ("AppAssistant::initialize");

		AppAssistant._camera = null;
		this.appController = controller;
		
		AppAssistant.libraries = MojoLoader.require(
			{ name: "metascene.videos", version: "1.0" },
			{ name: "mediaextension", version: "1.0"});
			
		if (!window.Media){
			Media = {};
		}	
		Media.MediaCapture = AppAssistant.libraries.mediaextension.MediaCapture;

		
		llog("AppAssistant::initialize() completed");
	},
	
	handleLaunch: function(params){
		llog("Handle launch, with params: "+Object.toJSON(params));

		if (params.launchedAtBoot) {
			llog("In boot.  Don't make a scene.");
		}
		else{
			var stageController = this.appController.getStageController(AppAssistant.STAGE_NAME_CAMERA);
			
			if (!!stageController){
				// Stage already exists, just bring to fore, and pop to capture scene.
				var allScenes = stageController.getScenes();
				if (!allScenes || !allScenes.length){
					// No scenes
					stageController.pushScene(AppAssistant.SCENE_NAME_CAPTURE, {});
				}
				else if ( allScenes.length > 1) {
					// There is one or more scenes pushed on top of the capture scene, remove!
					stageController.popScenesTo(allScenes[0]);
				}


				stageController.activate();				
				
			}
			else {
				this.openChildWindow(this.appController);
			}
			
		}
	},

	
	/**
	 * Called from handleLaunch if we're a lightweight stage.
	 * 
	 * @param {Object} appController
	 */
	openChildWindow: function(appController) {
		llog("AppAssistant::openChildWindow");

		var f = function(stageController){
			stageController.pushScene(AppAssistant.SCENE_NAME_CAPTURE, {});
			llog("Pushed "+AppAssistant.SCENE_NAME_CAPTURE+" scene"); 	
		}.bind(this);

		appController.createStageWithCallback({name: AppAssistant.STAGE_NAME_CAMERA, lightweight: true}, f);		
	},
	
	/**
	 * Defined in AppAssistant I/F
	 */
	setup: function(){
		llog("AppAssistant::setup");
	}	

});


AppAssistant.EFS = {
	exe:   '/var/luna/data/extractfs',    // fuse application
	reviewImage: ':0:0:160:240:3',  // :thumb-offset:thumbbytes:width:height:mode
	savedImage: ':0:0:70:70:4',
	savedThumb: ':70:70:4',
	scrimImage: ':0:0:480:480:4',   // Use with plain JPG filename to get square image for scrim - with JPEG
	scrimVideo: ':480:480:4'        // Use with MP4 thumbnail offset URL to get square image for scrim - with MP4
	
};

AppAssistant.SCENE_NAME_CAPTURE = "capture";
AppAssistant.STAGE_NAME_CAMERA = 'camera';
AppAssistant.photoRollPhotoLoader = null;
AppAssistant.photoRollVideoLoader = null;

