/* tabswitch.js

	- Lifted from a great job done by folks at the
	- USGS, http://earthquake.usgs.gov/js/easytoggle.js

	- Which was based on scripts from
	- Simon Willison, http://simon.incutio.com/archive/2003/11/06/easytoggle
	- http://www.quirksmode.org/js/events_properties.html 
*/
    
	
	
	
	
/* Alternative to Scott Andrew's function (below) by Simon Willison - adds IE 5/Mac compatiblity */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}




/* load toggle groups */
addLoadEvent(et_init);


//global to keep track of toggle groups
var et_toggleGroups = [];



/* Initialization 
	scans all links for 'class="anything toggle anything"'
	toggle groups are formed by class name, so all links within
	a group must be identical
*/
function et_init() {
    var link, id, target, group;
	
	for (var i = 0; (link = document.links[i]); i++) {
		//check for links with class containing word toggle
		if (/toggle/.exec(link.className)) {
			
			//see if group exists
			group = et_find_group(link.className);
			if(!group) {
				//create it if it doesn't
				group = new et_group(link.className);
				et_toggleGroups[et_toggleGroups.length] = group;
			}
	
			//find corresponding element and add to group
			id = link.href.split('#')[1];
			target = document.getElementById(id);
			target.id = 'et_'+id;
			group.addElement(target);
			
			//hide corresponding anchor, if it exists
			et_remove_anchor(id);
			
			if(group.first) {
				//show first element from each group
				group.first = false;
				link.parentNode.id = group.name + 'sel';
				link.parentNode.className = 'et_sel';
			} else {
				//hide all others
				target.style.display = 'none';
			}
			
			//set up toggle event
			link.onclick = et_event;
		}
	}
	
	et_set_status_from_url();
}
	

/* Toggle Function
	figures out which group has toggle event (link clicked)
	shows element corresponding to link
	hides all other elements within that group
*/
function et_event(e) {
	var current, source, elem, id;
	
	if(typeof e == 'undefined') { var e = window.event; } 
	
	//find new selection / current group
	if(typeof e.target != 'undefined') { source=e.target; } 
	else if (typeof e.srcElement != 'undefined') { source = e.srcElement;	} 
	else { return(true); }

	//in case there were other elements within the link
	while ((typeof source.nodeName == 'undefined') || (source.nodeName != "A" && source.nodeName != "a")) {
        source = source.parentNode;
    }
	
	if( (group = et_find_group(source.className)) ) {
		id = source.href.split('#')[1];
		group.toggle(id);
		
		//keep history cleaned so back goes to previous page
		if(typeof location.replace != 'undefined' 
		   && !(/OmniWeb/.exec(navigator.userAgent))) {
			location.replace('#'+id);
		}
	}
	
	return(false);
}


function et_set_status_from_url() {
	var id = ("test"+window.location).split('#')[1];
	var group;
	
	//sometimes browsers already jumped to the link,
	// so go back to the top
	if(et_toggleGroups.length != 0) {
		window.scrollTo(0,0);
	}
	for(var i=0; (group = et_toggleGroups[i]); i++) {
		if(group.hasElement(id)) {
			group.toggle(id);
			break;
		}
	}
	
	return;
}

/* helper to remove anchor elements, to prevent browser from scrolling */
function et_remove_anchor(id) {
	var anchor;
	
	for(var j=0; (anchor = document.anchors[j]); j++) {
		if(anchor.name == id) {
			anchor.parentNode.removeChild(anchor);
			break;
		}
	}

	return;
}

/* helper to find group element belongs to, by classname */
function et_find_group(name) {
	var i, group;
	
	for(i = 0; (group = et_toggleGroups[i]); i++) {
		if(group.name == name) {
			return(group);
		}
	}
	
	return(false);
}


/* helper to search for link corresponding to "panel" */
function et_find_toggle_link(id) {
	var i, link, id;
	
	for (i = 0; (link = document.links[i]); i++) {
		if(/toggle/.exec(link.className)) {
			if(link.href.split('#')[1] == id) {
				return(link);
			}
		}
	}
	
	return(false);
}





/******
	toggle group data structure
******/
function et_group(name) { 
	this.name = name;
	this.elements = [];	//all elements in group
	this.first = true;		//for tracking first in each group
	//void addElement(element)
	//bool hasElement(id)
	//void toggle(id)
}


function et_group_add_element(element) {
	this.elements[this.elements.length] = element;
}
et_group.prototype.addElement = et_group_add_element;


function et_group_has_element(id) {
	var elem;
	for(var i=0; (elem = this.elements[i]); i++) {
		if(elem.id == 'et_'+id) {
			return(true);
		}
	}
	return(false);
}
et_group.prototype.hasElement = et_group_has_element;


function et_group_select_element(id) {
	var elem;
	//remove style from current group selection
	var current = document.getElementById(this.name+'sel');
	if(current) {
		current.id = '';
		current.className = '';
	}
	var source = et_find_toggle_link(id);
	
	//show new selection and do actual hide
	for(var i = 0; (elem = this.elements[i]); i++) {
		if(elem.id != 'et_'+id) {
			elem.style.display = 'none';
		} else {
			elem.style.display = 'block';
			if(source) {
				source.parentNode.id = this.name+'sel';
				source.parentNode.className = 'et_sel';
			}
			else { alert("No Source"); }
		}
	}
}
et_group.prototype.toggle = et_group_select_element;
