//When the document loads...
window.onload = function() {

	// Fixes the link underline issue in FF
	moveUnderlinesToChildren("a", "sup")
	moveUnderlinesToChildren("a", "font")
	
	//Loop through each unordered list in the document
	for(i = 0; i < document.getElementsByTagName("ul").length; i++) {			
		ul = document.getElementsByTagName("ul")[i]

		//Only process <ul>s with classes of "tabPanel"
		if(ul.className.indexOf("tabPanel") >= 0) {
			cleanWhitespace(ul); //Remove any text nodes between <li>s
			
			//Reorder the list so that all title <li>s are in a row at the 
			//beginning, and all content <li>s in row at the end
			for(j = 1; j <= ul.childNodes.length / 2; j++)
				ul.childNodes[j].parentNode.appendChild(ul.childNodes[j])
			
			//Set up onclick activation events for each title <li>
			for(j = 0; j < ul.childNodes.length / 2; j++)
				ul.childNodes[j].onclick = function() { 
					activateTab(this); return false; 
				}
			
			activateTab(ul.childNodes[0]) //Activate the first tab
		}
	}
	
	// Fixes the Commentbox nav in FF
	if(document.URL.indexOf("commentbox") != -1)
				window.location.href = document.URL;
				
}

// Fixes the link underline issue in FF
moveUnderlinesToChildren = function(tagName, childName)
{
   tags = document.getElementsByTagName(tagName)
   for(i=0; i<tags.length; i++)
   {
      childTags = tags[i].getElementsByTagName(childName)
      
      if(childTags.length > 0)
      {
         tags[i].style.textDecoration = "none"
         for(j=0; j<childTags.length; j++)
            childTags[j].style.textDecoration = "underline"
      }
   }
}

//This is called when an inactive tab is clicked
activateTab = function(targetLi) {
	ul = targetLi.parentNode
	numLIs = ul.childNodes.length
	
	//Find the content <li> that corresponds to the clicked title <li>
	targetPanelIndex = numLIs / 2 + getElementIndex(targetLi)
	
	//Loop through each <li>
	for(k = 0; k < numLIs; k++) {
		li = ul.childNodes[k]
		if(k < numLIs / 2)
			li.className = "tab" //Reset the title <li>s to inactive
		else if(k == targetPanelIndex)
			li.style.display = "block" //Show the active content <li>
		else
			li.style.display = "none" //Hide all other content <li>s
	}

	targetLi.className = "activeTab" //Make this <li> active
	
	if(targetLi.blur) //Make sure the blur function is available
		targetLi.blur() //Remove the dotted "active" line
}

//Remove all text nodes which are children of el
cleanWhitespace = function(el) {
	for(m = el.childNodes.length-1; m >= 0; m--)
		if(el.childNodes[m].nodeType == 3)
			el.removeChild(el.childNodes[m]);
}

//Returns this elements position with respect to its parent
getElementIndex = function(element) {
	index = 0
	while(element.previousSibling) {
		element = element.previousSibling
		index++
	}
	return index
}
