// JavaScript Document

// Set up the Tabs at window load
addLoadEvent(setup_Tabs)

var firstRun = ""

function setup_Tabs() {
	// Get the tab elementss
	var tabArr = getElementsByClass('tab');
	
	// loop round all the inner elements and give them click functionality
	for ( j=0; j in tabArr; j++  ) {
		tabArr[j].onclick = getData;
	};				

	// if it's the first tab... get the first tabs data and turn it on
	firstRun = tabArr[0];
	getData();

}

function getData() {
	if ( firstRun != "" ) {
		// Get the tab elementss
		var obj = firstRun;
		firstRun="";
	} else {
		var obj = this;
	}
	// get it's first inner element
	var thisChild = obj.firstChild;
	// loop round all the inner elements
	while ( thisChild != obj.lastChild ) {
		// if this inner element is the name span
		if ( thisChild.className == "content" ) {
			tabOn(obj,thisChild.id);
	
			break;
		}
		thisChild = thisChild.nextSibling;
	};		
}

function tabOn(tab,dataId) {
	// set the current tab to class 'on'
	tab.id = "on"

	// if an innerdata id exists load the relevant data
	if ( dataId != '' ) { document.getElementById('tab-content').innerHTML = document.getElementById(dataId+'-Data').innerHTML };

	// Get the tabs elements 
	var tabArr = getElementsByClass('tab');

	// loop round all the inner elements
	for ( i=0; i in tabArr; i++  ) {
		// if this tab is not the one we've just turned on turn it off
		if ( tabArr[i] != tab ) { tabArr[i].id = "" };
	};				

}
