$(document).ready(function() {
    // Support for zebra striping in tables to make them more readable
    $('tbody tr:odd').addClass('odd');
    $('tbody tr:even').addClass('even');
    
    // Support for different icons after links to different file types
    $('p a[href$=".xls"]').addClass('excel');
    $('p a[href$=".doc"]').addClass('word');
    $('p a[href$=".ppt"]').addClass('powerpoint');
    $('p a[href$=".pdf"]').addClass('acrobat');
    
    // Ensure external links open in a new window and have an icon to indicate this
    $('a[href^="http://"]').addClass('external');
    $('a.external').attr('target', '_blank');
    
    // Highlight links which are href="!", these need to be supplied still.
    $('a[href^="!"]').addClass('achtung');
    
    // Turn the tree menu into a menu
    // Note the menu state will persist. This future-proofs the navigation somewhat on the offchance a
    // subsite is added where the user navigating through a page structure not represented in the tree menu.
    // The "saved" state is retrieved from the state it was left in on the last page viewed but this state
    // is only displayed if the page navigated to does not appear in the treemenu. Hopefully this means that
    // entrypoints to non-menu documents will remain visible in the menu by virtue of being the last thing
    // accessed from the menu as long as no other page on the menu is viewed.
    ddtreemenu.createTree("treemenu1", true);
    
    
    // Prepend ./ to internal links to stop the "you are here" sign getting confused.
    $('.treeview a').each(function () {
        a = $(this);
        var hr = a.attr('href');
        if (hr.indexOf(':') == -1)
            a.attr('href', './' + hr);
    });
    // Highlight links to the current page (i.e. "you are here" on the cheap)
    var pn = window.location.pathname;
    var page = pn.substring(pn.lastIndexOf('/') + 1);
    if (pn.indexOf('index.htm') < 0)
        $('a[href$="./'+page+'"]').addClass('currentPage');
    
    // Expand the tree menu so it shows current page
    var openLists = '#treemenu1 ul:has(a.currentPage)';
    $(openLists).each(function (u) {
        // This is done before the opening so that multiple links to the same page in the menu are all shown
        ddtreemenu.flatten('treemenu1', 'collapse')
    });
    $(openLists).each(function (u) {
        // Open the branch of the treemenu where the current page resides
        ddtreemenu.expandSubTree ('treemenu1', this);
    });
    
    // Hide the bullets/numbers on <li> tags which contain a heading
    $('.faq li:has(h2)').addClass('noBullet');
    $('.faq_as li li:has(h3)').each(function () {
        this.innerHTML = '<a href="#top" class="topFloatRight">Top&nbsp;&uarr;</a>\n' + this.innerHTML;
    });
    $('dl.glossary dt').each(function () {
        this.innerHTML = '<a href="#top" class="topFloatRight">Top&nbsp;&uarr;</a>\n' + this.innerHTML;
    });
    $('#rightColumn > h2').each(function () {
        $(this).before ('<a href="#top" class="topFloatRight">Top&nbsp;&uarr;</a>\n');
    });
    
    $('ul.TAFModifier > li').each(function () {
        folder = $('a:first', this);
        shlink = $('ul > li:first > a', this);
        if (!shlink.hasClass('currentPage'))
            folder.attr('href', shlink.attr('href') || 'javascript:void(0);');
    });
    
    
    /*
    This automatically folds up 
        <h2>headings</h2>
        <div class="fold">
            <p>
                which have a div of class "fold" immediately after them and containing
                the content which is to be hidden.
            </p>
            <p>
                the headings are wrapped in a link which jumps to the heading
                (arbitrary #h-N anchors assigned automatically) and toggles the
                visibility of the "fold" div.
            </p>
        </div>
    Links to pages with named anchors in the form #h-N (automatic, remember to update links if adding headings above a foldable section as anchor names will be bumped)
    */
    var anch = 1;
    
    $('div.fold').each (function () {
        var div = $(this);
        var head = $(this).prev("h2");
        if (head.length) {
            div.hide();
            anch++;
            head.html ($('<a name="h-' + anch + '" href="#h-' + anch + '"></a>').addClass ("toggleLink").append (head.html()).click (function () {
                $(this).parent().next("div.fold").toggle();
            }));
        }
    });

    var myFile = document.location.toString();
    if (myFile.match('#')) { // the URL contains an anchor
        // click the navigation item corresponding to the anchor
        var myAnchor = '#' + myFile.split('#')[1];
        $('a.toggleLink[href="' + myAnchor + '"]').click();
    }
    
    
});

//$('.sssmenu li.menutitle[li.active]‘).addClass(’active’);
