/*
	Demonstrates use of the List widget to display an editable list of words.
*/
function CommandMenuAssistant() {
	/* this is the creator function for your scene assistant object. It will be passed all the 
	   additional parameters (after the scene name) that were passed to pushScene. The reference
	   to the scene controller (this.controller) has not be established yet, so any initialization
	   that needs the scene controller should be done in the setup function below. */		
}

CommandMenuAssistant.prototype.setup = function(){
	this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, 
        {
            items:	[
                { label: 'Button 1', command: 'cmd-1' },
                { label: 'Button 2', command: 'cmd-2', width:150 }
            ]
        });
	this.showMenu = this.showMenu.bind(this);
	this.hideMenu = this.hideMenu.bind(this);
	this.toggleMenu = this.toggleMenu.bind(this);
	Mojo.Event.listen(this.controller.get('show_menu_button'),Mojo.Event.tap, this.showMenu);
	Mojo.Event.listen(this.controller.get('hide_menu_button'),Mojo.Event.tap, this.hideMenu);
	Mojo.Event.listen(this.controller.get('toggle_menu_button'),Mojo.Event.tap, this.toggleMenu);
}

CommandMenuAssistant.prototype.showMenu = function(event){
    // show the undo button if not already visible:
    this.controller.setMenuVisible(Mojo.Menu.commandMenu, true);
}


CommandMenuAssistant.prototype.hideMenu = function(event){
    // show the undo button if not already visible:
    this.controller.setMenuVisible(Mojo.Menu.commandMenu, false);
}

CommandMenuAssistant.prototype.toggleMenu = function(event){
    // toggle the visible state of the menu
    this.controller.toggleMenuVisible(Mojo.Menu.commandMenu);
}

// handler for commands from our command menu "undo" button
	/*
		Menu commands are now handled using the "chain of command" mechanism.
		If your scene assistant implements handleCommand, it will be called with
		the mojo-command events generated by menuitems.
		
		The chain of command is a hierarchical command handling scheme that maintains a stack of handlers
		for the current stage and another for the current scene.  Other events are also sent through the
		chain of command (mojo-back, for example).
		
		As with regular HTML events, you can use event.stopPropagation() to halt the event's propagation 
		through the chain, and event.preventDefault() to prevent any default behavior from occurring
		(and event.stop() that does both).
		
	*/
CommandMenuAssistant.prototype.handleCommand = function(event) {
	if(event.type == Mojo.Event.command) {
		switch(event.command)
		{
			case 'cmd-1':
				this.controller.get('message').innerText = ('menu button 1 pressed')
			break;
			case 'cmd-2':
				this.controller.get('message').innerText = ('menu button 2 pressed')
			break;
			default:
				//Mojo.Controller.errorDialog("Got command " + event.command);
			break;
		}
	}
}
CommandMenuAssistant.prototype.activate = function(event) {
	/* put in event handlers here that should only be in effect when this scene is active. For
	   example, key handlers that are observing the document */
}


CommandMenuAssistant.prototype.deactivate = function(event) {
	/* remove any event handlers you added in activate and do any other cleanup that should happen before
	   this scene is popped or another scene is pushed on top */
}

CommandMenuAssistant.prototype.cleanup = function(event) {
	/* this function should do any cleanup needed before the scene is destroyed as 
	   a result of being popped off the scene stack */
	Mojo.Event.stopListening(this.controller.get('show_menu_button'),Mojo.Event.tap, this.showMenu);		  
	Mojo.Event.stopListening(this.controller.get('hide_menu_button'),Mojo.Event.tap, this.hideMenu);
	Mojo.Event.stopListening(this.controller.get('toggle_menu_button'),Mojo.Event.tap, this.toggleMenu);  
	  
}