/*
 * Dynamic Menu
 * Written by Andy Peatling - http://www.cssdev.com/
 * April 1, 2006.
 */

function collapseMenu(menuId, node){
    if (!node) {
        node = jQuery('#' + menuId);
    }
    
    var found = false;
    jQuery(node).find('a').each(function(){
        if (jQuery(this).attr('href') == location.href || openedCategory == this.id) {
            //selectedNodes[selectedNodes.length] = this;
            expandAllInMainCategory(this);
            jQuery(this).parent().addClass('selected');
            found = true;
        }
    });
    
    if (!found) {
        jQuery(node).find('li:first > ul').show();
    }
}

function prepareMenu(menuId){
    jQuery('#' + menuId + ' > li > a').click(function(e) {
        toggleMenu(jQuery(this).parent().find('ul:first'), jQuery(this).attr('href'), jQuery(this).attr('followLink'))
        e.preventDefault();
    });
}

function expandSelectedNodes(menuId){
	jQuery.each(selectedNodes, function() {
        expandNode(menuId, this);
    });
}

function expandNode(menuId, node){
	var child = jQuery(node).children('ul:first');
	if (child.length > 0) {
		child.toggle();
	}
    if (node.parentNode.id != menuId) {
		var parentUl = node.parentNode;
		if (parentUl) {
			expandNode(menuId, parentUl);
		}
    }
}

function toggleMenu(node, link, followLink){
    if (!link) 
        return false;
    if (parseInt(followLink) || jQuery(node).length < 1) {
		location.href = link;
		return true;
	}
	
		
    // Collapse all nodes, and only show clicked node (when clicking top level of menu)
    /*
if (node.parentNode.parentNode.id == "menu") {
        hideTopLevels();
    }
*/
    if (jQuery(node).is(':visible')) {
        jQuery(node).hide(200);
        /*Effect.BlindUp(node, {
            duration: 0.2
        });*/
    }
    else {
        jQuery(node).show(200);
        /*Effect.BlindDown(node, {
            duration: 0.2
        });*/
    }
}

function hideTopLevels(){
    if (!document.getElementById) 
        return false;
    if (!(node = document.getElementById("sidebar_menu"))) 
        return false;
    
    if (node.childNodes.length > 0) {
        for (var i = 0; i < node.childNodes.length; i++) {
            var child = node.childNodes[i];
            for (var j = 0; j < child.childNodes.length; j++) {
                var grandchild = child.childNodes[j];
                if (grandchild.nodeName == "UL") {
                    if (grandchild.style.display == '') {
                        jQuery(grandchild).hide(200);
                        /*Effect.BlindUp(grandchild, {
                            duration: 0.2
                        });*/
                    }
                }
            }
        }
    }
}

function expandAllInMainCategory(selected) {
    selected = jQuery(selected);
    while (selected.parent().attr('id') != 'sidebar_menu') {
        selected = selected.parent();
    }
    selected.parent().find('li').children('ul').hide();
    selected.find('ul').show();
    /*selected.parent().find('li').each(function() {
        jQuery(this).children('ul').show();
    });*/
}

var openedCategory = null;
var selectedNodes = [];
jQuery(document).ready(function() {
    jQuery('#sidebar_menu ul').hide();
    collapseMenu('sidebar_menu');
    prepareMenu('sidebar_menu');
    //expandSelectedNodes('sidebar_menu');
});