/* Copyright 2009 Palm, Inc.  All rights reserved. */

var MenuController = Class.create ({
	initialize: function() {
		
	}
});	

MenuController.setupTextColorMenu = function() {
	// insert our color menu item into the Edit submenu for richtext
	var foreColorMenu = {label:$L('Text Color'), command:'forecolor', checkEnabled:true};
	Mojo.Menu.styleEditItem.items.push(foreColorMenu);	
};

MenuController.handleTextColorMenu = function(sceneAssistant) {
	var controller = sceneAssistant.controller;
	var onselect = function(color){
		controller.document.execCommand("ForeColor", false, color);
	};
	var colorPicker = new ColorPickerDialogAssistant(sceneAssistant, onselect);
	colorPicker.show();
};

MenuController.showComposeView = function(email, login, scene, commandMenu) {
 	var newEmail = email || new Email();
	var composeReadyFunc;
	
	if (login) {
		newEmail.login = login;
	}
	
	// Disable compose/reply items until compose stage is opened.
	if(scene && commandMenu) {
		this._configureComposeCommands(scene, commandMenu, true);
		composeReadyFunc = this._configureComposeCommands.bind(this, scene, commandMenu, false);
	}
	
	AppAssistant.openComposeStage(newEmail, composeReadyFunc);
};

// Search the given menu model for compose commands, and change their enable state to match 'disabled'
MenuController._configureComposeCommands = function(scene, menu, disabled) {
	var i, items;
	
	// If scene has no assistant, assume it was popped and we should not try to change its menu model.
	if(!scene.assistant) {
		Mojo.Log.info("_configureComposeCommands: Scene appears to have been popped, returning.");
		return;
	}
	
	// look for compose-related menu items, and set their enable state appropriately.
	items = menu.items;
	for(i=0; items && i<items.length; i++) {
		// Look up the menu item's command string in our hash table to see if it opens a compose stage.
		if(this._composeCommands[items[i].command]) {
			items[i].disabled = disabled;
		}
	}
	
	scene.modelChanged(menu);
};

// A list of menu commands to be temporarily disabled while waiting for a compose stage to open.
MenuController._composeCommands = {
	'reply': true,
	'replyAll': true,
	'forward': true,
	'compose': true
};

MenuController.showHelp = function() {
	var x = new Mojo.Service.Request("palm://com.palm.applicationManager", {
		method: "open",
		parameters: {
			id: 'com.palm.app.help',
			params: {
				target: "http://help.palm.com/email/index.html"
			}
		}
	}); 
};

MenuController.showPrefs = function(stageController) {
	stageController.pushScene('accountmanager');
};
