/**
 *
 * JaxMenu Plugin
 * Requires the Jax JavaScript Library
 *
 * Prototype for the JaxMenus object and its methods and properties.
 * The menus parameter is a a multidimensional array that holds the
 * name of a menu element ID, as well an X, Y coordinate adjustment
 * value. The optional pgW parameter is a compensation for a centered
 * page of a certain width. The 'gutter' property is calculated from
 * this to maintain the menus correctly centered in the middle of
 * the page.
 *
 * @param Array menus = new Array(new Array('menu1Nav', 50, 125),
 *                                new Array('menu2Nav', 150, 125),
 *                                new Array('menu3Nav', 200, 125));
 * @param int   pgW = 980;
 */

function JaxMenu(menus, pgW) {

    this.menus = menus;
    this.menuFx = new JaxFx();
    this.pgWidth = (pgW != null) ? pgW : 0;
    this.gutter = 0;
    this.menuTop = 0;
    this.scrollOffset = 0;
    this.menuOpen = '';

    // method to initialize the menus
    this.initMenus = function() {

        // Set the gutter of the page
        this.gutter = (this.pgWidth != 0) ? ((document.body.clientWidth - this.pgWidth) / 2) : 0;

        // Establish the menu positions
        for (i = 0; i < this.menus.length; i++) {

            this.menuTop = this.menus[i][2];
            div = document.getElementById(this.menus[i][0]);
            div.style.top = this.menuTop + 'px';
            div.style.left = (this.gutter + this.menus[i][1]) + 'px';

        }

    }

    // Method to show a menu
    this.showMenu = function(div) {

        // Re-initialize the menus based on any new window sizing or scrolling
        this.initMenus();

        // Set the menuOpen variable and display the current menu
        this.menuOpen = div;
        this.menuFx.show(div);

    }

    // Method to hide a menu
    this.hideMenu = function(div) {
        this.menuOpen = '';
        this.menuFx.hide(div);
    }

    // Function to hide all menus
    this.hideAll = function(div) {
        for (i = 0; i < this.menus.length; i++) {
            this.menuFx.hide(this.menus[i][0]);
        }
    }

    // Function to check for the mouse outside of a menu and close that menu
    this.checkMenu = function(event) {

        if (this.menuOpen != '') {
            // Set the boundaries of the current open menu (top X,Y and bottom X,Y) to detect if the mouse is outside of the menu
            // There's about a 30px pad to allow for some cushion as the mouse rolls out of the menu area
            this.currentMenuOpen = document.getElementById(this.menuOpen);
            this.topXCoor = this.currentMenuOpen.offsetLeft - 300;
            this.topYCoor = this.currentMenuOpen.offsetTop - 100 - this.scrollOffset;
            this.bottomXCoor = this.currentMenuOpen.offsetLeft + this.currentMenuOpen.offsetWidth + 100;
            this.bottomYCoor = this.currentMenuOpen.offsetTop + this.currentMenuOpen.offsetHeight + 100 - this.scrollOffset;

            if (navigator.appName.indexOf('Microsoft') != -1) {
                if ((window.event.clientX > this.bottomXCoor) || (window.event.clientX < this.topXCoor) || (window.event.clientY < this.topYCoor) || (window.event.clientY > this.bottomYCoor)) {
                    this.menuFx.hide(this.menuOpen);
                }
            } else {
                if ((event.clientX > this.bottomXCoor) || (event.clientX < this.topXCoor) || (event.clientY < this.topYCoor) || (event.clientY > this.bottomYCoor)) {
                    this.menuFx.hide(this.menuOpen);
                }
            }

        }

    }

}

