KeyboardSliderAssistant.prototype.SwitchSubscription = undefined;
KeyboardSliderAssistant.prototype.Step = 0;
KeyboardSliderAssistant.prototype.state = null;
/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function KeyboardSliderAssistant()
{
	this.commandMenuItems = new Array();
	this.commandMenuModel = {
			visible:true,
			items: [{ }] 
    };
	this.cmdmenuItemContinue = new Object();
}

KeyboardSliderAssistant.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
 ************************************************************************/
KeyboardSliderAssistant.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";
    this.commandMenuItems.push(cmdmenuItemSkip);
	var cmdmenuItem1 = new Object();
    this.commandMenuItems.push(cmdmenuItem1);
	
    this.cmdmenuItemContinue.label = $L("Continue");
    this.cmdmenuItemContinue.command = "handleContinue_NoResponse";
	this.commandMenuItems.push(this.cmdmenuItemContinue);
	this.commandMenuModel.items = this.commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.commandMenuModel);
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
	this.controller.get('NoResponse').observe(Mojo.Event.tap, this.handleContinue_NoResponse.bindAsEventListener(this));
	this.controller.setupWidget('spinnerDiv', 
		{ spinnerSize: Mojo.Widget.spinnerLarge },  
		{ spinning: true });
	this.controller.get('NoResponse').hide();
	this.controller.get('image').hide();
}

KeyboardSliderAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("volume");
}

KeyboardSliderAssistant.prototype.activate = function()
{
	console.log( "Slider: activate called   " + this.commandMenuItems.length);
	if (this.commandMenuItems.length == 2) {
		console.log( "push continue   " + this.commandMenuItems.length);
		this.cmdmenuItemContinue.label = $L("Continue");
		this.cmdmenuItemContinue.command = "handleContinue_NoResponse";
		this.commandMenuItems.push(this.cmdmenuItemContinue);
		this.commandMenuModel.items = this.commandMenuItems;
		this.controller.modelChanged(this.commandMenuModel);
	}
	this.controller.get('instructions').update($L("Tap Continue to test the keyboard slider."));
	this.Step = 0;
	this.controller.get('spinnerDiv').hide(); 
	//this.controller.get('NoResponse').update( $L("Continue") );
	this.controller.get('NoResponse').hide();
	//this.getSliderValue();

	//this.SubscribeSwitches();

}

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

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

	// Set up the screen for the next time we are activated
	//this.controller.get('spinnerDiv').show(); 
	//this.controller.get('instructions').hide();
	this.controller.get('image').hide();
}


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

}


KeyboardSliderAssistant.prototype.SubscribeSwitches = function()
{
  	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("Slider: Failed to subscribe to switch updates");
											},
										onSuccess: this.onSwitchesCallback.bind(this), 
					                	});

}

KeyboardSliderAssistant.prototype.onSwitchesCallback = function(payload)
{
	//console.log( "Slider: " + Object.toJSON( payload ) );
	if (payload.key === "slider")
	{
		if (this.state != payload.state)
		{
			Logging.log( "KeyboardSlider", "Successfully detected " + this.state);
			this.Step++;
			this.state = payload.state;
			this.switchState( );
		}
	}
}

KeyboardSliderAssistant.prototype.switchState = function( )
{
	//console.log( "Slider, state=" + this.state );

	if (this.Step == 3)
	{
		this.controller.stageController.pushScene("sliderCount");
		return;
	}

	if ( this.state === "up" )
	{
		this.controller.get('image').src = "images/sliderClose.png";
		this.controller.get('instructions').update( $L("Close the keyboard.") );
	}
	else if ( this.state === "down" )
	{
		this.controller.get('image').src = "images/sliderOpen.png";
		this.controller.get('instructions').update( $L("Open the keyboard.") );
	}
	this.controller.get('instructions').show();
}

/************************************************************************
 * If the user clicks the "test" button, skip to the next test
 ************************************************************************/
KeyboardSliderAssistant.prototype.handleContinue_NoResponse = function()
{
	//continue
	if (this.Step == 0) {
		//this.controller.get('spinnerDiv').show(); 
		this.Step++;
		this.getSliderValue();
	    this.SubscribeSwitches();
		this.controller.get('NoResponse').show();
		
		this.commandMenuItems.pop();//   .push(this.cmdmenuItemContinue);
		this.commandMenuModel.items = this.commandMenuItems;
		this.controller.modelChanged(this.commandMenuModel);
		//this.controller.get('NoResponse').update( $L("No Response") );
		
	}
	else {
		
		var str = "No response to instructions: " + $('instructions').innerHTML;
		Logging.log( "KeyboardSlider", str );
		this.Step++;
		if (this.state == "up") 
			this.state = "down";
		else 
			if (this.state == "down") 
				this.state = "up";
		
		this.switchState();
	}
}


