ProximityAssistant.prototype.Polling = false;
ProximityAssistant.prototype.RequestCall = undefined;
ProximityAssistant.prototype.RequestCallOn = undefined;
ProximityAssistant.prototype.RequestCallOff = undefined;

function ProximityAssistant()
{
}

ProximityAssistant.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
 ************************************************************************/
ProximityAssistant.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";
    commandMenuItems.push(cmdmenuItemSkip);
	var cmdmenuItem1 = new Object();
    commandMenuItems.push(cmdmenuItem1);
	var cmdmenuItem2 = new Object();
    commandMenuItems.push(cmdmenuItem2);
	
	this.commandMenuModel.items = commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.commandMenuModel);
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
	this.controller.get('Yes').observe(Mojo.Event.tap, this.handleYes.bindAsEventListener(this));
	this.controller.get('No').observe(Mojo.Event.tap, this.handleNo.bindAsEventListener(this));

	this.controller.get('instructions').update( $L("Hold your hand over the screen.<br><br>Did the phone vibrate?") );
}

ProximityAssistant.prototype.handleNo = function()
{
	Logging.log( "Proximity Sensor", "No response" );
	this.controller.stageController.pushScene("keyboardBacklight");
}

ProximityAssistant.prototype.handleYes = function()
{
	Logging.log( "Proximity Sensor", "Yes response" );
	this.controller.stageController.pushScene("keyboardBacklight");
}

ProximityAssistant.prototype.handleSkip = function()
{
	this.controller.stageController.pushScene("keyboardBacklight");
}

ProximityAssistant.prototype.ProximityGetData = function(data)
{
	console.log("Proximity: ProximityGetData: " + Object.toJSON(data));

	if (this.Polling)
	{
		if (data == undefined || data.success === "true")
		{
			this.RequestCall = new Mojo.Service.Request('palm://com.palm.crotest', {
												method: 'Proximity',
												parameters: {"command": "getdata"},
												onSuccess: this.ProximityGotData.bind(this),
												onFailure: function() {
														console.log("Proximity: Failed to turn proximity on");
													}
												});
		}
	}
}

ProximityAssistant.prototype.ProximityGotData = function(data)
{
	console.log("Proximity: ProximityGotData: " + Object.toJSON(data));

	if ((data.success == "") || (data.success == "Un-detection"))
	{
		// wait 1 second and try again
		setTimeout( this.ProximityGetData.bind(this), 500);
	}
	else if (data.success === "Detection")
	{
		Vibrate.vibrate();
		this.turnProximityOff();
		Logging.log( "Proximity Sensor", "Successful" );
		//this.controller.stageController.pushScene("keyboardBacklight");
	}
}

ProximityAssistant.prototype.activate = function()
{
	console.log( "Proximity: activate called" );
	this.turnProximityOn();
}

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

ProximityAssistant.prototype.deactivate = function()
{
	console.log( "Proximity: deactivate called" );
	this.turnProximityOff();
}

ProximityAssistant.prototype.turnProximityOff = function()
{
	console.log( "Proximity: Turning proximity off" );
	this.Polling = false;
  	this.RequestCallOff = new Mojo.Service.Request('palm://com.palm.crotest', {
					                    method: 'Proximity',
					                    parameters: {"command": "off"},
										onSuccess: function() {
												console.log("Proximity: Successfully turned proximity off");
											},
										onFailure: function() {
												console.log("Proximity: Failed to turn proximity on");
											}
					                	});
}


ProximityAssistant.prototype.turnProximityOn = function()
{
	console.log( "Proximity: Turning proximity on" );
	this.Polling = true;
  	this.RequestCallOn = new Mojo.Service.Request('palm://com.palm.crotest', {
					                    method: 'Proximity',
					                    parameters: {"command": "on"},
										onSuccess: this.ProximityGetData.bind(this),
										onFailure: function() {
												console.log("Proximity: Failed to turn proximity on");
											}
					                	});
}

