/*
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 'slider_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="slider_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>
    <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>
</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

Amended in his blogpost: http://www.i-marco.nl/weblog/archive/2008/05/08/simple_jquery_accordion_menu__
    - "If you want to use this commercially, just go ahead. It's better than all these people emailing me about compensation and then never get back to me. A donation is highly appreciated (at paypal@i-marco.nl) but don't feel obliged. But please, don't write me email promising one when you aren't gonna actually do it. Sorry if this sounds harsh but I'm really disappointed in how people dealt with this."

Modified by Doug Jones, Assurance Technologies, LLC 2009

:Disabled Cookie Functionality:
    - commented out all jquery cookie plugin usage due to bugginess
    - wrote code in left_menu.html to assign unique id to each submenuheader <a>
        that can be used to identify the open submenu.   Problem was that instead
        of overwriting the cookie value with each new link click, multiple
        cookies were being added with same name (slider_menu's id: "side_menu")
        and it only used the first cookie it came across to determine which submenu
        should be expanded on page load.

Refactored and Features Added - Eli Collins, Assurance Technologies, LLC 2009

	* renabled cookies after glitch dissapeared (ampersands in id?)
    * refactored to simplify code a little bit
    * trying out code which jumps to page, even for submenu headers.
    * bugfix: can now handle links at top of menu, not just in submenu

*/

function initMenus() {
    //hide all submenus initially
    $('ul.slider_menu ul').hide();

    //show default menu based on cookie
    $.each($('ul.slider_menu'), function(){
        var cookie = $.cookie(this.id);
        if(cookie) {
            $('#' + this.id + ' #' + cookie).next().show();
        }
        else {
            $('#' + this.id + '.expandfirst ul:first').show();
        }
    });

    //attach callback to alter menu when link is clicked
    $('ul.slider_menu li > a').click(
        function() {
            // "this" - link element that was clicked
            //FIXME: what if this.id isn't defined?

            // "rootNode" - root ul for this menu
            var rootNode = this.parentNode;
            while(rootNode && !$(rootNode).hasClass('slider_menu')){
                rootNode = rootNode.parentNode;
            }
            //FIXME: what to do if we ever roll off the end w/o finding root node?
            var rootId = rootNode.id;
            //FIXME: what if rootId isn't defined?

            // "menuNode" - submenu ul that is paired with this link
            var menuNode = $(this).next();
            if (!menuNode[0]){
                return true;
            }
//            if (!menuNode.is("ul")){
//                //FIXME: coder isn't using ULs!
//                return true;
//            }

            //prep func to redirect to page if it has target set
            var target = this.href;
            var jumpToPage = null;
/*            if(target && target != "#" && target != document.location){
                jumpToPage = function () {
                    document.location = target;
                }
            }*/

            if(menuNode.is(':visible')){
                //close submenu only if "collapsible" option is set...
                if($(rootNode).hasClass('collapsible')) {
                    menuNode.slideUp("normal");
                }else if(jumpToPage){
                    jumpToPage();
                }
            }else{
                //close all other submenus, and open this one

                //remember this for next time
                //TODO: could remember open state of ALL of them
                $.cookie(rootId, null);
                $.cookie(rootId, this.id, { path: '/' });

                //close all other submenus
                if(!$(rootNode).hasClass('noaccordion')) {
                    $('#' + rootId + ' ul:visible').slideUp('normal');
                }

                //open this submenu
                menuNode.slideDown('normal', jumpToPage);
            }
            return false;
        }
    );
}

//function assign_unique_submenu_ids() {
//    //assign generic id's to all submenu <ul>'s without id's
//    var submenu_id = 1;
//    $('ul#side_menu').find('ul').each(function(i) {
//            if ($(this).attr('id') == "") {
//                $(this).attr('id', ("submenu_"+submenu_id));
//                submenu_id++;
//                //alert("Found ul " + i + " ; id=" + $(this).attr('id'));
//            }
//        }
//    );
//}

$(document).ready(function() {
    initMenus();
    //assign_unique_submenu_ids();
    }
);
