ScreenAssistant.prototype.Step = 0;
ScreenAssistant.prototype.backgroundColor = ["white", "red", "green", "blue", ""];
ScreenAssistant.prototype.textColor = ["black", "white", "white", "white", "black"];

function ScreenAssistant()
{
	this.commandMenuItems = new Array();
	this.commandMenuModel = {
			visible:true,
			items: [{ }] 
    };
	this.cmdmenuItemSkipDone = new Object();
}

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

ScreenAssistant.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.tapHandler = this.handleTap.bindAsEventListener(this);
	this.controller.sceneElement.observe(Mojo.Event.tap, this.tapHandler)
	this.cmdmenuItemSkipDone.label = $L("Skip");
    this.cmdmenuItemSkipDone.command = "handleSkip";
    this.commandMenuItems.push(this.cmdmenuItemSkipDone);
		
	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('TryAgain').observe(Mojo.Event.tap, this.handleTryAgain.bindAsEventListener(this));
	//this.controller.get('Continue').observe(Mojo.Event.tap, this.handleContinue.bindAsEventListener(this));

	this.WhiteEvent = false;
	this.RedEvent = false;
	this.GreenEvent = false;
	this.BlueEvent = false;
	this.WhiteAttribute = {
		modelProperty:"value"
	};
	this.WhiteModel = { value: this.WhiteEvent };
	this.RedAttribute = {
		modelProperty:"value"
	};
	this.RedModel = { value: this.RedEvent };
	this.GreenAttribute = {
		modelProperty:"value"
	};
	this.GreenModel = { value: this.GreenEvent };
	this.BlueAttribute = {
		modelProperty:"value"
	};
	this.BlueModel = { value: this.BlueEvent };
	this.controller.setupWidget('WhiteCheckBox', this.WhiteAttribute, this.WhiteModel);
	this.controller.setupWidget('RedCheckBox', this.RedAttribute, this.RedModel);
	this.controller.setupWidget('GreenCheckBox', this.GreenAttribute, this.GreenModel);
	this.controller.setupWidget('BlueCheckBox', this.BlueAttribute, this.BlueModel);
	this.controller.listen('whitecb_div', Mojo.Event.tap, this.handleWhite.bind(this) );
	this.controller.listen('WhiteCheckBox', Mojo.Event.propertyChange, this.handleWhite.bind(this) );
	this.controller.listen('redcb_div', Mojo.Event.tap, this.handleRed.bind(this) );
	this.controller.listen('RedCheckBox', Mojo.Event.propertyChange, this.handleRed.bind(this) );
	this.controller.listen('greencb_div', Mojo.Event.tap, this.handleGreen.bind(this) );
	this.controller.listen('GreenCheckBox', Mojo.Event.propertyChange, this.handleGreen.bind(this) );
	this.controller.listen('bluecb_div', Mojo.Event.tap, this.handleBlue.bind(this) );
	this.controller.listen('BlueCheckBox', Mojo.Event.propertyChange, this.handleBlue.bind(this) );

	this.controller.get('checkboxes').hide();
	//this.controller.get('TryContinue').hide();
	this.controller.get('TryAgain').hide();
	//this.controller.get('Continue').hide();
}
ScreenAssistant.prototype.handleWhite = function( )
{
	if (this.WhiteEvent == true)
		this.WhiteEvent = false;
	else
		this.WhiteEvent = true;
	if (this.WhiteEvent == false) {
		this.WhiteModel.value = false;
	}
	else {
		this.WhiteModel.value = true;
	}
	this.controller.modelChanged(this.WhiteModel);
}
 
ScreenAssistant.prototype.handleRed = function( )
{
	if (this.RedEvent == true)
		this.RedEvent = false;
	else
		this.RedEvent = true;
	if (this.RedEvent == false) {
		this.RedModel.value = false;
	}
	else {
		this.RedModel.value = true;
	}
	this.controller.modelChanged(this.RedModel);
}
 
ScreenAssistant.prototype.handleGreen = function( )
{
	if (this.GreenEvent == true)
		this.GreenEvent = false;
	else
		this.GreenEvent = true;
	if (this.GreenEvent == false) {
		this.GreenModel.value = false;
	}
	else {
		this.GreenModel.value = true;
	}
	this.controller.modelChanged(this.GreenModel);
}
 
ScreenAssistant.prototype.handleBlue = function( )
{
	if (this.BlueEvent == true)
		this.BlueEvent = false;
	else
		this.BlueEvent = true;
	if (this.BlueEvent == false) {
		this.BlueModel.value = false;
	}
	else {
		this.BlueModel.value = true;
	}
	this.controller.modelChanged(this.BlueModel);
}
ScreenAssistant.prototype.handleTryAgain = function()
{
	this.activate();
}

ScreenAssistant.prototype.handleContinue = function()
{
	var str = "";
	str += this.WhiteModel.value ? "White " : "";
	str += this.RedModel.value ? "Red " : "";
	str += this.GreenModel.value ? "Green " : "";
	str += this.BlueModel.value ? "Blue " : "";

	if (str.length > 0)
	{
		str = "Colors with issues: " + str;
		Logging.log("Screen Colors", str );
	}
	else
	{
		Logging.log("Screen Colors", "No issues" );
	}

	this.controller.stageController.pushScene("proximity");
}

ScreenAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("proximity");
}

ScreenAssistant.prototype.deactivate = function()
{
	// Set up the screen so that next time we don't flicker when re-setting up the screen
	this.activate();
	this.controller.window.PalmSystem.enableFullScreenMode(false);
}

ScreenAssistant.prototype.activate = function()
{
	this.controller.get('instructions').update( $L("Tap the screen 5 times.  It will change color each time.<BR><BR>Examine the screen closely after each tap.") );
	this.controller.get('wrapinstructions').show( );
	this.Step = 0;
	//this.controller.get('Skip').show();
	//this.controller.get('SkipDiv').show();
	//this.controller.get('TryContinue').hide();
	this.controller.get('TryAgain').hide();
	//this.controller.get('Continue').hide();
	this.controller.get('header').show();
	this.controller.get('checkboxes').hide();
	
	//Reset the background
	this.controller.window.PalmSystem.enableFullScreenMode(false);
	var obj = this.controller.get("screen");
	obj.style.backgroundColor = this.backgroundColor[4];
	obj.style.Color = this.textColor[4];
	this.controller.get("screen").show();

	// Reset these values
	this.WhiteModel.value = false;
	this.RedModel.value = false;
	this.GreenModel.value = false;
	this.BlueModel.value = false;
	this.controller.modelChanged(this.WhiteModel, this);
	this.controller.modelChanged(this.RedModel, this);
	this.controller.modelChanged(this.GreenModel, this);
	this.controller.modelChanged(this.BlueModel, this);
	
}

ScreenAssistant.prototype.handleTap = function()
{
	var obj = this.controller.get("screen");
	obj.style.backgroundColor = this.backgroundColor[this.Step];
	obj.style.Color = this.textColor[this.Step];

	this.controller.get('wrapinstructions').hide( );

	if (this.Step == 0)
	{
		//this.controller.get('SkipDiv').hide();
		this.controller.window.PalmSystem.enableFullScreenMode(true);
	}

	if (this.Step < 4)
	{
		this.controller.get('header').hide();
		this.controller.get('checkboxes').hide();
		this.Step++;
	}
	else
	{
		this.controller.window.PalmSystem.enableFullScreenMode(false);
		this.controller.get('header').show();
		this.controller.get('checkboxes').show();
		this.controller.get("screen").hide();
		//show continue
		this.commandMenuItems.pop();
		var cmdmenuItem1 = new Object();
    	this.commandMenuItems.push(cmdmenuItem1);
		var cmdmenuItem2 = new Object();
    	this.commandMenuItems.push(cmdmenuItem2);
		this.cmdmenuItemSkipDone.label = $L("Continue");
   		this.cmdmenuItemSkipDone.command = "handleContinue";
    	this.commandMenuItems.push(this.cmdmenuItemSkipDone);
		this.commandMenuModel.items = this.commandMenuItems;
		this.controller.modelChanged(this.commandMenuModel);
		//this.controller.get('TryContinue').show();
		this.controller.get('TryAgain').show();
		//this.controller.get('Continue').show();
	}

}

