$(document).ready(
    function()
    {
        footnoteToggle();
        
        initZebraTables();
        
        initToggleElements(
            '#content table td h2', // clickProxySelector
            '+ span'                // toggledElementSelector)
        );

        initFoldables();
        
        initHoveralbes();

        /* Preload Images for Navi-Mouseovers
        /* nav-quick must be the first item in array
        --------------------------------------------- */
        var navigations = [
            $('ul#nav-quick-lev0 li a'),
            $('ul#nav-top-lev0 ul.nav-top-lev1 li a')
        ]
        preloadNavigation(navigations);
        
        initFooterTooltip();
        
        initFoldNavigation();
        
        initToggleNaviElements(
            '.wsc-nav-left-lev0',
            '> li > a',                 // clickProxySelector
            '+ ul'                      // toggledElementSelector)
        );
    }
);

$(window).load(
    function()
    {
        fixBorderHeight();
    }
);

function initZebraTables()
{
    $('table:not(.no-zebra) tbody tr:nth-child(even)').addClass('zebra'); // don't use :odd, use :nth-child(odd)
    //$('table tr').each(function(){$(this).find('td:first').addClass('first-td')})
    //$('table tr').each(function(){$(this).find('td:last').addClass('last-td')})
}

function initToggleElements(clickProxySelector, toggledElementSelector)
{
    var $allClickProxies = $(clickProxySelector);
    $allClickProxies
        .css({cursor: 'pointer'})
        .click(
            function()
            {
                // the clicked h2 is a proxy for another element which will be
                // hidden or displayed, search for this first because it will
                // be reused
                $element = $(this).find(toggledElementSelector);
                
                // treat clicked element  (use slideDown() instead of
                // toggle() is user is not allowed to close opened elements
                // by clicking on them)
                $element.toggle('fast');
                
                // hide all visible elements, but not the clicked element (that's the trick!)
                $allClickProxies.find(toggledElementSelector).filter(':visible').not($element).slideUp('fast');
            }
        )
        // hide all by default
        .find(toggledElementSelector).hide();
}

function initFoldables()
{
    $('.foldable > *:header:nth-child(1)')
        .click(
            function(ev)
                {
                    var $this = $(this);
                    if (this.mvno_hidden == window.undefined)
                    {
                        this.mvno_text = $this.text();
                        this.mvno_hidden = false;
                    }
                    this.mvno_hidden = !this.mvno_hidden;
                    var f = this.mvno_hidden ? 'hide' : 'show';
                    $this.nextAll()[f]();
            
                    var down = "\u25BC";
                    var right = "\u25BA";
                    $this.text((this.mvno_hidden ? right: down) + ' ' + this.mvno_text); // black down
                }
        )
        .trigger('click');
}

function initHoveralbes()
{
    $('.hoverable').hover(
        function()
        {
            $(this).addClass('hovered');
        },
        function()
        {
            $(this).removeClass('hovered');
        }
    );
}

function initFooterTooltip()
{
    $('a.registered').cluetip({
      splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
                       // ...and split the contents into separate divs where there is a "|"
      showTitle: true, // hide the clueTip's heading
      positionBy: 'auto',
      activation: 'hover',
      sticky: false
    })
}

/**
 * Preload images for navigation-mouseovers.
 */
function preloadNavigation(navigations)
{
        var images = [];
        var counter = 0;
        
        $(navigations).each(
            function()
            {
                this.each(
                    function()
                    {
                        var matches = $(this).css('backgroundImage').match(/^url\((.*?)(?:-act|-no)?\.gif\)$/i);
                        if (matches && matches[1].length > 1)
                        {
                            images.push(matches[1] + '-act.gif');
                            
                            // If quick-nav (the first item in navigations-array)
                            if(counter == 0)
                            {
                               images.push(matches[1] + '-hover.gif');
                            }
                        }
                    }
                )
                counter++
            }
        );

        $.preload(images)
}

function initToggleNaviElements(clickProxy, clickProxySelector, toggledElementSelector)
{
    var $allClickProxies = $(clickProxy).find(clickProxySelector);
    $allClickProxies
        .css({cursor: 'pointer'})
        .click(
            function(event)
            {
                var linkSharp = $(this).filter("a[href*='#']").eq(0);
                if (linkSharp.length)
                {
                    event.preventDefault();
                    // the clicked li is a proxy for another element which will be
                    // hidden or displayed, search for this first because it will
                    // be reused
                    $element = linkSharp.find(toggledElementSelector);
                    //$(clickProxy).find('li').not(this).removeClass('act');
                    $(clickProxy).find('li').not('.last-item').not(this).find('img').height('26px');
                    
                    if (linkSharp.hasClass('act'))
                    {
                        //$(this).parent().removeClass('act');
                        linkSharp.find('img').height('26px');
                    }
                    else 
                    {
                        //$(this).parent().addClass('act');
                        linkSharp.find('img').height('27px');
                    }           
                    
                    // treat clicked element  (use slideDown() instead of
                    // toggle() is user is not allowed to close opened elements
                    // by clicking on them)
                    $element.toggle('fast');
                    
                    // hide all visible elements, but not the clicked element (that's the trick!)
                    $allClickProxies.find(toggledElementSelector).filter(':visible').not($element).slideUp('fast');
                }
            }
        )
        // hide all by default
        //$allClickProxies.parent().not('act').find('a').find(toggledElementSelector).hide();
        .find(toggledElementSelector).parent().not('.act').find('ul').hide();
}

function initFoldNavigation()
{
    //fold in navigation elements
    $('.fold').not('.last-item').addClass('unfolded');
}

function fixBorderHeight() {
    if($('#outer-border-bottom').height() % 2 == 1) {
        $('#outer-border-bottom').addClass('outer-border-bottom-heightfix');
    }
}

function footnoteToggle() {
    $('#footnote li ul').hide();

    $('#footnote li span').click(
        function() {
            $('#footnote li ul').toggle();
            $(this).toggleClass('open');
        }
    );
}
