﻿$(function() {
    var navWidth = 535;
    var ulIndent = 20;
    var pathname = window.location.pathname;
    $('.file_list').css('width', navWidth + 'px');
    $('.file_list ul').css('width', navWidth + 'px');
    $('.file_list ul').css('margin-left', ulIndent + 'px');

    $('.file_list a').each(function() {
        var level = $(this).parents('ul').length;
        var liWidth = navWidth - (ulIndent * level) + 30;
        $(this).parent('li').css('width', liWidth + 'px');
    });
    $('.file_list li').each(function() {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).prepend('<img src="components/images/imgOnOpen.gif" />');
            }
            else {
                $(this).prepend('<img src="components/images/imgOffClosed.gif" />');
            }
        }
    });
    var slideSpeed = 'slow'; 
    $('.file_list a').each(function() {
        var thisHref = $(this).attr('href')
        if ((window.location.pathname.indexOf(thisHref) == 0) || (window.location.pathname.indexOf('/' + thisHref) == 0)) {
            $(this).addClass('Current');
        }
    });
    $('.Current').parent('li').children('ul').show();
    $('.Current').parents('ul').show();
    $('.file_list li').each(function() {
        if ($(this).children('ul').length > 0) {
            if ($(this).children('ul').is(":visible")) {
                $(this).children('img').attr('src', 'components/images/imgOnOpen.gif');
            }
        }
    });
    $('.file_list img').click(function() {
        if ($(this).parent('li').children('ul').html() != null) {
            $(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
            $(this).parent('li').parent('ul').children('li').children('img').attr('src', 'components/images/imgOffClosed.gif');
            $(this).delay(100).is(':hidden');
            if ($(this).parent('li').children('ul').css('display') == "block") {
                $(this).parent('li').children('ul').hide(slideSpeed);
                $(this).attr('src', 'components/images/imgOffClosed.gif');
            } else {
                $(this).parent('li').children('ul').show(slideSpeed);
                $(this).attr('src', 'components/images/imgOnOpen.gif');
            }
            return false;
        }
    });
    $('.file_list li').click(function() {
        if ($(this).children('a').length == 0) {
            if ($(this).children('ul').html() != null) {
                $(this).parent('ul').children('li').children('ul').hide(slideSpeed);
                if ($(this).children('ul').css('display') == "block") {
                    $(this).children('ul').hide(slideSpeed);
                } else {
                    $(this).children('ul').show(slideSpeed);
                }
            }
        }
    });
    var imgOffClosed = "url(components/images/imgOffClosed.gif) no-repeat 5px ";
    var imgOnClosed = "url(components/images/imgOnClosed.gif) no-repeat 5px ";
    var imgOnOpen = "url(components/images/imgOnOpen.gif) no-repeat 5px ";
    var charBeforeLB = 23;
    var paddingBig = 12;
    var paddingSmall = 8;
    var maxLIHeight = 50;
    $('.file_list > li').each(function() {
        var childText = $(this).children('a').text();
        var topPadding = paddingBig;
        if (childText.length < charBeforeLB) { topPadding = paddingSmall; }
        if ($(this).height() < maxLIHeight) {
            if ($(this).children('a').attr('class') == "Current") {
                $(this).parent('li').children('img').attr('src', 'components/images/imgOnOpen.gif');
            } else {
                $(this).parent('li').children('img').attr('src', 'components/images/imgOffClosed.gif');
            }
        }
        else {
            $(this).children('img').attr('src', 'components/images/imgOnOpen.gif');
        }
    });
});
