﻿// ------------ actions menu helper class ------------ //
Ext.ux.ActionsMenuHelper = Ext.extend(Object, {   
    
    // internal fields
    m_menuItemData: null,
    m_defaultMarginTop: 0,


    // standard constructor
    constructor: function() {        
        Ext.ux.ActionsMenuHelper.superclass.constructor.call(this);
    },
    
    // standard initializer
    initComponent: function() {
        Ext.ux.ActionsMenuHelper.superclass.initComponent.apply(this, arguments);

        // extra args have to go here as base class initComponent doesn't get called for some reason
        var config = {};

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
    },
    
    
    // initialize the actions menu with the specified array of menu items
    initialize: function(menuItems, height) {
        this.m_menuItemData = menuItems;
        
        for (var i = 0; i < menuItems.length; i++) {
            if (!menuItems[i].isDisabled) {
                $.preloadImages(menuItems[i].hoverImagePath, menuItems[i].inactiveImagePath);

                // bind hover images
                $.bindHoverImages(
                    menuItems[i].imageId,
                    menuItems[i].hoverImagePath,
                    menuItems[i].inactiveImagePath,
                    0,
                    null, null,
                    menuItems[i].checkIsEnabled,
                    null, null, null, null);
            }
        }

        /* total height of all menu items inc. spacer */
        this.setHeight(height);

        $("#actions_menu_content").appendTo("#actions_menu_container");
    },
    
    // set the height of the actions menu
    setHeight: function(height) {
        height += 23; /* bottom dropshadow height */

        this.m_defaultMarginTop = (height * -1);
        $("#actions_menu_container").css("marginTop", this.m_defaultMarginTop);

        $("#actions_menu_container").height(height);
        $("#actions_menu_content").height(height);
    },

    // retrieve the default margin top
    getDefaultMarginTop: function() {
        return this.m_defaultMarginTop;
    },

    // called before the menu is shown
    beforeShow: function() {

        // set the enabled state of each menu item (if an enabled status handler is defined)
        for (iMenuItem = 0; iMenuItem < this.m_menuItemData.length; iMenuItem++) {

            // only do this for menu items with javascript handlers
            if((this.m_menuItemData[iMenuItem].handler !== null) && (this.m_menuItemData[iMenuItem].handler !== "")) {
                
                var bIsEnabled      = true;
                var menuItem        = this.m_menuItemData[iMenuItem];
                var checkIsEnabled  = this.m_menuItemData[iMenuItem].checkIsEnabled;

                if ((checkIsEnabled !== null) && (checkIsEnabled !== "")) {
                    bIsEnabled = executeFunctionByName(checkIsEnabled, window);
                }
                
                $('#' + menuItem.linkId + '').removeAttr("onclick");    // remove onclick attribute (bound on server-side)
                $('#' + menuItem.linkId + '').unbind("click");          // remove click attribute (bound using jQuery)

                if (bIsEnabled) {
                    // if menu item is enabled, set as inactive image
                    if (menuItem.inactiveImagePath && menuItem.inactiveImagePath !== "") {
                        $('#' + menuItem.imageId + '').attr("src", menuItem.inactiveImagePath);
                        $('#' + menuItem.linkId + '').css("cursor", "pointer");
                        $('#' + menuItem.linkId + '').bind("click",
                            delegateWithArguments(this, executeFunctionByName, menuItem.handler, window)); // bind click handler    
                    }
                }
                else {
                    // if menu item is disabled, set as disabled image
                    if (menuItem.disabledImagePath && menuItem.disabledImagePath !== "") {
                        $('#' + menuItem.imageId + '').attr("src", menuItem.disabledImagePath);
                        $('#' + menuItem.linkId + '').css("cursor", "default");
                    }
                }
            }
        }
    }
});