VolumeAssistant.prototype.VolumeUpCount = 0;
VolumeAssistant.prototype.VolumeDownCount = 0;
VolumeAssistant.prototype.SwitchesSubcription = undefined;
VolumeAssistant.prototype.Step = 0;


/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function VolumeAssistant()
{
}

VolumeAssistant.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 (typeof this[event.command] == "function") {
			this[event.command](event);
		}
	} else if (event.type == Mojo.Event.commandEnable && 
		 (event.command == Mojo.Menu.helpCmd)) {
		event.stopPropagation();
	}
}

/************************************************************************
 * Do initial setup
 ************************************************************************/
VolumeAssistant.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('NoResponse').observe(Mojo.Event.tap, this.handleNoResponse.bindAsEventListener(this));
	this.commandMenuModel = {
			visible:true,
			items: [{ }] 
    }; 
	var commandMenuItems = new Array();
    
  
	var cmdmenuItemSkip = new Object();
    cmdmenuItemSkip.label = $L("Skip");
    cmdmenuItemSkip.command = "handleSkip";
    commandMenuItems.push(cmdmenuItemSkip);
	var cmdmenuItem1 = new Object();
    commandMenuItems.push(cmdmenuItem1);
	var cmdmenuItem2 = new Object();
    commandMenuItems.push(cmdmenuItem2);
	
	this.commandMenuModel.items = commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.commandMenuModel);
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));

	this.Initialize();
}

VolumeAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("ringer");
}

VolumeAssistant.prototype.activate = function()
{
	//console.log( "Volume: activate called" );
	this.Step = 0;
	this.VolumeDownCount = 0;
	this.VolumeUpCount = 0;
	this.SubscribeSwitches();
}

VolumeAssistant.prototype.cleanup = function()
{
	//console.log( "Volume: cleanup called" );
}

VolumeAssistant.prototype.Initialize = function()
{
	this.controller.get('instructions').update( $L("Increase the volume 3 times.") );
	this.controller.get('image').src = "images/volumeUp.png";	
		
}

VolumeAssistant.prototype.deactivate = function()
{
	//console.log( "Volume: deactivate called" );
  	if (this.SwitchesSubcription != undefined)
		this.SwitchesSubcription .cancel();

	this.Initialize();
}

VolumeAssistant.prototype.SubscribeSwitches = function()
{
  	this.SwitchesSubcription = new Mojo.Service.Request('palm://com.palm.keys/audio', {
					                    method: 'status',
					                    parameters: {'subscribe': true},
										onFailure: function() {
												console.log("Volume: Failed to subscribe to audio updates");
											},
										onSuccess: this.onSwitchesCallback.bind(this), 
										//onComplete: this.onSwitchesCallback.bind(this), 
					                	});


}

VolumeAssistant.prototype.onSwitchesCallback = function(payload)
{
	console.log( "Volume: Step=" + this.Step + ", " + Object.toJSON( payload ) + "VolumeUpCount=" + this.VolumeUpCount + "; VolumeDownCount=" + this.VolumeDownCount );
	if ((this.Step == 0) && (payload.key === "volume_up") && (payload.state === "up"))
	{
		this.VolumeUpCount++;

		if ( this.VolumeUpCount === 3 )
			this.switchState( payload.key );
	}
	else if ((this.Step == 1) && (payload.key === "volume_down") && (payload.state === "up"))
	{
		this.VolumeDownCount++;

		if ( this.VolumeDownCount === 3 )
			this.switchState( payload.key );
	}

}

VolumeAssistant.prototype.switchState = function( key )
{
	//console.log( "Volume: SWITCHING STATE! = " + key );
	this.Step++;

	if (key != undefined)
		Logging.log( "Volume", "Successfully detected " + key);

	if (this.Step == 1)
	{
		this.controller.get('image').src = "images/volumeDown.png";		
		this.controller.get('instructions').update( $L("Decrease the volume 3 times.") );
	} 
	else if (this.Step == 2)
	{
		this.controller.stageController.pushScene("ringer");
	}
}

/************************************************************************
 * If the user clicks the "test" button, skip to the next test
 ************************************************************************/
VolumeAssistant.prototype.handleNoResponse = function()
{
	var str = "No response to instructions: " + this.controller.get('instructions').innerHTML;
	Logging.log( "Volume", str );

	this.switchState();
}


