RingerAssistant.prototype.SwitchSubscription = undefined;
RingerAssistant.prototype.Step = 0;
RingerAssistant.prototype.state = null;

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

RingerAssistant.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
 ************************************************************************/
RingerAssistant.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.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('NoResponse').observe(Mojo.Event.tap, this.handleNoResponse.bindAsEventListener(this));
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));

	this.controller.setupWidget('spinnerDiv', 
		{ spinnerSize: Mojo.Widget.spinnerLarge },  
		{ spinning: true });

	this.Initialize();
}

RingerAssistant.prototype.Initialize = function()
{
	this.controller.get('spinnerDiv').show(); 
	this.controller.get('image').hide();
	this.controller.get('instructions').hide();
}

RingerAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("complete", $L("Hardware Tests"), $L("The hardware tests are complete.") );
}

RingerAssistant.prototype.activate = function()
{
	//console.log( "Ringer: activate called" );
	this.Step = 0;
	this.SubscribeSwitches( );

	this.getRingerValue();
}

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

RingerAssistant.prototype.deactivate = function()
{
	//console.log( "Ringer: deactivate called" );
	if (this.SwitchSubscription != undefined)
		this.SwitchSubscription.cancel();

	// Setup screen for next time we are activated
	this.Initialize();
}

RingerAssistant.prototype.getRingerValue = function()
{
  	var request = new Mojo.Service.Request('palm://com.palm.keys/switches', {
					                    method: 'status',
					                    parameters: {'get': 'ringer'},
										//onComplete: this.onSwitchesCallback.bind(this), 
										onFailure: function() {
												console.log("Ringer: Failed to get ringer status");
											},
										//onSuccess: this.onSwitchesCallback.bind(this), 
										onSuccess: function( data ) 
											{
												//console.log( "Ringer: " + Object.toJSON( data ) );
												if (data.key === "ringer")
												{
													this.state = data.state;
													this.switchState( );
													this.controller.get('image').show();
													this.controller.get('spinnerDiv').hide(); 
												}
											}.bind(this)
					                	});

}

RingerAssistant.prototype.SubscribeSwitches = function( )
{
	//console.log( "Ringer: Subscribe called");
  	this.SwitchSubscription = new Mojo.Service.Request('palm://com.palm.keys/switches', {
					                    method: 'status',
					                    parameters: {'subscribe': true},
										//onComplete: this.onSwitchesCallback.bind(this), 
										onFailure: function() {
												console.log("Ringer: Failed to subscribe to switch notifications");
											},
										onSuccess: this.onSwitchesCallback.bind(this), 
					                	});
}

RingerAssistant.prototype.onSwitchesCallback = function(payload)
{
	//console.log( "Ringer: " + Object.toJSON( payload ) );
	if (payload.key === "ringer")
	{
		if (payload.state != this.state)
		{
			this.state = payload.state;
			Logging.log( "Ringer", "Successfully detected " + this.state);

			this.Step++;
			this.switchState( );
		}
	}
}

RingerAssistant.prototype.switchState = function( )
{
	//console.log( "Ringer, ringer=" + this.state );

	this.controller.get('instructions').show();
	if (this.Step == 0)
	{
		this.controller.get('instructions').update( $L("Slide the Ringer switch.") );
	}
	else if (this.Step == 1)
	{
		this.controller.get('instructions').update( $L("Slide the Ringer switch the other direction.") );
		//this.controller.get('Skip').update( $L("Done") );
	}
	else if (this.Step == 2)
	{
		this.handleSkip();
		return;
	}
	
	
	if ( this.state === "down" )
	{
		this.controller.get('image').src = "images/ringerOff.png";
	}
	else if ( this.state === "up" )
	{
		this.controller.get('image').src = "images/ringerOn.png";
	}

}

/************************************************************************
 * If the user clicks the "test" button, skip to the next test
 ************************************************************************/
RingerAssistant.prototype.handleNoResponse = function()
{
	var str = "No response sliding ringer while in state: " + this.state;
	Logging.log( "Ringer", str );

	this.Step++;

	if (this.state == "up")
		this.state = "down";
	else if (this.state == "down")
		this.state = "up";

	this.switchState();
}


