function toggleMenuPrefix(element, newPrefixText, oldPrefixText)
{
    if (element.hasClass('menuTree'))
    {
        element.find('.menuTreeHead').each(function()
        {
            var menuTree = $(this);
            var html = menuTree.html().replace(oldPrefixText, newPrefixText);
            var dex = html.indexOf(newPrefixText);
            if (dex < 0)
            {
                html = ' ' + newPrefixText + ' ' + html;
            }
            menuTree.html(html);
        });
    }
}

function initMenus()
{
    /*$('ul.menu ul').hide();*/
    
    $.each($('ul.menu'), function()
    {
        var cookie = $.cookie(this.id);        
        if(cookie != null && String(cookie).length < 1) 
        {
            $('#' + this.id + '.expandfirst ul:first').show();
        }
        else
        {
            $('#' + this.id + ' .' + cookie).next().show();            
            $('#' + this.id + ' .' + cookie).find('.menuTopHead').css('font-weight', 'bold');
        }
    });

    $('ul.menu li a').click( function()
    {
        var checkElement = $(this).next();        
        var parent = this.parentNode.parentNode.id;
        
        $.cookie(parent, null);

        if($('#' + parent).hasClass('noaccordion'))
        {
            if((String(parent).length > 0) && (String(this.className).length > 0))
            {
                if($(this).next().is(':visible'))
                {
                    $.cookie(parent, null);
                }
                else
                {
                    $.cookie(parent, this.className);
                }
                $(this).next().slideToggle('normal');
            }
        }            

        if((checkElement.is('ul')) && (checkElement.is(':visible')))
        {        
            /*if($('#' + parent).hasClass('collapsible'))
            {            
                $('#' + parent + ' ul:visible').slideUp('normal');
                
                toggleMenuPrefix($(this.parentNode), '+', '-');
                $('#' + parent).find('.menuTopHead').css('font-weight', 'normal');
            }
            return false;*/
        }
        
        if((checkElement.is('ul')) && (!checkElement.is(':visible')))
        {
            var parObj = $('#' + parent + ' ul:visible')                        
            parObj.parent().find('.menuTopHead').css('font-weight', 'normal');            
            toggleMenuPrefix(parObj.parent(), '+', '-');
            parObj.slideUp('normal');
                       
            if((String(parent).length > 0) && (String(this.className).length > 0))
            {
                $.cookie(parent, this.className);
            }
        
            checkElement.slideDown('normal');            
            checkElement.parent().find('.menuTopHead').css('font-weight', 'bold');
            
            
            toggleMenuPrefix($(this.parentNode), '-', '+');
            
            return false;
        }
    });
    
        
    $('li.menuTree ul.menu').each(function()
    {
        var menuTree = $(this);       
        if (menuTree.is(':visible'))
        {
            /*var parentHeader = $('#' + this.parentNode.id + ' .menuTreeHead')
            
            var html = parentHeader.html().replace('+', '-');
            if (html.indexOf('-') < 0)
            {
                html = ' - ' + html;
            }
            parentHeader.html(html);*/
        }
    });
}

$(document).ready(
function()
{
    initMenus();
});
