﻿// ------------ actions menu helper class ------------ //
Ext.ux.ActionsMenuHelper = Ext.extend(Ext.util.Observable, {

    // internal fields
    m_generalMenuItemData: null, // store these separately so we can re-initialise with additional page-specific items at any time
    m_generalMenuItemDataHeight: 0,
    m_menuItemData: null,
    m_pageSpecificMenuItemData: [],


    // standard constructor
    constructor: function(contentElementIdPrefix) {

        // args passed in
        this.m_contentElementIdPrefix = contentElementIdPrefix;

        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(menuItemData, height) {
        this.m_generalMenuItemData = menuItemData;
        this.m_generalMenuItemDataHeight = height;

        // initialize
        this.initializeInt(menuItemData);
    },

    // store menu items corresponding to tab id in associative array
    setMenuItems: function(menuItemDataId, menuItemData, height) {

        menuItemDataId = menuItemDataId.toLowerCase();
        
        var menuItemDataArray = [];

        // each item is a two-element array containing menuitemdata and height
        menuItemDataArray.push(menuItemData);
        menuItemDataArray.push(height);

        this.m_pageSpecificMenuItemData[menuItemDataId] = menuItemDataArray;

        // initialize
        this.initializeInt(menuItemData);
    },

    // activate the specified menu
    activateMenu: function(menuItemDataId, panelType, isFirstTime) {

        menuItemDataId  = menuItemDataId.toLowerCase();
        panelType       = panelType.toLowerCase();
       
        var pageSpecificMenuItemData    = this.m_pageSpecificMenuItemData[menuItemDataId];

        // move any page-specific menu items already added into the menu
        // back to placeholder from whence they came
        for (var menuItemDataIdToMove in this.m_pageSpecificMenuItemData) {
            $("#actions_menu_items > #actions_menu_items_" + menuItemDataIdToMove).appendTo("#actions_menu_items_placeholder_" + menuItemDataIdToMove);
        }

        // create menu from general and any page-specific items
        this.m_menuItemData = this.m_generalMenuItemData;
        if (pageSpecificMenuItemData !== null) {
            this.m_menuItemData = this.m_menuItemData.concat(pageSpecificMenuItemData[0]);
        }

        // append general and page-specific menu items
        $("#actions_menu_content").appendTo("#actions_menu_container");
        var menuItemsContentElement = this.m_contentElementIdPrefix + menuItemDataId;
        $("#" + menuItemsContentElement).appendTo("#actions_menu_items");

        /* total height of all menu items inc. spacer */
        var height = this.calculateHeight(menuItemDataId);

        $("#actions_menu_container").css("marginTop", (height * -1));
        $("#actions_menu_container").height(height);
        $("#actions_menu_content").height(height);

        $("#actions_menu_content").css("display", "block"); // fix for ie7 so menu doesn't push contents down while loading
    },

    // initialize the actions menu with the specified array of menu items
    initializeInt: function(items) {

        for (var i = 0; i < items.length; i++) {
            if (!items[i].isDisabled) {
                $.preloadImages(items[i].hoverImagePath, items[i].inactiveImagePath);

                // bind standard hover images
                $.bindHoverImages(
                    items[i].imageId,
                    items[i].hoverImagePath,
                    items[i].inactiveImagePath,
                    0,
                    null, null,
                    items[i].checkIsEnabled,
                    null, null, null, null);

                // bind handler hover images
                $.bindHoverImages(
                    items[i].handlerImageId,
                    items[i].hoverImagePath,
                    items[i].inactiveImagePath,
                    0,
                    null, null,
                    items[i].checkIsEnabled,
                    null, null, null, null);         
            }
        }
    },

    // set the height of the actions menu
    calculateHeight: function(menuItemDataId) {

        menuItemDataId = menuItemDataId.toLowerCase();
        
        var height = this.m_generalMenuItemDataHeight + this.m_pageSpecificMenuItemData[menuItemDataId][1];

        height += 23; /* bottom dropshadow height */
        return height;
    },

    // retrieve the margin top
    // this is the place at which the top of the menu scrolls to when closed up
    getMarginTop: function(menuItemDataId) {

        menuItemDataId = menuItemDataId.toLowerCase();
        
        var marginTop = this.calculateHeight(menuItemDataId);

        marginTop = marginTop - 44 + 2; // size of header less a bit
        marginTop = marginTop * -1;

        return marginTop;
    },

    // 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);
                }

                if (bIsEnabled) {
                    // if menu item is enabled, set as inactive image
                    if (menuItem.inactiveImagePath && menuItem.inactiveImagePath !== "") {
                        $('#' + menuItem.handlerImageId + '').attr("src", menuItem.inactiveImagePath);
                        $('#' + menuItem.handlerLinkId + '').css("cursor", "pointer");
                    }
                }
                else {
                    // if menu item is disabled, set as disabled image
                    if (menuItem.disabledImagePath && menuItem.disabledImagePath !== "") {
                        $('#' + menuItem.handlerImageId + '').attr("src", menuItem.disabledImagePath);
                        $('#' + menuItem.handlerLinkId + '').css("cursor", "default");
                    }
                }
            }
        }
    }
});