document.addEventListener("DOMContentLoaded", function(){

	var tools = {

		randomID : function(){
			
			return Math.random().toString(36).slice(2).replace(/\d/g,"");

		}

	};

	var docs = document.querySelector("#docs");

	//prettyPrint
	var pres = document.querySelectorAll("pre");
	if(pres.length){
		[].forEach.call(pres, function(pre){
			pre.classList.add("prettyprint");
		});	
		prettyPrint();
	}
	
	//generate icon
	var generateIcon = function(){
		var menu = document.querySelector("#menu");
		var dds = menu.querySelectorAll("dd");
		if(dds.length){
			[].forEach.call(dds, function(dd){
				var frag = document.createDocumentFragment();
				var i = document.createElement("i");
				i.classList.add("fa");
				i.classList.add("fa-angle-double-right");
				frag.appendChild(i);
				dd.insertBefore(frag, dd.firstChild);
			});	
		}
	}


	//generate archive
	var archive = function(renderH3){
		
		var h2s = document.querySelectorAll("#docs h2");
		if(h2s.length){
			var h1 = document.querySelector("#docs h1");
			var arch = document.createElement("ul");
			arch.id = "arch";
			[].forEach.call(h2s, function(h2){
				var id = tools.randomID();
				h2.id = id;
				var li = document.createElement("li");
				var a = document.createElement("a");
				a.addEventListener("click", function(e){
					var offset = document.querySelector("#" + id).offsetTop;
					window.scrollTo(0,offset);
				}, false);
				a.innerHTML = h2.innerHTML;
				li.appendChild(a);

				if(renderH3){
					var h3s = document.querySelectorAll("." + h2.innerHTML.toLowerCase());
					if(h3s.length){
						var ul = document.createElement("ul");
						[].forEach.call(h3s, function(h3){
							var h3id = "h3" + tools.randomID();
							var h3li = document.createElement("li");
							var h3lia = document.createElement("a");
							h3li.appendChild(h3lia);
							h3lia.addEventListener("click", function(e){
								var offset = document.querySelector("#" + h3id).offsetTop;
								window.scrollTo(0,offset);
							}, false);
							h3.id = h3id;
							h3lia.innerHTML = h3.innerHTML;
							ul.appendChild(h3li);
						});
						li.appendChild(ul);
					}
				}
				
				arch.appendChild(li);
			});
			
			docs.insertBefore(arch, h1.nextSibling);
		}
	}
	if(url(3) === "api_document"){
		archive(true);	
	} else {
		archive();
	}
	
	//scrollTop
	var goTop = function(){
		if(document.querySelector("#gotop")) return console.log("duplicate id: gotop");
		
		var gotop = document.createElement("div");
		gotop.id = "gotop";
		gotop.innerHTML = '<i class="fa fa-arrow-up"></i>';
		
		document.body.appendChild(gotop);

		gotop.addEventListener("click", function(){

			window.scroll(0,0);

		}, false);

		window.addEventListener("scroll", function(e){
			
			if(document.body.scrollTop >= (window.screen.availHeight + 100)){
				gotop.style.display = "block";
			} else {
				gotop.style.display = "none";
			}

		}, false);
	}
	goTop();

	//menu
	var h3nav = {};
	var initMenu = function(){
		var h3s = document.querySelectorAll("h3");
		[].forEach.call(h3s, function(h3){
			var text = h3.innerText || h3.textContent;
			text = text.toLowerCase().replace(/\s/ig, "");
			h3nav[text] = h3;
		});
		var mod = url(1, location.href);
		var el = h3nav[mod];
		while(el.nextElementSibling.tagName === "DL"){
			el.nextElementSibling.style.display = "block";
			el = el.nextElementSibling;
		}
	}
	initMenu();

}, false);
@@ -1476,24 +1476,122 @@
 (renderH3)%7B%0A
+%09%09%09%09%09var sel = h2.innerText %7C%7C h2.textContent;%0A%09%09%09%09%09sel = sel.toLowerCase().replace(/%5C(.*%5C)/,%22%22);%0A
 %09%09%09%09%09var h3s
@@ -1629,34 +1629,11 @@
 %22 + 
-h2.innerHTML.toLowerCase()
+sel
 );%0A%09
@@ -3340,16 +3340,41 @@
 tSibling
+ && el.nextElementSibling
 .tagName

fix () bug