KeyboardAssistant.prototype.KeyDown = null;
KeyboardAssistant.prototype.keyToImageMap =
{
	13: "enter",
	8: "backspace",
	16: "shift",
	129: "orange",
	32: "space",
};
KeyboardAssistant.prototype.keyToTextMap =
{
	190: ".",
	188: ",",
	48: "@",
	17: "Sym",
}

/************************************************************************
 * Create the class, initialize things
 ************************************************************************/
function KeyboardAssistant()
{
	// Function bindings
	this.onKeyDown = this.onKeyDown.bindAsEventListener(this);
	this.onKeyUp = this.onKeyUp.bindAsEventListener(this);

}


KeyboardAssistant.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
 ************************************************************************/
KeyboardAssistant.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.cmdMenuModel = {
			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.cmdMenuModel.items = commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.cmdMenuModel);	
	  
   
	//this.controller.get('Skip').observe(Mojo.Event.tap, this.handleSkip.bindAsEventListener(this));
}

KeyboardAssistant.prototype.handleSkip = function()
{
  	var request = new Mojo.Service.Request('palm://com.palm.crotest/GetFeatures', {
		parameters: {},
		onSuccess: function (features) {
			if(features.hasSlider)
				this.controller.stageController.pushScene("keyboardSlider");
			else
				this.controller.stageController.pushScene("volume");			
		}.bind(this),
		onFailure: function() {
			console.log("Failed to retrieve phone features");
			this.controller.stageController.pushScene("keyboardSlider");
		}.bind(this)
	});
}

/************************************************************************
 * When activated, begin listening for keydown events
 ************************************************************************/
KeyboardAssistant.prototype.activate = function(param)
{
	this.controller.document.addEventListener("keydown", this.onKeyDown, true);
	this.controller.document.addEventListener("keyup", this.onKeyUp, true);
}

/************************************************************************
 * Upon deactivate, stop listening for the keydown event
 ************************************************************************/
KeyboardAssistant.prototype.deactivate = function()
{
	this.controller.document.removeEventListener("keydown", this.onKeyDown, true);
	this.controller.document.removeEventListener("keyup", this.onKeyUp, true);
}

/************************************************************************
 * When the key is released, go to the next test
 ************************************************************************/
KeyboardAssistant.prototype.onKeyUp = function(event)
{
	if (event.keyCode == this.KeyDown)
	{
		this.KeyDown = null;
		//this.controller.get('letter').style.backgroundColor = "white";
		this.controller.get('letter').style.color = "black";
		this.controller.get('characterPanel').src = "images/character_panel.png";

		// If we need to show an alternate image, do so
		for (var item in this.keyToImageMap)
		{
			if (event.keyCode == item)
			{
				this.controller.get('letter').update( "<img src='images/" + this.keyToImageMap[ event.keyCode ] + "Up.png'>" );
			}
		}

	}
	else {
		//fix for buttons not working after pressing shift+ key combination
		//console.log("onKeyUp ketcode NOT= keydown" + "keyCode" + event.keyCode + "keyDown" + this.KeyDown);
		if (event.keyCode == 129 && this.KeyDown != null) {
		
			this.KeyDown = null;
			this.controller.get('letter').style.color = "black";
			this.controller.get('characterPanel').src = "images/character_panel.png";
		}
	}   

}

/************************************************************************
 * When a key is depressed, if it's the key we're looking for then 
 * show the darker image and increase to the next key
 ************************************************************************/
KeyboardAssistant.prototype.onKeyDown = function(event)
{
	/* Keyboard keycodes
	[81,87,69,114,84,89,85,73,79,80,
	65,83,68,70,71,72,74,75,76,8,
	18,90,88,67,86,66,78,77,188,13,
	16,64,32,190,17];
	*/

	console.log("Keyboard: keycode = " + event.keyCode );
	if (this.KeyDown != null)
		return;

	var letterDiv = this.controller.get('letter');

	if ((event.keyCode == 91) || (event.keyCode == 27) || (event.keyCode == 231))
	{
		// Gesture area touch,  or "back" or "forward" gesture
		return;
	}

	this.KeyDown = event.keyCode;
	this.controller.get('characterPanel').src = "images/character_panel-down.png";
	letterDiv.style.color = "white";
	//letterDiv.style.backgroundColor = "black";

	// First see if we want to show an image instead of text
	for (var item in this.keyToImageMap)
	{
		if (event.keyCode == item)
		{
			letterDiv.update( "<img src='images/" + this.keyToImageMap[ event.keyCode ] + "Down.png'>" );
			return;
		}
	}

	// Now check if we need to show 'special' text instead of the default associated character
	for (var item in this.keyToTextMap)
	{
		if (event.keyCode == item)
		{
			letterDiv.update( this.keyToTextMap[ item ]  );
			return;
		}
	}

	// All other 'regular' keys
	letterDiv.update( String.fromCharCode(event.keyCode).toUpperCase());

}

