var tableOfContents=new Class({options:{prefix:"tocList",returnLink:false,returnLinkText:"back to top",inject:"after"},initialize:function(A,B){$extend(this.options,B);this.tags=[];if($type(A)=="string"){this.tags[0]=new this.tagSearch({tagSearch:A})}else{A.each((function(C,D){this.tags[D]=new this.tagSearch(C)}).bind(this))}},output:function(A){A=$(A);A.getChildren().dispose();this.tocList=new Element("ul",{id:this.options.prefix});this.buildList(0);this.tocList.inject(A,"bottom");if(this.options.returnLink==true){new Element("a",{id:this.options.prefix+"Top",name:this.options.prefix+"Top"}).inject(document.body,"top")}},buildList:function(){for(tocLevel=0;tocLevel<this.tags.length;tocLevel++){tag=this.tags[tocLevel];$$(tag.tagSearch).each((function(B,A){li=this.createLi(tocLevel,B);if(tag.tagLink==true){listElement=new Element("a",{href:"#"+this.options.prefix+"_"+tocLevel+"_"+A})}else{listElement=new Element("span")}listElement.set("text",B.get("text"));if($type(tag.tagWrap)=="object"){listElement=new Element(tag.tagWrap.name).grab(listElement);if($type(tag.tagWrap.properties)=="object"){listElement.setProperties(tag.tagWrap.properties)}}else{if($type(tag.tagWrap)=="string"){listElement=new Element(tag.tagWrap).grab(listElement)}}listElement.inject(li);B.store("tocListRef",li);new Element("a",{id:this.options.prefix+A,name:this.options.prefix+"_"+tocLevel+"_"+A}).inject(B);if(this.options.returnLink==true){topLink=new Element("a",{href:"#"+this.options.prefix+"Top"});topLink.set("text",this.options.returnLinkText);topLink.inject(B,this.options.inject)}}).bind(this))}},tagSearch:new Class({options:{tagSearch:false,tagLink:true,tagWrap:false},initialize:function(A){$extend(this.options,A);this.tagSearch=this.options.tagSearch;this.tagLink=this.options.tagLink;this.tagWrap=this.options.tagWrap}}),createLi:function(A,B){li=false;if(A>0){for(parentLevel=A-1;parentLevel>=0;parentLevel--){parentTocEl=B.getPrevious(this.tags[parentLevel].tagSearch);if(parentTocEl==false){parentTocEl=B.getParent(this.tags[parentLevel].tagSearch)}for(tocLevelSearch=0;tocLevelSearch<A;tocLevelSearch++){ancestorTocElement=B.getPrevious(this.tags[tocLevelSearch].tagSearch);if(ancestorTocElement){if(ancestorTocElement.getPrevious(this.tags[parentLevel].tagSearch)==parentTocEl){parentTocEl=false}}}if(parentTocEl){parentTOC=parentTocEl.retrieve("tocListRef").getFirst();li=new Element("li");subUl=new Element("ul").grab(li);for(positionSearch=A-1;positionSearch>=parentLevel;positionSearch--){if(positionSearch>0&&parentTocEl.getAllNext(this.tags[positionSearch].tagSearch).contains(B.getPrevious(this.tags[positionSearch].tagSearch))){parentTOC=B.getPrevious(this.tags[positionSearch].tagSearch).retrieve("tocListRef").getFirst();break}}for(x=1;(A-parentLevel)>x;x++){subUl=new Element("ul").grab(subUl)}subUl.inject(parentTOC,"after")}}if(li==false){li=new Element("li");subUl=new Element("ul").grab(li);for(x=2;(A-parentLevel)>x;x++){subUl=new Element("ul").grab(subUl)}subUl.inject(this.tocList,"top")}}if(li==false){li=new Element("li").inject(this.tocList)}return li}});