/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

function initMenus() {
    var options = { path: '/promep', expires: 999 };
    
    jQuery('ul.menu ul').hide();
    jQuery.each(jQuery('ul.menu'), function(){
        var cookie = jQuery.cookie(this.id);
        if(cookie === null || String(cookie).length < 1) {
            jQuery('#' + this.id + '.expandfirst ul:first').show();
        }
        else {          
            jQuery('#' + this.id + ' .' + cookie).next().show();
        }
    });
    jQuery.each(jQuery('ul.menu li ul li a'), function(){
        var cookie = jQuery.cookie('menu_selItem');
        if(cookie === null || String(cookie).length < 1) {
            // do nothing
        } else if (cookie == this.id){
            jQuery(this).addClass('selectedMenu');
        }        
    });
    jQuery('ul.menu li ul li a').click(
        function() {
            // var key = this.parentNode.parentNode.parentNode.parentNode.id + '_selItem'
            var key = 'menu_selItem';

            // replace cookie
            jQuery.cookie(key, this.id, options);
        }
    );
    jQuery('ul.menu li a').click(
        function() {
            var checkElement = jQuery(this).next();
            var parent = this.parentNode.parentNode.id;

            if(jQuery('#' + parent).hasClass('noaccordion')) {
                if((String(parent).length > 0) && (String(this.className).length > 0)) {
                    if(jQuery(this).next().is(':visible')) {
                        jQuery.cookie(parent, null, options);
                    }
                    else {
                        jQuery.cookie(parent, this.className, options);
                    }
                    jQuery(this).next().slideToggle('normal');
                }               
            }
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                if(jQuery('#' + parent).hasClass('collapsible')) {
                    jQuery('#' + parent + ' ul:visible').slideUp('normal');
                }
                return false;
            }
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                jQuery('#' + parent + ' ul:visible').slideUp('normal');
                if((String(parent).length > 0) && (String(this.className).length > 0)) {
                    jQuery.cookie(parent, this.className, options);
                }
                checkElement.slideDown('normal');
                return false;
            }
        }
    );
}
jQuery(document).ready(function() {initMenus();});

