$(window).load(function(){
    var $currentMenuNode = $('#menuNodes .selectedMenuNode .menuNodeLink').parent().parent();
    var $currentSubMenuNode = $('#menuNodes .selectedSubMenuNode .subMenuNodeLink').parent().parent();

    $('.subMenuNode').mouseover(function(){
        if($currentSubMenuNode)
        {
            $($currentSubMenuNode).removeClass('selectedSubMenuNode');
        }
    });
    $('.subMenuNode').mouseout(function(){
        if($currentSubMenuNode)
        {
            $($currentSubMenuNode).addClass('selectedSubMenuNode');
        }
    });
    $('.subMenuNode a').click(function(){
        if($currentSubMenuNode)
        {
            $($currentMenuNode).removeClass('selectedSubMenuNode');
        }
        $('.subMenuNode').unbind('mouseover').unbind('mouseout');
        $(this).parent().parent().addClass('selectedSubMenuNode');
    });

    $('.menuNode').mouseover(function(){
        var subMenuId = '#sub' + $(this).attr('id');
        if($currentMenuNode && $currentMenuNode.attr('id') == $(this).attr('id'))
        {
            if($(subMenuId).length > 0 && $(subMenuId).css('display') == 'none')
            {
                $('.activeSubMenu').hide();
                $(subMenuId).show()
            }
            if($($currentMenuNode).find('.menuNodeLink').hasClass('selectedMenuNodeLink') == false)
            {
                $($currentMenuNode).find('.menuNodeLink').addClass('selectedMenuNodeLink');
            }
            return;
        }
        else if($currentMenuNode)
        {
            $($currentMenuNode).find('.menuNodeLink').removeClass('selectedMenuNodeLink');
        }
        $(this).find('.menuNodeLink').addClass('selectedMenuNodeLink');
        $('.subMenuNodes').hide();
        if($(subMenuId).length > 0)
        {
            $(subMenuId).show()
        }
    });
    $('.menuNode').mouseout(function(){
        $(this).find('.menuNodeLink').removeClass('selectedMenuNodeLink');
        $('.subMenuNodes').hide();
        $($currentMenuNode).find('.menuNodeLink').addClass('selectedMenuNodeLink');
        if($currentMenuNode && $currentMenuNode.attr('id') == $(this).attr('id'))
        {
            $($currentMenuNode).find('ul').show();
        }
        else if($currentMenuNode)
        {
            $($currentMenuNode).find('ul').show();
        }
    });
    $('#menuNodes a').mousedown(function(){
        if($currentMenuNode)
        {
            $($currentMenuNode).removeClass('selectedMenuNodeLink');
        }
        $('.menuNode').unbind('mouseover').unbind('mouseout');
    });
})

var roller = function(trigger, target, callback)
{
    $(trigger).click(function()
    {
        if(callback != null)
        {
            callback();
        }
        $(target).toggle();
        return false;
    });
}
