BackGestureAssistant.prototype.Step = 0;

/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function BackGestureAssistant()
{
	this.commandMenuItems = new Array();
	this.commandMenuModel = {
			visible:true,
			items: [{ }] 
    };
	this.cmdmenuItemSkipDone = new Object();
}

/************************************************************************
 * When activated, begin listening for keydown events
 ************************************************************************/
BackGestureAssistant.prototype.activate = function(param)
{
	this.Step = 0;
}

BackGestureAssistant.prototype.Initialize = function()
{
	this.controller.get('NoResponse').show();
	//this.controller.get('Continue').hide();
	this.controller.get('image').src = "images/backgesture.png";
	this.controller.get('instructions').update( $L("Slide your finger from right to left as shown above.") );
	this.controller.get('complete_instructions').hide();
}

/************************************************************************
 * Upon deactivate, stop listening for the keydown event
 ************************************************************************/
BackGestureAssistant.prototype.deactivate = function()
{
	// Reset the screen for the next time it's shown
	this.Initialize();
}

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

BackGestureAssistant.prototype.setup = function()
{
	this.appMenuModel = {
		visible: true,
		items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
	};
	this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);
 

    
  
	//var cmdmenuItemSkip = new Object();
    this.cmdmenuItemSkipDone.label = $L("Skip");
    this.cmdmenuItemSkipDone.command = "handleSkip";
    this.commandMenuItems.push(this.cmdmenuItemSkipDone);
	//var cmdmenuItem1 = new Object();
    //commandMenuItems.push(cmdmenuItem1);
	//var cmdmenuItem2 = new Object();
    //commandMenuItems.push(cmdmenuItem2);
	
	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.handleNoResponse.bindAsEventListener(this));
	//this.controller.get('Continue').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));

	this.Initialize();
}

BackGestureAssistant.prototype.handleNoResponse = function()
{
	Logging.log( "Back Gesture", "No response" );
	this.controller.stageController.pushScene("gestureStrip");
}

BackGestureAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("gestureStrip");
}

BackGestureAssistant.prototype.handleCommand = function(event)
{
	console.log( "BackGesture: back gesture caught" );
	if (event.type == Mojo.Event.back)
	{
		//event.preventDefault();
		//event.stopPropogation();
		if (this.Step == 0)
		{
			Logging.log( "Back Gesture", "Successful" );
			this.Step++;
			this.controller.get('instructions').hide();
			this.controller.get('image').src = "images/BigCheck.png";
			this.controller.get('complete_instructions').update( $L("You performed the back gesture properly.") );
			this.controller.get('complete_instructions').show();
			this.controller.get('NoResponse').hide();
			//this.controller.get('Skip').hide();
			//this.controller.get('Continue').show();
			this.commandMenuItems.pop();
			var cmdmenuItem1 = new Object();
    		this.commandMenuItems.push(cmdmenuItem1);
			var cmdmenuItem2 = new Object();
    		this.commandMenuItems.push(cmdmenuItem2);
		    this.cmdmenuItemSkipDone.label = $L("Done");
   		    this.cmdmenuItemSkipDone.command = "handleSkip";
    	    this.commandMenuItems.push(this.cmdmenuItemSkipDone);
			this.commandMenuModel.items = this.commandMenuItems;
		    this.controller.modelChanged(this.commandMenuModel);
			Event.stop(event);
		}
	}else if (typeof this[event.command] == "function") {
			this[event.command](event);
		}
}



