/*
 * Beard Navigation Bar
 *
 * simple one-level menu using jQuery (http://www.jquery.com)
 *
 * Written by Markus Hall, Beard (http://www.beard.se)
 *
 */

var selected = null;

var selectedTop = null;
var selectedSub = null;
var overTheSub = false;
var hoverTop = null;

function setupMenu()
{
    // Set absolute left position for LI:s of child UL:s
    // as relative positioning did not work in IE
    $(".NavBar ul li ul").each(function(){
        var w2 = 0;
        $('li', $(this).parent()).each(function(){
                // alert('set "' + $(this).html() + '" left = "' + w2);
                $(this).left(w2);
                w2 += $(this).width();
        });
    });

    $(".NavBar>ul>li").filter(':not(#Filler)').topHoverClass();
    $(".NavBar>ul>li>ul>li").filter(':not(#Filler)').subHoverClass();

    // Set selected
    $(".NavBar A").checkCurrent();
};

$.fn.checkCurrent = function()
{
    this.each(function(){
        if ($(this).href() == document.location.href)
        {
            var s = this.parentNode;

            var isTopLevel = $('.NavBar', $(s.parentNode.parentNode.parentNode)).html();
            if (isTopLevel)
            {
                selectedTop = s;
            }
            else
            {
                selectedTop = s.parentNode.parentNode;
                selectedSub = s;
            }
        }
    });

    if (selectedTop != null)
    {
        $(selectedTop).showSubMenu(true);
        $(selectedTop).setHover(true);
        $(selectedTop).setInPath(true);
    }

    if (selectedSub != null)
    {
        $(selectedSub).setHover(true);
        $(selectedSub).setInPath(true);

        $(selectedTop).setHover(false);
    }
}

// Show or hide lines enclosing the selected <LI>
$.fn.setInPath = function(state)
{
    if (state == true)
    {
        $('A:first', $(this).prev()).addClass('LeftOfSelected');
        $('A:first', $(this).next()).addClass('RightOfSelected');
        $('A:first', $(this)).addClass('SelectedLeft');
        $('A:first', $(this)).addClass('SelectedRight');
    }
    else
    {
        $('A:first', $(this).prev()).removeClass('LeftOfSelected');
        $('A:first', $(this).next()).removeClass('RightOfSelected');
        $('A:first', $(this)).removeClass('SelectedLeft');
        $('A:first', $(this)).removeClass('SelectedRight');
    }
}

// Show or hide hover background for the selected <LI>
$.fn.setHover = function(state)
{
    if (state == true)
    {
        $(this).addClass('sfHover');

        // Set menu image for use with background
        var img = $("img", $(this)).get(0);

        if (img)
        {
            var s = img.src;
            if (s)
            {
                s = s.replace('_2.gif', '_1.gif');
                img.src = s;
            }
        }
    }
    else
    {
        $(this).removeClass('sfHover');

        // Set menu image for use with background
        var img = $("img", $(this)).get(0);

        if (img)
        {
            var s = img.src;
            if (s)
            {
                s = s.replace('_1.gif', '_2.gif');
                img.src = s;
            }
        }
    }
}

// Show or hide child <UL> for the selected <LI>
$.fn.showSubMenu = function(state)
{
    if (state == true)
    {
        $(this).addClass('showChild');
    }
    else
    {
        $(this).removeClass('showChild');
    }
}

$.fn.topHoverClass = function() {
    return this.each(function(){
    $(this).hover(
        function() {

            // Pekar vi på den som är vald i toppmenyn?
            if (!equals(this, selectedTop))
            {
                // Nä, fäll ihop den toppmeny som är vald
                if (selectedTop != null)
                {
                    $(selectedTop).showSubMenu(false);
                    $(selectedTop).setHover(false);
                }

                // Och visa undermenyn för denna
                $(this).showSubMenu(true);
                $(this).setInPath(true);
            }

            // Alltid visa hover för den man pekar på...
            // ..förtom om man har pekat direkt på en utfälld
            // undermeny, utan att passera toppmenyn (~ovanifrån).
            if (!overTheSub)
            {
                $(this).setHover(true);

                // Ta bort eventuell markering i submenyn
                $('li', $(this)).each(function() { $(this).setHover(false); });
            }
            hoverTop = this;
        },
        function() {
            // Lämnar vi nu den som är vald i toppmenyn?
            if (equals(this, selectedTop))
            {
                // Ja, återställ markering till undermenyn om något
                // är valt däri
                if (selectedSub != null)
                {
                    $(selectedSub).setHover(true);

                    // Sluta hovra i toppmenyn
                    $(this).setHover(false);
                }
            }
            else
            {
                // Nä, fäll ihop denna toppmeny
                $(this).showSubMenu(false);
                $(this).setInPath(false);

                // Och visa undermenyn...
                $(selectedTop).showSubMenu(true);

                // och hovra den som är vald om det inte är
                // något i undermenyn som är valt
                if (selectedSub == null)
                    $(selectedTop).setHover(true);

                // Sluta hovra
                $(this).setHover(false);
            }
            hoverTop = null;

            // Sätt hover för eventuell vald undermeny
            $(selectedSub).setHover(true);
        }
    );
    });
};

$.fn.subHoverClass = function() {
    return this.each(function(){
    $(this).hover(
        function() {
            overTheSub = true;

            // Ta bort hover för meyvalet (ex. "our dogs") till denna undermeny
            $(this).parent().parent().setHover(false);

            // Ta bort hover för eventuell vald undermeny
            if (selectedSub != null)
                $(selectedSub).setHover(false);

            // Alltid visa hover för den man pekar på...
            $(this).setHover(true);
            $(this).setInPath(true);
        },
        function() {
            $(hoverTop).setHover(true);

            // Sätt tillbaka hover för menyvalet (ex. "our dogs")
            // till denna undermeny
            if (equals(selectedTop, $(this).parent().parent()))
                $(this).parent().parent().setHover(true);

            // Sluta hovra
            $(this).setHover(false);

            if (!equals(this, selectedSub))
                $(this).setInPath(false);

            overTheSub = false;
        }
    );
    });
};

function equals(jq1, jq2)
{
    return $(jq1).html() == $(jq2).html();
}

