//-------------------------------------------------------------------------
// singleton class: MainMenu

var mainMenu = new function() {

    // constructor
    this.maxLevels = 5;
    this.activeList = new Array();

    // method: show
    this.show = function(event) {
        var element = getElementByEvent(event);
        if (element.id == null || element.id == "")
            return true;
        var menu = new Menu(element);
        if (menu.isTab()) {
            if (menu.id == "productcatalogs_0")
                setVisibility("select-category", false);
            this.hideAll();
            if (menu.element.className == "inactive")
                menu.element.className = "hover";
            try {
                var child = menu.getChild();
                var coords = coordUtils.getBottomLeft(menu.element).moveElement(child.element);
                child.element.style.display = "block";
                this.activate(child.id);
            } catch (e) {
            }
        } else {
            this.hideSubMenus(menu.level);
            try {
                menu.element.className = "hover";
                var child = menu.getChild(menu.level+1);
                var A = menu.element;
                var DT = A.parentNode;
                var DL = DT.parentNode;
                var A_coords = coordUtils.getTopLeft(A, true);
                var DT_coords = coordUtils.getTopRight(DT);
                var DL_coords = coordUtils.getTopRight(DL);
                var coords = new Coords(DL_coords.left-1, DT_coords.top-1);
                if (A_coords.left > 5)
                    coords.top -= 5;
                coords.moveElement(child.element);
                child.element.style.display = "block";
                this.activate(child.id);
            } catch (e) {
            }
        }
        this.activate(menu.id);
        menu.element.blur();
        return true;
    }

    // method: hide
    this.hide = function(element) {
        var menu = new Menu(element);
        if (menu.id == "productcatalogs_0")
            setVisibility("select-category", true);
        if (menu.isTab()) {
        } else {
            if (menu.element.tagName == "DIV")
                menu.element.style.display = "none";
        }
        if (menu.element.className == "hover")
            menu.element.className = "inactive";
        this.deactivate(menu.id);
        return true;
    }

    // method: hideAll
    this.hideAll = function() {
        var length = this.activeList.length;
        for (var i=0; i<length; i++) {
            this.hide(this.activeList[this.activeList.length-1]);
        }
        this.activeList = new Array();
    }

    // method: hideSubMenus
    this.hideSubMenus = function(level) {
        var length = this.activeList.length;
        for (var i=0; i<length; i++) {
            var id = this.activeList[this.activeList.length-1];
            for (var l=level; l<this.maxLevels; l++) {
                if (id != null && id.indexOf("_" + l) >= 0) {
                    try {
                        var menu = new Menu(id);
                        this.hide(menu.element);
                    } catch (e) {
                    }
                }
            }
        }
    }

    // method: isActive
    this.isActive = function(name) {
        for (var i=0; i<this.activeList.length; i++) {
            if (this.activeList[i] == name)
                return true;
        }
        return false;
    }

    // method: activate
    this.activate = function(name) {
        name = name.toString();
        if (!this.isActive(name))
            this.activeList[this.activeList.length] = name;
        return true;
    }

    // method: deactivate
    this.deactivate = function(name) {
        name = name.toString();
        var newList = new Array();
        var j=-1;
        for (var i=0; i<this.activeList.length; i++) {
            if (this.activeList[i] != name)
                newList[++j] = this.activeList[i];
        }
        this.activeList = newList;
        return true;
    }

    // method: toString
    this.toString = function() {
        var buffer = "Active Menus: ";
        for (var i=0; i<this.activeList.length; i++) {
            buffer += this.activeList[i] + " ";
        }
        return buffer;
    }


    //-------------------------------------------------------------------------
    // inner class: Menu
    function Menu(element) {

        // constructor
        var object = element;
        if (typeof element == "string")
            element = getElementById(element);
        if (element == null)
            throw "Element " + object + " not found";
        var tokens = element.id.split("_");
        this.element = element;
        this.name = tokens[0];
        this.level = 0;
        if (tokens.length >= 2)
            this.level = parseInt(tokens[1]);
        this.id = this.name + "_" + this.level;

        // method: isTab
        this.isTab = function() {
            if (this.level == 0)
                return true;
            return false;
        }

        // method: isSubmenu
        this.isSubmenu = function() {
            if (this.level > 0)
                return true;
            return false;
        }

        // method: getParent
        this.getParent = function() {
            var level = this.level;
            if (level > 0)
                level--;
            var parent = new Menu(this.name + "_" + level);
            return parent;
        }

        // method: getChild
        this.getChild = function(level) {
            if (level == null)
                level = this.level + 1;
            var child = new Menu(this.name + "_" + level);
            return child;
        }

        // method: hasChildren
        this.hasChildren = function() {
            var element = getElementById(this.name + "_" + (this.level+1));
            if (element)
                return true;
            return false;
        }

        // method: toString
        this.toString = function() {
            return "Menu: " + this.id;
        }


    }


}
