TouchgridAssistant.prototype.Step = 0;
TouchgridAssistant.prototype.backEvent = false;
function TouchgridAssistant()
{
	this.handleStart = this.handleStart.bindAsEventListener(this);
	this.handleSkip = this.handleSkip.bindAsEventListener(this);
	this.handleContinue = this.handleContinue.bindAsEventListener(this);
	this.handleMouseMove = this.handleMouseMove.bindAsEventListener(this);
	this.handleMouseUp = this.handleMouseUp.bindAsEventListener(this);
	this.handleMouseDown = this.handleMouseDown.bindAsEventListener(this);
	
}

/************************************************************************
 * Do initial setup
 ************************************************************************/
TouchgridAssistant.prototype.setup = function()
{
	

	
	console.log("TouchgridAssistant setup");
	
	
	var devInfo = Mojo.Environment.DeviceInfo;
	if (devInfo && devInfo.modelName) {
		console.log("devInfo" + Object.toJSON(devInfo))
		if (devInfo.modelName === "Pixi") {
			this.controller.get('topTable').deleteRow(1);
			this.controller.get('bottomTable').deleteRow(1);
		}
	}
	this.controller.get('Start').observe(Mojo.Event.tap, this.handleStart.bindAsEventListener(this));
	this.controller.get('BottomSkip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
	this.controller.get('TopSkip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
	this.controller.get('TopContinue').observe(Mojo.Event.tap, this.handleContinue.bindAsEventListener(this));
	this.controller.get('BottomContinue').observe(Mojo.Event.tap, this.handleContinue.bindAsEventListener(this));

	this.Initialize();
}
TouchgridAssistant.prototype.handleCommand = function(event) {
	 if (event.type == Mojo.Event.back) {
        this.backEvent = true;
    }
	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();
	}
}
TouchgridAssistant.prototype.handleContinue = function()
{
	console.log( "Touchgrid: handleContinue, this.Step=" + this.Step );

	this.Step++;
	if (this.Step == 1)
	{
		this.controller.get('topMessage').show( );
		this.controller.get('topButtons').show();
		this.controller.get('bottomButtons').hide();
		this.controller.get('bottomMessage').hide( );
		this.controller.get('topMessage').update( $L("Run your finger over all of the boxes to change them from black to green.") );
		this.controller.get('topGrid').hide();
		this.controller.get('bottomGrid').show();

		var rowsTop = this.controller.get('topTable').rows;
		for (var r=0; r < rowsTop.length; r++)
		{
			for (var j=0; j<8; j++)
			{
				if (rowsTop[r].cells.item(j).bgColor == "black")
					Logging.log( "Touchgrid", "Unselected cell: row=" + r + ", col=" + j);
			}
		}

	}
	else if (this.Step == 2)
	{
		this.controller.window.PalmSystem.enableFullScreenMode(false);
		this.handleSkip();

		var rowsBottom = this.controller.get('bottomTable').rows;

		for (var r=0; r < rowsBottom.length; r++)
		{
			for (var j=0; j<8; j++)
			{
				if (rowsBottom[r].cells.item(j).bgColor == "black")
					Logging.log( "Touchgrid", "Unselected cell: row=" + r + ", col=" + j);
			}
		}
	}
}

TouchgridAssistant.prototype.handleStart = function()
{
	this.controller.window.PalmSystem.enableFullScreenMode(true);
	this.controller.get('header').hide();
	this.controller.get('Start').hide();
	this.controller.get('bottomButtons').show();
	this.controller.get('BottomContinue').show();
	this.controller.get('BottomSkip').show();
  	this.controller.get('topMessage').hide( );
	this.controller.get('bottomMessage').show( );
	this.controller.get('bottomMessage').update( $L("Run your finger over all of the boxes to change them from black to green.</br>") );
	
	this.controller.get('topGrid').show();
}

TouchgridAssistant.prototype.handleSkip = function()
{
		this.controller.stageController.pushScene({name: "zoom", disableSceneScroller: true}); 
}

TouchgridAssistant.prototype.Initialize = function()
{
	console.log("TouchgridAssistant Initialize");
	
	
	this.controller.get('topMessage').update( $L("Tap 'Start' to test all of the sensors on the Touchscreen.") );
	this.controller.get('topMessage').show( );
	this.controller.get('bottomMessage').hide( );
	this.controller.get('BottomContinue').hide();
	this.controller.get('header').show();
	this.controller.get('Start').show();
	this.controller.get('BottomSkip').show();
	this.controller.get('topGrid').hide();
	this.controller.get('bottomGrid').hide();
	this.controller.get('topButtons').hide();
	this.controller.get('bottomButtons').show();
	this.Step = 0;
	
	var rowsTop = this.controller.get('topTable').rows;
	var rowsBottom = this.controller.get('bottomTable').rows;

	for (var r=0; r < rowsTop.length; r++)
	{
		for (var j=0; j<8; j++)
		{
			rowsTop[r].cells.item(j).bgColor = "black";
			rowsBottom[r].cells.item(j).bgColor = "black";
		}
	}
}

TouchgridAssistant.prototype.activate = function()
{
	console.log( "Touchgrid: activate called" );
	//this.cmdmenuItemDone.label = $L("Continue");
   // this.cmdmenuItemDone.command = "handleStart";
	//this.controller.modelChanged(this.commandMenuModel);
	
	this.controller.document.observe( "mousemove", this.handleMouseMove );
	this.controller.document.observe( "mouseup", this.handleMouseUp );
	this.controller.document.observe( "mousedown", this.handleMouseDown );
}

TouchgridAssistant.prototype.cleanup = function()
{
	console.log( "Touchgrid: cleanup called" );
}

TouchgridAssistant.prototype.deactivate = function()
{
	console.log( "Touchgrid: deactivate called" );
	this.controller.document.stopObserving( "mousemove", this.handleMouseMove );
	this.controller.document.stopObserving( "mouseup", this.handleMouseUp );
	this.controller.document.stopObserving( "mousedown", this.handleMouseDown );
	
//	Event.stopObserving( document, "mousemove", this.handleMouseMove );	
//	Event.stopObserving( document, "mouseup", this.handleMouseUp );	
//	Event.stopObserving( document, "mousedown", this.handleMouseDown );	

	//console.log( "Touchgrid: deactivate called" );
	this.controller.window.PalmSystem.enableFullScreenMode(false);

	this.Initialize();
}

TouchgridAssistant.prototype.handleMouseUp = function(event)
{
	//console.log( "Touchgrid: mouseup");
}

TouchgridAssistant.prototype.handleMouseDown = function(event)
{
	//console.log( "Touchgrid: mousedown" );
	if (event && event.target && event.target.bgColor)
	{
		event.target.bgColor = "#33FF99";
	}
}

TouchgridAssistant.prototype.handleMouseMove = function(event)
{
	//console.log( "Touchgrid: mousemove");
	if (event && event.target && event.target.bgColor)
	{
		event.target.bgColor = "#33FF99";
	}
}

