var tb_defaultHeight, tb_defaultWidth;

$(function () {
    tb_defaultHeight = 500;
    tb_defaultWidth = 700;

    $(document).pngFix();

    $('ul.jqtoggle').find('>li:has(>ul:not(.notoggle),>dl:not(.notoggle),>p:not(.notoggle))')
    .addClass('jqtoggler').click(function () {
        $(this).toggleClass('open').find('>ul,>dl,>p').not('.notoggle').slideToggle();
    }).find('>ul,>dl,>p').not('.notoggle').hide().css({
        'cursor':'auto'
    }).click(function () {
        return false;
    }).end().end().end().prepend('<li class="jqexpand"><span>Expand</span></li>')
    .find('li.jqexpand').removeClass('jqtoggler').toggle(function () {
            var li = $(this),
                span = li.find('span');

            li.toggleClass('jqexpand').toggleClass('jqcollapse'
            ).attr('title', li.attr('title').replace('Expand','Collapse')
            ).parent('ul').find('>li.jqtoggler:not(.open)').trigger('click');

            span.text('Collapse');
        },function () {
            var li = $(this),
                span = li.find('span');

            li.toggleClass('jqexpand').toggleClass('jqcollapse'
            ).attr('title', li.attr('title').replace('Collapse','Expand')
            ).parent('ul').find('>li.jqtoggler.open').trigger('click');

            span.text('Expand');
    }).attr('title','Expand this list');

    $('dl.jqtoggle').find('>dt').each(function () {
        var dt = $(this),
            sib = dt.next('dd,dt'),
            dds = [];

        while (sib && sib.length == 1 && sib.is('dd')) {
            if (sib.not('.notoggle')) {
                dds.push(sib.hide());
            }
            sib = sib.next('dd,dt');
        }

        if (dds.length) {
            dt.addClass('jqtoggler').click(function () {
                $(this).toggleClass('open');

                for (var i = 0; i < dds.length; i++) {
                    dds[i].slideToggle();
                }
            }).parent('dl').addClass('active');
        }
    }).find('>dd:not(.notoggle)').hide().css({
        'cursor':'auto'
    }).click(function () {
        return false;
    });

    $('dl.jqtoggle.active').prepend('<dd class="jqexpand"><span>Expand</span></dd>')
    .find('dd.jqexpand').removeClass('jqtoggler').toggle(function () {
        var dd = $(this),
            span = dd.find('span');

        dd.toggleClass('jqexpand').toggleClass('jqcollapse')
        .attr('title', dd.attr('title').replace('Expand','Collapse'))
        .parent('dl').find('>dt.jqtoggler:not(.open)').trigger('click');

        span.text('Collapse');
    },function () {
        var dd = $(this),
            span = dd.find('span');

        dd.toggleClass('jqexpand').toggleClass('jqcollapse')
        .attr('title', dd.attr('title').replace('Collapse','Expand'))
        .parent('dl').find('>dt.jqtoggler.open').trigger('click');

        span.text('Expand');
    }).attr('title','Expand this list');

    $('fieldset.jqtoggle').children(':not(legend)').hide();
    $('fieldset.jqtoggle').prepend('<div class="jqexpand"><span>Expand</span></div>')
    .find('div.jqexpand').removeClass('jqtoggler').toggle(function () {
        var toggler = $(this),
            span = toggler.find('span');

        toggler.toggleClass('jqexpand').toggleClass('jqcollapse')
        .attr('title', toggler.attr('title').replace('Expand', 'Collapse'))
        .parent('legend').parent('fieldset').children(':not(legend,.jqexpand,.jqcollapse)').show();

        span.text('Collapse');
    }, function () {
        var toggler = $(this),
            span = toggler.find('span');

        toggler.toggleClass('jqexpand').toggleClass('jqcollapse')
        .attr('title', toggler.attr('title').replace('Collapse', 'Expand'))
        .parent('legend').parent('fieldset').children(':not(legend,.jqexpand,.jqcollapse)').hide();

        span.text('Expand');
    });

    $('a[target=external]').addClass('external')
    .not('[title]').attr('title', 'Link will open in a new window.');
});

