/* <![CDATA[ */

var XHTMLNS = "http://www.w3.org/1999/xhtml";

if(document.getElementsByTagName) {
	//thank goodness we're serving these as text/html
	document.write("<style type=\"text/css\" media=\"screen,projection\">.tabcontent {display: none; border: 1px solid #999999; padding: 5px;} .tabcontainer h3 { display: none; } .tabcontent.activetab {display: block;}</style>\n");
	/* The reason I was able to get away with the previous snippet of code is
	   mainly because if the browser was able to run that piece of code (hiding
	   the panes) then it'll also be able to run the following piece (showing
	   them) so there are no issues.  Keyboard navigation including tab indices
	   should be handled right because we're actually modifying the DOM when
	   adding the tab activator links.
	 */
	
	
	//now set up the runner that will move the tab pieces
	//TODO: use addEvent
	addEvent(window, "load", makeTabs);
	
}

function makeTabs(e) {
	var containers = document.getElementsByTagName("div");
	for(var i=0; i < containers.length; i++) {
		var x = containers[i];
		if(checkClass(x, "tabcontainer")) {
			//need to kick it up a notch
			//create the nodes to handle the tab links
			var d = document.createElementNS(XHTMLNS,"div");
			var c = x.childNodes;
			
			for(var j=0; j < c.length; j++) {
				if(c[j].nodeName=="H3") {
					//it's a heading, so find its proper child
					var childpos = j+1;
					while(childpos < c.length && c[childpos].nodeType != 1)	childpos++;
					if(childpos < c.length) {
						//we've got a child
						//alert("Found the child at "+childpos+" with inner html "+c[childpos].innerHTML);
						if(!c[childpos].getAttribute("id")) {
							alert("A child at position " + childpos + " has no id!");
							//TODO: give it one
						}
						var a = document.createElementNS(XHTMLNS,"a");
						var l = document.createTextNode(c[j].innerHTML);
						a.appendChild(l);
						d.appendChild(a);
						//TODO: Use the ID of the div, so at least we'll hop with stylesheets off
						a.href="#"+j;
						a.title=c[j].title;
						//TODO: clickhandler should use e.srcEvent and not require an id
						//TODO: also onfocus?
						eval("a.onclick = function(e) { return clickhandler('"+c[childpos].getAttribute("id")+"'); };");
						if(checkClass(c[childpos], "activetab")) {
							x.activetab = c[childpos].getAttribute("id");
							x.activelink = a;
							juggleClass(a, "activetab", 1);
						}
					}
					j = childpos;
				}
			}
			x.insertBefore(d, c[0]);
			d.className="tablinks";
		}
	}
}

//TODO: get rid of id, and directly use an event handler with e.srcEvent
//  keep in mind Safari's handling of events returns the text node IIRC
function clickhandler(id) {
	var p = document.getElementById(id).parentNode;
	
	juggleClass(document.getElementById(id), 'activetab', 1);
	juggleClass(document.getElementById(p.activetab), 'activetab', 0);
	p.activetab = id;
	//also set the active tab-link
	return false;
}

if(!document.createElementNS) {
	document.createElementNS = function(ns,elt) {
		return document.createElement(elt);
	}
}

function juggleClass(o,c,s) {
 o.className=s==1?o.className+' '+c:o.className.replace(' '+c,''); 
}

function checkClass(o,c) {
 var isClassInObj=o.className.indexOf(c)!=-1?true:false;
 return isClassInObj;
}

// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

/* ]]> */
