/* Copyright 2009 Palm, Inc.  All rights reserved. */
/*global Mojo, Class, $L */
/*jslint rhino:true, undef:true */


function AppAssistant(appController)
{
}

AppAssistant.prototype.handleLaunch = function(params) {
	
	try {
		var launchParams = {};
	
		if (Object.isString(params)) {
			if (params.isJSON()) {
				launchParams = params.evalJSON();
			}
		}
		else {
			launchParams = params;
		}
	
		// If we're headless, then we need to manually create a card stage.
		if (Mojo.Controller.appInfo.noWindow) {
			var f = function(stageController) {

				if (launchParams.target) {
					// Sanitize the target property
					launchParams.target = launchParams.target.stripScripts().stripTags();
				}

				var sceneParams = {
					launchParams: launchParams
				};

				if (launchParams.target) {
					stageController.pushScene({name:'document', disableSceneScroller:true}, sceneParams);
				}
				else {
					stageController.pushScene({name:'recent'}, sceneParams);
				}
			};
			var stageName = "docviewer-" + Date.now();
			this.controller.createStageWithCallback({name: stageName, lightweight: true}, f);
		} else if(params.banner) {
			Mojo.Log.warn("Notifications not yet implemented.");
		}
	} 
	catch (e) {
		Mojo.Log.logException(e, "AppAssistant#handleLaunch");
	}
};

AppAssistant.prototype.GetFileExtension = function(fileinput) {

	if (fileinput.empty()) {
		return '';
	}

	var dot = fileinput.lastIndexOf('.');
	if (dot === -1) {
		return '';
	}

	var extension = fileinput.substr(dot + 1, fileinput.length);
	if (extension.length === 4) {
		extension = extension.substr(0, 3);
	}

	return extension;
};

AppAssistant.prototype.checkFileType = function(fileinput) {

	var extension = this.GetFileExtension(fileinput);

	return this.fileExtensions.indexOf(extension.toLowerCase()) != -1;
};

AppAssistant.prototype.cleanup = function() {
};

AppAssistant.prototype.fileExtensions = ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'txt'];
