// Accessible Site Map/Site Tree using
// jQuery and ARIA standards. Heavily
// based on Travis Roth's work
// http://www.travisroth.com/

// Ryan Laverty for Shure Incorporated
// 2009 - 2010

$(document).ready(function() { 

// Find the list that will be the container
// for our accessible tree and assign ARIA
// roles.

	var siteTree = $('ul.sitemap-list');
	//siteTree.attr({role: 'tree', tabindex: '0', 'aria-labelledby': 'treelabel'});
	//siteTree.find('li').attr({role: 'treeitem', tabindex: '-1'});
	//siteTree.find('li a').attr({tabindex: '-1'});
	

// Find list items that are groups (have
// (sublists) and add classes. Also adds
// expand/collapse links.

// index counter for ARIA labels
	//var treeitem_label_i = 0;
	
	$('ul.sitemap-list li > ul').each(function(i) { 
		
		//treeitem_label_i++;
		
// Find this list’s parent list item. 

		var parent_li = $(this).parent('li'); 

// Style the list item as group. 

		parent_li.addClass('group'); 

// Temporarily remove the list from the 
// parent list item, add expand/collapse
// link, then re-insert the list.

		var sub_ul = $(this).remove();
		//parent_li.find('a').wrapInner('<span id="ariaLabel'+treeitem_label_i + '"></span>');
		//parent_li.attr('aria-labelledby','ariaLabel'+treeitem_label_i);
		parent_li.prepend('<a class="expander" href="#">expand&#47;collapse</a>').find('a.expander').click(function() {

// Make the anchor toggle the leaf display. 

			sub_ul.slideToggle();
			$(this).toggleClass('expanded');
			return false

		}); // close the add a click event handler

// Add ARIA expanded attributes and re-insert
// the sublists.
		parent_li.append(sub_ul);
		siteTree.children('li').children('a.expander').addClass('expanded');

	}); // closes the loop working on each branch

// Hide all lists except the category-level
// items (first child list items of tree). 

	$('ul.sitemap-list li ul li ul').hide(); 

});  
