// Execute this after the site is loaded.
$(function() {
		   
    // Find list items representing folders and
    // style them accordingly.  Also, turn them
    // into links that can expand/collapse the
    // tree leaf.
    $('li > div').each(function(i) {
        // Find this list's parent list item.
        var parent_li = $(this).parent('li');

        // Style the list item as folder.
        parent_li.addClass('expand');

        // Temporarily remove the list from the
        // parent list item, wrap the remaining
        // text in an anchor, then reattach it.
        var sub_div = $(this).hide();
        parent_li.wrapInner('<a/>').find('a').click(function() {
            // Make the anchor toggle the leaf display.

		if(sub_div.hasClass('loaded'))
		{
			sub_div.hide("fast");
			sub_div.html("");
			sub_div.removeClass('loaded');
			parent_li.removeClass('collapse');
			parent_li.addClass('expand');
		}
		else
		{
			var myurl='/scripts/ajax/xsitemap.asp?root='+parent_li.attr("id");   
			$.ajax({
				url: myurl,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				error: function(){
					alert('We appologize but this service has been temporarily disabled.  Please Try Again.');
				},
				success: function(str){
					sub_div.html(str);
					sub_div.show("fast");
					sub_div.addClass('loaded');
					parent_li.addClass('collapse');
					parent_li.removeClass('expand');
				}
			});
		}
        });
		parent_li.find('a').addClass('xa');
        parent_li.append(sub_div);
    });

    // Hide all lists except the outermost.
    $('ul ul').hide();
});
