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

var AppManagerMojoService = Class.create( {
  initialize: function() {
  },
  
	launchAddToContact : function(sceneController, sendemail,name){
    var params = {launchType:"addToContacts",
									points:[{type:"email",value:sendemail}],
									name:name};
	
		return sceneController.serviceRequest(AppManagerMojoService.identifier,{
      method: 'open',
      parameters : {target : "opencontact://" + Object.toJSON(params)}
    });    
  },
  
	makeRecipientsArray: function(recipients) {
		var rArray = [];
		recipients.each(function(recipient){
			var r = {};
			r.type = 'email';		// Do NOT Localize
			r.role = 1;					// TO:
			r.value = recipient.email;
			// The participants scene handles looking up contacts
			// associated to recipients, so recipient.name is usually
			// set in that case.  Scheduled reminders do not look up
			// associated contacts so let's just use the email for the
			// display name
			if (recipient.name == undefined) {
				r.contactDisplay = recipient.email;
			} else {
				r.contactDisplay = recipient.name;
			}
			// The contactid may not always be available.  The participants
			// scene handles looking up contacts associated to recipients.
			// Scheduled reminders however do not.
			r.contactId = recipient.contactid;
			rArray.push(r);
		});
		return rArray;
	},
	
	launchComposeMessage : function(sceneController, subject, recipients, message, accountId){ 	
		var params = {};
		params.summary = subject;
		params.recipients = this.makeRecipientsArray(recipients);
		params.text = message;	
		params.account = accountId + "";	 
		//Mojo.Log.info("launchComposeMessage: %s", Object.toJSON(params));
    return sceneController.serviceRequest(AppManagerMojoService.identifier, {
        method: 'open',
        parameters: {id:'com.palm.app.email', params:params}
    });			
  },
	
  launchMaps:function(sceneController,address,sAddr){
	// fix NOV-81255 - getting map/direction info on an address
	// should launch app user has chosen in Launcher Preferences.
	//
	// sAddr = true -- we want to get directions to the address passed in
	// sAddr = false -- we just want to map the address passed in
	
		var mapTarget = "maploc:";
		if (sAddr) {
			mapTarget = "mapto:";
		}

		// params is what we pass into the default navigation app 
		// this is set in Launcher-->Default Applications
		var params = {target: mapTarget + address.gsub("\n", " ").gsub("\r", " ")};
		this.launchDefault(sceneController,params);
  },
  
  launchHelp:function(sceneController){
  	/*	var openParams = {  scene: 'page', url: "http://help.palm.com/calendar/index.html"};
		return new Mojo.Service.Request('palm://com.palm.applicationManager', {
        				method: 'open',
		      			parameters: { id: 'com.palm.app.browser',
						              params: openParams }
   			 }); */
	sceneController.serviceRequest("palm://com.palm.applicationManager", 
	    { method: "open",
 		  parameters: {
      	  id: 'com.palm.app.help',
      	  params: {target: "http://help.palm.com/calendar/index.html"}
  		}
	});
  },

  // Get the app id of the default
  // application for this mime type or url
  getActiveHandlerForDefault: function(result) {
   	if (result.returnValue) {
		
	// Return the appId of the default handler 
	// from the Mime systems response
		if (result.redirectHandlers) {
			return result.redirectHandlers.activeHandler.appId;
		} else if (result.resourceHandlers) {
			return result.resourceHandlers.activeHandler.appId;
		}
						
	} else {
		// The call to the mime system
		// failed because of an error in
		// the system. 
		// in this case just return default map application
		return defaultMapsApplication;
	}
  },

  // Open the default application with
  // the data specified
  launchDefault: function(sceneController,params){
	this.params = params;
		
	// Get the default application from the
	// mime system.
	sceneController.serviceRequest("palm://com.palm.applicationManager", {
		method: "listAllHandlersForUrl",
		parameters: {url: "mapto:"},
		onSuccess: function(e){
			var defaultHandler = this.getActiveHandlerForDefault(e);
			this.launchApp(sceneController,defaultHandler, this.params);
		}.bind(this),
		onFailure: function(e){
			this.launchApp(sceneController,defaultMapsApplication, this.params);
		}.bind(this)
	});
  },
	
  // Open the app with the appid
  // and data that was specified
  launchApp: function(sceneController,appid, data){
  	var params = data;
	
	sceneController.serviceRequest("palm://com.palm.applicationManager", {
		method: 'open',
		parameters: {id: appid, params: params}
	});	
  }
	
 });
 
 
 
 AppManagerMojoService.identifier = 'palm://com.palm.applicationManager';
 defaultMapsApplication = 'com.palm.app.maps';