function Menu()
{
    /* Bevat alle items die op dit moment open staan */
    this.openedItems = new Array();

    /* Initialiseert alle links binnen het menu met de onclick handler */
    this.initialize = function(id)
    {
        this.rootId = id;
        
        var as = document.getElementById(id).getElementsByTagName('A');
        for (var i=0; i<as.length; i++)
        {
            var a = as[i];
            if (!a.parentNode.className.match(/noSubmenu/))
            {
                a.onclick = function(event) { return leftMenu.openNode(event); };
            }
        }
    };
    
    /* Opent een node waarop geklikt is */
    this.openNode = function(evt)
    {
        var event = window.event || evt;
        var a = event.srcElement || event.currentTarget;
        var li = a.parentNode;
        
        this.flipLiNode(li);
        
        return true;
    };
    
    /* Schakelt de status van een <LI> element */
    this.flipLiNode = function (li)
    {
        if (li.className.match(/active/))
        {
            this.closeLiNode(li);
        }
        else
        {
            this.openLiNode(li);
        }    
    }
    
    this.closeLiNode = function(li)
    {
            li.className = li.className.replace(/\s?active/, '');
            this.hideSubMenu(li);
            this.removeOpenedId(li.id);
    }

    this.openLiNode = function(li, recursive)    
    {
        if (li != undefined)
        {
                li.className += ' active';
                this.showSubMenu(li);
                this.addOpenedId(li.id);
                
                if (recursive == undefined || recursive == true)
                {
                    this.closeNonParents(li);
                }
        }
    }
    
    /* Toont het eerste submenu onder een list item */
    this.showSubMenu = function(li)
    {
        var ul = this.getSubMenu(li);
        if (ul)
        {
            ul.style.display = 'block';
        }
    }
    
    /* Verbergt het eerste submenu onder een list item */
    this.hideSubMenu = function(li)
    {
        var ul = this.getSubMenu(li);
        if (ul)
        {
            ul.style.display = 'none';
        }
    }
    
    /* Haalt de eerste <UL> onder een item op */
    this.getSubMenu = function(li)
    { 
        var uls = li.getElementsByTagName('UL');
        if (uls.length > 0)
        {
            return uls[0];
        }
    };
    
    /* Controleert of een menuitem volgens de cookie al openstaat */
    this.isOpenedItem = function(id)
    {
        return this.inArray(id, this.openedItems);
    };
    
    /* Voegt een item toe aan de lijst met openstaande menuitems */
    this.addOpenedId = function(id)
    {
        if (!this.isOpenedItem(id))
        {
            this.openedItems.push(id);
            this.storeOpenedIds();
        }
    }
    
    /* Verwijderd een item uit de lijst met openstaande menuitems */
    this.removeOpenedId = function(id)
    {
        var newOpenedItems = new Array();
        
        for (var i=0; i<this.openedItems.length; i++)
        {
            if (this.openedItems[i] != id && this.openedItems[i].length)
            {
                newOpenedItems.push(this.openedItems[i]);
            }
        }
        
        this.openedItems = newOpenedItems;
        
        this.storeOpenedIds();
    }
    
    /* Slaat de openstaande menuitems op in de cookie */
    this.storeOpenedIds = function()
    {
        var idString = this.openedItems.join(',');
        
        this.setCookie('openedIds', idString, 0, '/');
    }
    
    /* Schrijf een waarde weg naar een cookie */
    this.setCookie = function(name, value, expires, path, domain, secure)
    {
            var today = new Date();
            today.setTime( today.getTime() );
            if (expires > 0)
            {
                expires = expires * 60 * 60 * 24;
            }
            
            var expires_date = new Date( today.getTime() + (expires) );
            
            document.cookie = name+'='+escape( value ) +
                ( ( expires > 0 ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
                ( ( path ) ? ';path=' + path : '' ) +
                ( ( domain ) ? ';domain=' + domain : '' ) +
                ( ( secure ) ? ';secure' : '' );
        }
            
    /* Haalt de inhoud van een cookie variabele op */
    this.getCookie = function(cookieName)
        {
        if (document.cookie.length>0)
        {
            c_start = document.cookie.indexOf(cookieName + "=");
            if (c_start!=-1)
            { 
                c_start = c_start + cookieName.length+1; 
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end==-1)
                {
                    c_end = document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start, c_end));
            } 
        }
        return '';
    }
    
    /* Klap de menuitems uit de cookie open */ 
    this.initFromCookie = function()
    {
        var ids = this.getCookie('openedIds');
        if (ids.length)
        {
            ids = ids.replace(/[,]{2,}/, ',');
            this.openedItems = ids.split(',');
            
            for (var i=0; i<this.openedItems.length; i++)
            {
                var liName = this.openedItems[i];
                if (liName.length > 0)
                {
                    this.openLiNode(document.getElementById(liName));
                }
            }
        }
    };
    
    /* Klap alle items die niet boven het huidige item liggen dicht */
    this.closeNonParents = function(li)
    {
        var parts = li.id.split(/\-/);
        var baseName = parts[0];
        var parents = new Array(li.id);

        for (var iParent = 2; iParent < parts.length; iParent++)
        {
            parents.push(baseName + '-' + parts.slice(1, iParent).join('-'));
        }
        
        for (var i=0; i<this.openedItems.length; i++)
        {
            var liName = this.openedItems[i];
            if (liName.length > 0)
            {
                if (!this.inArray(liName, parents)
                    && !liName.match(new RegExp('^' + li.id + '\-')))
                {
                    this.closeLiNode(document.getElementById(liName));
                }
            }
        }

        for (var i=0; i<parents.length; i++)
        {
            this.openLiNode(document.getElementById(parents[i]), false);
        }
    }
    
    this.inArray = function(needle, haystack)
    {
        for (var i=0; i<haystack.length; i++)
        {
            if (haystack[i] == needle)
            {
                return true;
            }
        }
        
        return false;
    }
}

var leftMenu = new Menu();