/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Copyright: Daniel Cohen Gindi, and WeBuildIt */
/* Version: 2009-07-19 */

function floatingMenuManager() { this.create(); };

floatingMenuManager.prototype = {

    create: function() {
        var _thisObj = this;
        this._thisObj = this;
        this._FMouseOver = function() { _thisObj._clearMenuTimer(); };
        this._FMouseOut = function() { _thisObj._setMenuTimer(); };
        this._FCloseMenu = function() { _thisObj._closeMenu(); };
        this._create();
    },

    _thisInstance: this,
    _curSelMenu: null,
    _menuTimer: null,
    _menuHideTimeOut: 100,
    _elMenuItemParent: null,
    _elParentOriginalClass: null,
    _parentClassOnHover: null,
    _parentClassOnHover_add: false,
    _offset: new Array(0, 0),
    _baseNameMenuItem: 'menuItem',
    _baseNameFloatingMenu: 'subMenu',
    _alignBottom: false,
    _alignRight: false,
    _alignHCenter: false,
    _absolutePopupWidth: null,
    eventOnShow: null,
    eventOnHide: null,

    _create: function() { },
    isVisible: function() { return !!this._curSelMenu; },
    setHideTimeOut: function(timeout) { this._menuHideTimeOut = timeout; },
    setParentClassOnHover: function(cls, add) { this._parentClassOnHover = cls; this._parentClassOnHover_add = add; },
    setMenuItemBaseName: function(baseNameMenuItem) {
        this._baseNameMenuItem = baseNameMenuItem;
    },
    setFloatingMenuBaseName: function(baseNameFloatingMenu) {
        this._baseNameFloatingMenu = baseNameFloatingMenu;
    },
    setOffset: function(x, y, alignBottom, alignRight, alignHCenter, absolutePopupWidth) {
        this._offset = new Array(x, y);
        if (alignBottom) this._alignBottom = true;
        if (alignRight) this._alignRight = true;
        if (alignHCenter) this._alignHCenter = true;
        if (typeof (absolutePopupWidth) == typeof (1) || absolutePopupWidth == '100%') this._absolutePopupWidth = absolutePopupWidth;
    },
    _clearMenuTimer: function() {
        if (this._menuTimer) { clearTimeout(this._menuTimer); this._menuTimer = null; }
    },
    _setMenuTimer: function() {
        this._clearMenuTimer();
        this._menuTimer = setTimeout(this._FCloseMenu, this._menuHideTimeOut);
    },
    _closeMenu: function() {
        this._clearMenuTimer();
        if (this._curSelMenu) {
            ajax.events.unregister(this._curSelMenu, 'mouseover', this._FMouseOver);
            ajax.events.unregister(this._curSelMenu, 'mouseout', this._FMouseOut);
            this._curSelMenu.style.display = 'none';
            this._curSelMenu.style.visibility = 'hidden';
            this._curSelMenu = null;
            if (this._elParentOriginalClass != null) this._elMenuItemParent.className = this._elParentOriginalClass;

            if (this.eventOnHide) {
                try { this.eventOnHide(this); } catch (E) { }
            }
        }
    },
    showMenu: function(idx) {
        var el = ajax.$(this._baseNameFloatingMenu + idx);
        if (this._curSelMenu != el) { this._closeMenu(); this._curSelMenu = el; }
        else { this._clearMenuTimer(); return; }

        this._elMenuItemParent = ajax.$(this._baseNameMenuItem + idx);
        if (el == undefined) el = null;
        this._curSelMenu = el;

        if (this._curSelMenu != null) {
            this._curSelMenu.style.position = 'absolute';
            var elParentOffset = ajax.layout.offset(this._elMenuItemParent);
            if (this._absolutePopupWidth == '100%') this._curSelMenu.style.width = ajax.layout.width(this._elMenuItemParent) + 'px';
            if (this._alignHCenter) {
                var popW = this._absolutePopupWidth;
                if (popW == '100%') popW = null;
                if (!popW) popW = ajax.layout.width(this._curSelMenu);
                if (this._alignRight) this._curSelMenu.style.right = ((window.getClientWidth() - elParentOffset[0]) - ajax.layout.width(this._elMenuItemParent) - (popW - ajax.layout.width(this._elMenuItemParent)) / 2 + this._offset[0]) + 'px';
                else this._curSelMenu.style.left = (elParentOffset[0] - (popW - ajax.layout.width(this._elMenuItemParent)) / 2) + this._offset[0] + 'px';
            }
            else if (this._alignRight) this._curSelMenu.style.right = ((window.getClientWidth() - elParentOffset[0]) - ajax.layout.width(this._elMenuItemParent) + this._offset[0]) + 'px';
            else this._curSelMenu.style.left = (elParentOffset[0] + this._offset[0]) + 'px';
            if (this._alignBottom) this._curSelMenu.style.bottom = ((window.getClientHeight() - elParentOffset[1]) + this._offset[1]) + 'px';
            else this._curSelMenu.style.top = (elParentOffset[1] + this._offset[1]) + 'px';
            this._curSelMenu.style.display = '';
            this._curSelMenu.style.visibility = 'visible';
            this._curSelMenu.style.zIndex = '5000';

            if (this._parentClassOnHover != null) {
                this._elParentOriginalClass = this._elMenuItemParent.className;
                if (this._parentClassOnHover_add)
                    this._elMenuItemParent.className += ' ' + this._parentClassOnHover;
                else this._elMenuItemParent.className = this._parentClassOnHover;
            }

            ajax.events.register(this._curSelMenu, 'mouseover', this._FMouseOver);
            ajax.events.register(this._curSelMenu, 'mouseout', this._FMouseOut);

            if (this.eventOnShow) {
                try { this.eventOnShow(this); } catch (E) { }
            }
        }
    },
    hideMenu: function() { this._setMenuTimer(); }
};