function AudioHeadsetMicrophoneAssistant()
{
}

AudioHeadsetMicrophoneAssistant.prototype.oldScenario = undefined;
AudioHeadsetMicrophoneAssistant.prototype.currentScenario = "media_headset_mic";
AudioHeadsetMicrophoneAssistant.prototype.playFile = "/usr/palm/applications/com.palm.app.crotest/sounds/LaLaFile.wav";
AudioHeadsetMicrophoneAssistant.prototype.recordFile = "/var/tmp/recordTestFile.wav";
AudioHeadsetMicrophoneAssistant.prototype.errorClass = "Audio Headset Microphone";

AudioHeadsetMicrophoneAssistant.prototype.Initialize = function()
{
	this.controller.get('audioHeadsetMicrophoneRecord').hide();
	this.controller.get('audioHeadsetMicrophonePlay').show();
}

AudioHeadsetMicrophoneAssistant.prototype.handleCommand = function(event) {
	if(event.type == Mojo.Event.command) {
		if (event.command == Mojo.Menu.helpCmd) {
			Event.stop(event);
			var openParams = {
				target: 'http://help.palm.com/device_info/index.html'
			};
			this.controller.serviceRequest('palm://com.palm.applicationManager', {
				method: 'open',
				parameters: {
					id: 'com.palm.app.help',
					params: openParams
				}
			});
		}
	} else if (event.type == Mojo.Event.commandEnable && 
		 (event.command == Mojo.Menu.helpCmd)) {
		event.stopPropagation();
	}
}

AudioHeadsetMicrophoneAssistant.prototype.setup = function()
{
	this.appMenuModel = {
		visible: true,
		items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
	};
	this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);

//	this.controller.get('play').observe(Mojo.Event.tap, this.handlePlay.bindAsEventListener(this));
	this.controller.get('playNo').observe(Mojo.Event.tap, this.handlePlayNo.bindAsEventListener(this));
	this.controller.get('playYes').observe(Mojo.Event.tap, this.handlePlayYes.bindAsEventListener(this));

//	this.controller.get('record').observe(Mojo.Event.tap, this.handleRecord.bindAsEventListener(this));
//	this.controller.get('playRecord').observe(Mojo.Event.tap, this.handlePlayRecord.bindAsEventListener(this));
	this.controller.get('recordNo').observe(Mojo.Event.tap, this.handleRecordNo.bindAsEventListener(this));
	this.controller.get('recordYes').observe(Mojo.Event.tap, this.handleRecordYes.bindAsEventListener(this));

	this.attributes = {
		disabledProperty: 'disabled',
		type: Mojo.Widget.defaultButton
	};

	this.model = {
		buttonLabel: $L("Record"),
		buttonClass: '',
		disabled: false
	};

	this.controller.setupWidget('record', this.attributes, this.model);
	this.controller.listen('record', Mojo.Event.tap, this.handleRecord.bind(this));
	
	this.playattributes = {
		disabledProperty: 'disabled',
		type: Mojo.Widget.defaultButton
	};

	this.playmodel = {
		buttonLabel: $L("Play"),
		buttonClass: '',
		disabled: false
	};

	this.controller.setupWidget('play', this.playattributes, this.playmodel);
	this.controller.listen('play', Mojo.Event.tap, this.handlePlay.bind(this));
	
	this.playRecattributes = {
		disabledProperty: 'disabled',
		type: Mojo.Widget.defaultButton
	};

	this.playRecmodel = {
		buttonLabel: $L("Play"),
		buttonClass: '',
		disabled: false
	};

	this.controller.setupWidget('playRecord', this.playRecattributes, this.playRecmodel);
	this.controller.listen('playRecord', Mojo.Event.tap, this.handlePlayRecord.bind(this));

	this.Initialize();
}

AudioHeadsetMicrophoneAssistant.prototype.deactivate = function()
{
	this.Initialize();
	this.restoreScenario();
}

AudioHeadsetMicrophoneAssistant.prototype.cleanup = function()
{
	SceneChoices.push( "audioHeadsetMicrophone" );
}
AudioHeadsetMicrophoneAssistant.prototype.fixPlayButton = function()
{
	this.playmodel.buttonLabel = $L("Play");
	this.playmodel.disabled = false;
	this.controller.modelChanged(this.playmodel);
}

AudioHeadsetMicrophoneAssistant.prototype.handlePlay = function()
{
	this.playmodel.buttonLabel = $L("Playing");
	this.playmodel.disabled = true;
	this.controller.modelChanged(this.playmodel);

	setTimeout( this.fixPlayButton.bind(this), 5000 );
  	var request = new Mojo.Service.Request('palm://com.palm.audio/media', {
		method: 'getCurrentScenario',
		parameters: { },
		onSuccess: function(response) {
			this.oldScenario = response.scenario;

			if(this.currentScenario != this.oldScenario)
			{
				var request2 = new Mojo.Service.Request('palm://com.palm.audio/media', {
					method: 'setCurrentScenario',
					parameters: { "scenario":this.currentScenario },
					onSuccess: this.playSoundFile.bind(this),
					onFailure: function() {
						Logging.log(this.errorClass, "failed Mojo call: setCurrentScenario"); 
					}.bind(this)
				});
			}
			else
			{
				this.playSoundFile();
			}
		}.bind(this),
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: getCurrentScenario"); 
		}.bind(this)
	});
}

AudioHeadsetMicrophoneAssistant.prototype.handlePlayNo = function()
{
	this.checkError("Headset Microphone playback failed.");
	SceneChoices.pushNextScene(this.controller.stageController);
}

AudioHeadsetMicrophoneAssistant.prototype.handlePlayYes = function()
{
	Logging.log(this.errorClass, "playback success"); 

	this.controller.get('audioHeadsetMicrophonePlay').hide();
	this.controller.get('audioHeadsetMicrophoneRecord').show();
}

AudioHeadsetMicrophoneAssistant.prototype.fixRecordButton = function()
{
	this.model.buttonLabel = $L("Record");
	this.model.disabled = false;
	this.controller.modelChanged(this.model);
}

AudioHeadsetMicrophoneAssistant.prototype.handleRecord = function()
{
	this.model.buttonLabel = $L("Recording");
	this.model.disabled = true;
	this.controller.modelChanged(this.model);

	setTimeout( this.fixRecordButton.bind(this), 5000 );

  	var request = new Mojo.Service.Request('palm://com.palm.audio/media', {
		method: 'getCurrentScenario',
		parameters: { },
		onSuccess: function(response) {
			if(!this.oldScenario)
				this.oldScenario = response.scenario;

			if(this.currentScenario != this.oldScenario)
			{
				var request2 = new Mojo.Service.Request('palm://com.palm.audio/media', {
					method: 'setCurrentScenario',
					parameters: { "scenario":this.currentScenario },
					onSuccess: this.recordSoundFile.bind(this),
					onFailure: function() {
						Logging.log(this.errorClass, "failed Mojo call: setCurrentScenario");
					}.bind(this)
				});
			}
			else
			{
				this.recordSoundFile();
			}
		}.bind(this),
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: getCurrentScenario");
		}.bind(this)
	});
}

AudioHeadsetMicrophoneAssistant.prototype.recordSoundFile = function()
{
	var request = new Mojo.Service.Request('palm://com.palm.crotest', {
		method: 'RecordSound',
		parameters: {},
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: RecordSound");
		}.bind(this)
	});
}
AudioHeadsetMicrophoneAssistant.prototype.fixPlayRecordButton = function()
{
	this.playRecmodel.buttonLabel = $L("Play");
	this.playRecmodel.disabled = false;
	this.controller.modelChanged(this.playRecmodel);
}

AudioHeadsetMicrophoneAssistant.prototype.handlePlayRecord = function()
{
	this.playRecmodel.buttonLabel = $L("Playing");
	this.playRecmodel.disabled = true;
	this.controller.modelChanged(this.playRecmodel);

	setTimeout( this.fixPlayRecordButton.bind(this), 5000 );
  	var request = new Mojo.Service.Request('palm://com.palm.audio/media', {
		method: 'getCurrentScenario',
		parameters: { },
		onSuccess: function(response) {
			if(!this.oldScenario)
				this.oldScenario = response.scenario;

			if(this.currentScenario != this.oldScenario)
			{
				var request2 = new Mojo.Service.Request('palm://com.palm.audio/media', {
					method: 'setCurrentScenario',
					parameters: { "scenario":this.currentScenario },
					onSuccess: this.playRecordFile.bind(this),
					onFailure: function() {
						Logging.log(this.errorClass, "failed Mojo call: setCurrentScenario");
					}.bind(this)
				});
			}
			else
			{
				this.playRecordFile();
			}
		}.bind(this),
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: getCurrentScenario");
		}.bind(this)
	});
}

AudioHeadsetMicrophoneAssistant.prototype.wipeRecording = function()                             
{
	var request = new Mojo.Service.Request('palm://com.palm.crotest', {
		method: 'RemoveRecording',
		parameters: {},
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: RemoveRecording"); 
		}.bind(this)
	});
}

AudioHeadsetMicrophoneAssistant.prototype.handleRecordNo = function()                             
{
	this.checkError("Headset Microphone recording failed.");
	this.wipeRecording();
	SceneChoices.pushNextScene(this.controller.stageController);
}

AudioHeadsetMicrophoneAssistant.prototype.handleRecordYes = function()                             
{
	Logging.log(this.errorClass, "recording success");
	this.wipeRecording();
	SceneChoices.pushNextScene(this.controller.stageController);
}

AudioHeadsetMicrophoneAssistant.prototype.playSoundFile = function()
{
	this.playSound(this.playFile);
}

AudioHeadsetMicrophoneAssistant.prototype.playRecordFile = function()
{
	this.playSound(this.recordFile);
}

AudioHeadsetMicrophoneAssistant.prototype.playSound = function(file)
{
  	var request = new Mojo.Service.Request('palm://com.palm.crotest', {
		method: 'PlaySound',
		parameters: {"file":file},
		onSuccess: function() {
			Logging.log(this.errorClass, "successful Mojo call: PlaySound");
		}.bind(this),
		onFailure: function() {
			Logging.log(this.errorClass, "failed Mojo call: PlaySound");
		}.bind(this)
	});
}

AudioHeadsetMicrophoneAssistant.prototype.restoreScenario = function()
{
	if(this.oldScenario && this.currentScenario != this.oldScenario)
	{
	  	var request = new Mojo.Service.Request('palm://com.palm.audio/media', {
			method: 'setCurrentScenario',
			parameters: { "scenario":this.oldScenario },
			onFailure: function() {
				Logging.log(this.errorClass, "failed Mojo call: setCurrentScenario");
			}.bind(this)
		});
	}
}

AudioHeadsetMicrophoneAssistant.prototype.checkError = function(message)
{
	var request = new Mojo.Service.Request('palm://com.palm.keys/switches', {
		method: 'status',
		parameters: { "get":"ringer" },
		onSuccess: function(response)
		{
			if(response.state == "up")	// NOT silent mode, check volume
			{
				var request2 = new Mojo.Service.Request('palm://com.palm.audio/media', {
					method: 'getVolume',
					parameters: { "scenario":this.currentScenario },
					onSuccess: function(response) {
						if(response.volume != 0)	// NOT muted
							Logging.log(this.errorClass, message);
						else
							Logging.log(this.errorClass, "volume set to 0");
					}.bind(this)
				});
			}
			else
				Logging.log(this.errorClass, "ringer switch set to silent");
		}.bind(this)
	});
}

