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

var MenuHandler = {
    createAppMenu: function(that, menuModel){
    
        that.goToPrefs = this.goToPrefs;
        that.handleCommand = this.handleCommand;
        var items = $A([Mojo.Menu.editItem]);
        if (menuModel) 
            items = items.concat(menuModel);
        items.push(MenuHandler.prefsItem);
        items.push({
            label: $LL('Help'),
            command: Mojo.Menu.helpCmd
        });
        that.appMenuModel = that.appMenuModel ||
        {
            visible: true,
            label: 'Contacts Menu',
            items: items
        };
        that.appMenuModel.items = items;
        that.controller.setupWidget(Mojo.Menu.appMenu, {
            omitDefaultItems: true
        }, that.appMenuModel);
    },
    
    goToPrefs: function(){
        this.controller.stageController.pushScene('prefs');
    },
    
    
    
    handleCommand: function(event){
        // handle menu button command events
        if (event.type == Mojo.Event.command) {
            if (event.command == Mojo.Menu.prefsCmd) {
                event.stop();
                this.goToPrefs();
                return;
            }
            if (event.command === MenuHandler.deleteContact) {
                event.stop();
                this.handleDelete();
                return;
            }
            if (event.command === MenuHandler.addToLauncher) {
                event.stop();
                this.handleAddToLauncher();
                return;
            }
            if (event.command === MenuHandler.noop) {
                event.stop();
                return;
            }
            
            if (event.command === MenuHandler.linkProfiles) {
                event.stop();
                this.handleClipMore();
                return;
            }
            if ((typeof event.command) === "function") {
                event.command();
                event.stop();
            }
            
            if (event.command === MenuHandler.editContact) {
                this.handleEdit();
                event.stop();
                return;
            }
            
            if (event.command === MenuHandler.setSpeedDial) {
                this.setSpeedDial();
                event.stop();
                return;
            }
            
            if (event.command === MenuHandler.addReminder) {
                this.handleReminder();
                event.stop();
                return;
            }
			
			if(event.command === MenuHandler.addToSIM){
				this.addToSIM();
				event.stop();
				return;
			}
			
			if(event.command === MenuHandler.copyContact){
				this.copyDetailsToClipboard();
				event.stop();
				return;
			}
        }
    },
}
MenuHandler.editContact = "edit-contact";
MenuHandler.deleteContact = "deleteContact";
MenuHandler.preferences = "preferences";
MenuHandler.noop = "noop";
MenuHandler.addToLauncher = "addToLauncher";
MenuHandler.linkProfiles = "linkProfiles";
MenuHandler.setSpeedDial = "setSpeedDial";
MenuHandler.addReminder = "addReminder"
MenuHandler.addToSIM = "addToSIM";
MenuHandler.copyContact = "copyContact";
MenuHandler.prefsItem = {
    label: $L("Preferences & Accounts"),
    command: Mojo.Menu.prefsCmd,
    disabled: true
}

MenuHandler.launchHelp = function(){
    new Mojo.Service.Request("palm://com.palm.applicationManager", {
        method: "open",
        parameters: {
            id: 'com.palm.app.help',
            params: {
                target: "http://help.palm.com/contacts/index.html"
            }
        }
    });
}
