if (typeof Prototype != "undefined") {
	document.observe("dom:loaded", function() {
		try {
			var pageH1 = $$("#template h1").first();
			var subheaders = $$("#template h1 ~ h2");
			var pagePath = (typeof g_current_page_path != "undefined") ? g_current_page_path : "";
			
			if ((pagePath != "home") && pageH1 && (subheaders.length > 0)) {
				// Build the subheader container
				var subheadContainer = $(document.createElement("p"));
				subheadContainer.addClassName("subheaderLinkContainer");
				subheaders.each(function(item, i) {
					if (!item.firstChild.nodeValue || item.firstChild.nodeValue == "null") {
						return;
					}
					
					if (i > 0) {
						subheadContainer.appendChild(document.createTextNode(" | "));
					}
					
					// Span to point at
					var subheadSpan = $(document.createElement("span"));
					subheadSpan.id = "subheader" + i;
					item.appendChild(subheadSpan);
					
					// Link to the span
					var tempLink = $(document.createElement("a"));
					tempLink.href = pagePath + "#subheader" + i;
					tempLink.appendChild(document.createTextNode(item.firstChild.nodeValue));
					subheadContainer.appendChild(tempLink);
					
					if (i > 0) {
						// Create a "back to top" link
						var backToTopContainer = $(document.createElement("p"));
						var backToTopLink = $(document.createElement("a"));
						backToTopLink.href = pagePath + "#";
						backToTopLink.appendChild(document.createTextNode("back to top"));
						backToTopContainer.appendChild(backToTopLink);
						
						// Insert the "back to top" link
						var nextSubheader = item.next("h2");
						if (nextSubheader) {
							item.insert({before: backToTopContainer});
						} else {
							$(item.parentNode).appendChild(backToTopContainer);
						}
					}
				});
				
				// Insert it before/after? the h1
				pageH1.insert({after: subheadContainer});
				//$("template").appendChild(subheadContainer);
			}
		} catch (e) {
			//alert(Object.toJSON(e));
		}
	});
}
