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

function AppAssistant(appController) {
  this.appController = appController; 
	if (PalmSystem.version.match("desktop")){ //see if on desktop
		isConnectionAvailable = true;//undefined;
	}
  	else{
		isConnectionAvailable = undefined;
		Mojo.Log.info("-------------------- Get connection status ---------------");
  		AppAssistant.accountService.getConnectionStatus(readConnectionStatus, errorConnectionStatus);
	}
}


function StageAssistant(stageController) {
  var queryParams = document.URL.toQueryParams();
  this.stageController = stageController;
  var launchParams;
  try {
      Mojo.log("Backup App Launch Params (" + typeof(PalmSystem.launchParams) + "): " + PalmSystem.launchParams);
	  // evalJSON - sending true as argument in order to sanitize the launchParams var as per security guidelines
      launchParams = PalmSystem.launchParams.evalJSON(true);
	  Mojo.log("notify =" + launchParams.notify);
  } 
  catch (err) {
      Mojo.log("Backup App: No launch params" + err);
  }
  var sceneName = "backup";
  this.stageController.pushScene(sceneName,launchParams);
}

AppAssistant.prototype.handleRelaunch = function(){
	var stageMgr = Mojo.Controller.getAppController().getStageMgr();
	if (stageMgr.stageExists("backup")) {
	 	this.appController.getStageMgr().focusStage("backup");
	}
	else {
		var f = function(stageController){
			this.stageController = stageController;
			stageController.pushScene('backup');
		}.bind(this);
		Mojo.Controller.getAppController().createStageWithCallback("backup", f);
	}
}

function readConnectionStatus (response) {
	Mojo.Log.info("--------------- read connection status -----------%o", $H(response));
	if(response.isInternetConnectionAvailable != undefined) {
		isConnectionAvailable = response.isInternetConnectionAvailable;
	}
}

function errorConnectionStatus (response) {
	Mojo.Log.info("--------------- error connection status -----------%o", $H(response));
	isConnectionAvailable = false;
}

var serviceName = 'palm://com.palm.backup';
AppAssistant.backupService = new BackupService();
AppAssistant.accountService = new NovaAccountService();