/*This function searches a specified area for specified tags (or all headings) or (span) elements with a specified style,
then places a list of anchor links at the top of the page or in a specified area.

TAKES UP TO 4 ARGUMENTS:
autoAnchor(TAG OR STYLE TO FIND, ID OF SEARCH CONTAINER, ID OF OUTPUT CONTAINER, USE <li> TAGS AROUND <a> IN LIST -- BOOLEAN OR STRING)
-Place all arguments in quotes.
-Do not inclue < or > in tag names.
-When searching for a style, the style must applied to a <span> tag.
-If the tag argument is "allheadings", h2 - h5 tags are listed and indented by heading.

DEFAULTS:
-If no arguments are specified...
   Finds a containter with id of "anchortext" to search for <h4> tag,
   then finds container with id of "anchorlist" and places the anchor list within that container.
-If no id of "anchortext" is found...
   Searches the entire main content (the mainContainer <div> opened in the bodyTop include).
-If no id of "anchorlist" is found...
   Creates an unordered list just below the first <h1> tag with a style of "linkList".
-If no <h4> tags or no specified tag/style is found...
	generates nothing


Example:
  If no arguments are specified, and there are no <div> containers with "anchortext" or "anchorlist" id's, 
  the function searches the main content for <h4> tags and places the list just below the <h1> heading in a grey box
  (style of "linkList").
  
	
GENERATED TAGS:
The default generated anchors are of the form:
   <li><a href="#INNERTEXT_OF_TAG_WITH_WHITESPACE_REPLACED_BY_UNDERSCORES">INNERTEXT OF TAG</a></li>
   unless the tag already has an id in which case that id is used
List item and <a> tag styles will apply.
If the li argument is "false" or false, the <li> tags are not placed and <br /> tags are used.

PLACEMENT:
This function call must be placed at the end of the main content, prior to the bodyBottom.shtml include.

Avram Lavinsky
1/24/08
*/

var anchorListDestination;

	if(!Array.indexOf){//This fixes the ie indexOf lack of support
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}

function autoAnchor(tag,anchortext,anchorlistName,li){
	//Note that the tag argument can also represent a style
	var styledTag="span";//<<<<<Change this variable to change the tags to search if first argument represents a style
	var mainContainerID="mainContainer";//<<<<<<Change this to any id of the div containing main content
	var defaultListStyle="linkList";//<<<<<<<Change this to change the default UL style to output the list in
	var insertParentTags=false;//used when defaulting to adding a list destination
	//If mainContainerID is incorrect, navigation bar and header content may appear in the anchor list

			if(!anchortext){
				anchortext="anchortext";//id of area to search defaults to id of "anchortext"
			}
			if(!anchorlistName){
				anchorlistName="anchorlist";//id of area to write list defaults to "anchorlist"
			}
    		anchorListDestination=document.getElementById(anchorlistName);
    		var anchorSearchArea = document.getElementById(anchortext);
			if(!anchorSearchArea){
				//If there is no area to search with designated or default id then search mainContainer div
				anchorSearchArea=document.getElementById(mainContainerID);
				if(!anchorSearchArea){//Includes are missing -- we must be using an editor, so search it all
					anchorSearchArea=document.body;
				}
			}
			if(!anchorListDestination){
				//If there is no conatiner element with id matching anchor text variable or default id of "anchortext"
				//then add an element after the <h1> element
				insertParentTags=true;
				var titleNode=document.getElementsByTagName("h1")[0];
				var titleParent=titleNode.parentNode;
				anchorListDestination=document.createElement('div');
				titleParent.insertBefore(anchorListDestination,titleNode);
				titleParent.insertBefore(titleNode,anchorListDestination);
			}
			if(li===""||li=="true"||li===undefined){
				li=true;
			}
			if(li=="false"){
				li=false;
			}
			if(!tag){
				tag="h4";//Tag to search for defaults to h4
			}
			var tagArray=new Array();
			var hLevelArray = new Array();
			var topHeading=6;
			if(tag=="allheadings"){//"allheadings" will capture h2-h5
				li=false;//we won't output list item tags for all, only h2
				var i=0;
				var allArray=anchorSearchArea.childNodes;
				var headingArray = new Array("h2","h3","h4","h5","H2","H3","H4","H5");
				for(n=0;n<allArray.length;n++){
					checkHeading(allArray[n]);
					if(allArray[n].tagName=="div"||allArray[n].tagName=="DIV"||allArray[n].tagName=="a"||allArray[n].tagName=="A"){//Since we are only searching children of the specified containter, we search within divs and a tags (one level only) which may be concealing a heading tag child.

						var divChildrenArray=allArray[n].childNodes;

						for(m=0;m<divChildrenArray.length;m++){
							checkHeading(divChildrenArray[m]);
						}
					}
				}
			}else{								
				tagArray = anchorSearchArea.getElementsByTagName(tag);
			}
			if(tagArray.length==0){//no tags were found...				
				if(tag!="h4"){//...but the tag argument was specified, not defaulted, so check for a span with that style
					var m=0;
					var winner=false;
					var rawArray = anchorSearchArea.getElementsByTagName(styledTag);
					for (n in rawArray){
						if(rawArray[n].className==tag){							
							tagArray[m]=rawArray[n];
							m++;
							winner=true;
						}
					}
				}
			}
			if(tagArray.length>0||winner){//check again now that styles have been checked
				var anchorListItems="";
				if(insertParentTags==true){
					anchorListItems+='<ul class="linkList">';
				}
				for (n in tagArray){
					if(tagArray[n].innerHTML){
				
						var raw=tagArray[n].innerHTML;
						if(tagArray[n].id==""){	
							var txtArray=raw.match(/\w*/g);
							var txt=txtArray.join("_").slice(0,-1);
							tagArray[n].id="anchor"+txt;
						}
						if(li==true||hLevelArray[n]==topHeading){//display within li if li variable is true or tag is "allheadings" and we have top heading
							anchorListItems+='<li>';
						}
						if(hLevelArray[n]>topHeading){//if tag is "allheadings" and we do not have the top heading...
							anchorListItems+=indent(hLevelArray[n]-topHeading);//then indent by heading
						}
						anchorListItems+='<a href="#'+tagArray[n].id+'">'+raw+'</a>';
						if(li==true||hLevelArray[n]==topHeading){//if top heading or li argument is true...
							anchorListItems+='</li>';//then close li
						}else if(hLevelArray[n]>topHeading){//if tag is "allheadings" and we do not have the top heading...
							anchorListItems+="</div>";//then close the indented div
						}else{
							anchorListItems+='<br />';//otherwise just new line
						}
					 }	
				}
				if(insertParentTags==true){
					anchorListItems+='</ul>';
				}
				anchorListDestination.innerHTML+=anchorListItems;
			}
			
			function checkHeading(el){//checks to see if an element is a heading 2-5;
					var tn=el.tagName;
					if(tn!=undefined){
					  if(headingArray.indexOf(tn)>=0){//If the tag hame is in the array above, save it in the tagArray
						tagArray[i]=el;					
						hLevelArray[i]=tn.charAt(1);//Save the heading level in an array for indenting output later on
						if(hLevelArray[i]<topHeading){
							topHeading=hLevelArray[i];
						}
						i++;
					  }
					}
			}

}
function indent(factor){//indents txt by a factor using div so wrapped lines will indent
	var pix = factor*30+30;
	var str = '<div style="padding-left:'+pix+'px;">';
	return (str);
}
