
$(document).ready(function() {
	var sitemap = $("<div/>").attr("id", "map");
	$("#yui-main .yui-b").children().not("h1").wrapAll(sitemap);
						   
	var alphaList = createAlphaList();
	alphaList.insertAfter("#map");
	
	var buttons = createTabButtons();
	buttons.insertBefore("#map");
	
	var searchBox = createSearchBox();
	searchBox.insertBefore(buttons);
	
	// Create tabs
	//buttons.tabs();
	$("#yui-main .yui-b").children().not("h1").wrapAll( $("<div/>").addClass("tabbed") );
	var tabbed = $("div.tabbed:first");
	tabbed.tabs();
	
	// Collect list of searchable content
	var data = [];
	alphaList.find("li").each( function(i) {
		data.push( $(this) );
	});
	
	// Init autocomplete
	setupAutocomplete($("#indexSearch"), data);
});

// Generate alphabetically sorted list of all the links in the sitemap
// Also appends ancestor trail to help identify ambiguous items
function createAlphaList() 
{
	// Create container div for A-Z list
	var div = $("<div/>").attr("id", "alpha");
	
	// Create H2 heading
	//$("<h2/>").text("A-Z Index").addClass("first").appendTo(atoz);
	
	// Copy and sort data from site map
	var data = $("#map li").map(
		function (i) {

			var parentTrail = "";

			$(this).parents().filter("li:first").each( function() {
				parentTrail += ", " + jQuery.trim( $(this).children("a:first").text() );
			});
			
			$(this).parents().prev("h4, h3, h2").each( function() {
				parentTrail += ", " + jQuery.trim( $(this).text() );
			});
			
			var li = $(this).clone();
			li.contents().filter("ul, br").remove();
			li.children("a").children("br").remove();
			li.removeAttr("class");
			
			li.trimWhitespace();
			
			li.append("<em>"+parentTrail+"</em>");
			
			return li;
		}
	).tsort();
	
	// Add new data to A-Z div
	$("<ul/>").append(data).appendTo(div);
	
	return div;
}

// Create tab buttons for interface
function createTabButtons()
{
	var list = $("<ul/>");//.addClass("tabbed");
	list.append( $("<li/>").append( $("<a/>").attr("href", "#map").text("View as Site Map") ) );
	list.append( $("<li/>").append( $("<a/>").attr("href", "#alpha").text("View Alphabetically") ) );
	
	return list;
}

// Create search box for interface
function createSearchBox()
{
	var input = $("<input/>").attr("id", "indexSearch");
	
	var label = $("<label/>").attr("id", "indexSearchLabel").attr("for", "indexSearch").text("Find on this page: ");
	
	var div = $("<div/>").attr("id", "indexSearchBox").append(label).append(input);
	
	return div;
}

// Configure and initialize autocomplete for search box
function setupAutocomplete(inputObj, data)
{
	// Create new results div
	var resultObj = $("<div/>").attr("id", "indexResults").insertAfter(inputObj.parent());
	
	// Setup autocomplete options
	var options = {
		/*highlight : null, */
		matchContains : true,
		matchFirst : true,
		mustMatch : false,
		width : null,
		minChars : 1,
		max : 0,
		selectFirst: false,
		scroll : false,
		formatItem : function(row, pos, max, term) {
			// Grab item HTML for results display
			return row.html();
		},
		formatMatch : function(row, pos, max, term) {
			// Grab just item text for searching
			return row.text();
		},
		onChange : function(val) {
			//console.log("onChange: "+val);
		},
		resultsObj : resultObj
	};
	
	// Init autocomplete
	inputObj.autocomplete( data, options );
}

// Plugin that recursively trims leading & trailing whitespace on each text node
jQuery.fn.trimWhitespace = function() {
	return this.each( 
		function() {
			if (this.nodeType == 3) {
				this.data = jQuery.trim( this.data );
	
			} else {
				jQuery(this).contents().trimWhitespace();
			}
		}
	);
};
