TouchSensorsAssistant.prototype.tapCount = 0;
TouchSensorsAssistant.prototype.SensorsTapped = [0, 0, 0, 0, 0];

/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function TouchSensorsAssistant()
{
	this.tapHandler = this.tapHandler.bindAsEventListener(this);
}

TouchSensorsAssistant.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();
	}
}

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


TouchSensorsAssistant.prototype.activate = function()
{
	this.tapCount = 0;
	this.SensorsTapped = [0, 0, 0, 0, 0];
	Mojo.listen(this.controller.document, Mojo.Event.tap, this.tapHandler);
}

TouchSensorsAssistant.prototype.deactivate = function()
{
	Mojo.stopListening(this.controller.document, Mojo.Event.tap, this.tapHandler);

	for (var i=1; i<6; i++)
	{
		var str = 'targetImage' + i;
		this.controller.get(str).src = "images/dotDeactivated.png";
	}
}

TouchSensorsAssistant.prototype.handleNoResponse = function()
{
	Logging.log( "TouchSensors", "Problem, Number of taps = " + this.tapCount + ", SensorsTapped: " + this.SensorsTapped );
	this.controller.stageController.pushScene("zoom");
}

TouchSensorsAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("zoom");
}

TouchSensorsAssistant.prototype.tapHandler = function(event)
{
	//console.log("TouchSensor: tapped!  Count=" + this.tapCount);
	if ( event['target'].id.match( "targetImage" ) )
	{
		Logging.log( "TouchSensors", "Tapped: " + event['target'].id );

		// Retrieve the sensor number
		var num = event['target'].id.match( "[0-9]" );
		if (this.SensorsTapped[ num-1 ] == 0)
		{
			this.controller.get(event['target'].id).src = "images/dotActivated.png";
			this.SensorsTapped[ num-1 ]  = 1;
			this.tapCount++;
		}
	}

	if (this.tapCount == 5)
	{
		Logging.log( "TouchSensors", "All taps complete" );
		this.handleSkip();
	}
}


