// PACKAGE SIMPLE TABS JAVASCRIPT

// RUN SETUP
/*window.onload = function() {
	prepareTabs();
	determineStartPage();
}*/

// DEFINE GLOBAL VARIABLES
var sCurrentPage = 't1';
var sLastPage = 'null';
function determineStartPage() {
	var pageContainer = document.getElementById('pages');
	var pages = pageContainer.getElementsByTagName('div');
	for (var i=0; i<pages.length; i++) {
		if (pages[i].className == 'current page') {
			for (var k=0; k<pages[i].attributes.length; k++) {
				if (pages[i].attributes.item(k).nodeName == 'id') {
					var _id = pages[i].attributes.item(k).nodeValue;
					sLastPage = _id;
					// alert(sLastPage);
				}
			}
		}
	}
}


// SET-UP TABS
function prepareTabs() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById('tabs')) return false;
	// FIND ALL THE ANCHOR TAGS
	var tabs = document.getElementById('tabs');
	var lis = tabs.getElementsByTagName('li');
	var links = tabs.getElementsByTagName('a');
	// ADD FUNCTIONALITY TO LINKS
	for (var i=0; i<links.length; i++) {
		links[i].onclick = function() {
			// UNSELECT CURRENT TAB
			for (var j=0; j<lis.length; j++) {
				if (lis[j].className == 'current') {
					lis[j].className = '';
				}
			}
			// SWAP PAGES
			removePound(this.getAttribute('href'));
			swapTab();
			// alert(this);
			return false;
		}
	}
}

// REMOVE # SYMBOL
function removePound(string) {
	aWhichTab = string.split('#');
	sCurrentPage = aWhichTab[1];
	// alert(sCurrentPage);
	// SWAP PAGES AND TABS
	showTabContents();
	//swapTab();	
}

// SWAP PAGES
function showTabContents() {
	document.getElementById(sLastPage).className = 'page';
	document.getElementById(sCurrentPage).className = 'current page';
	sLastPage = sCurrentPage;
}

//SWAP TABS
function swapTab() {
	// FIND ALL THE TABS
	var tabs = document.getElementById('tabs');
	var lis = tabs.getElementsByTagName('li');
	// REMEOVE CURENT CLASS FROM CLICKED ON TAB
	for (var i=0; i<lis.length; i++) {
		if (lis[i].className == 'current') {
			lis[i].className = '';
		// ASSIGN NEW CURRENT TAB
		} else {
			var links = lis[i].getElementsByTagName('a');
			for (j=0; j<links.length; j++) {
				var href = links[j].getAttribute('href');
				ahref = href.split('#');
				href = ahref[(ahref.length-1)];
				if (href == (sCurrentPage)) {
					lis[i].className = 'current';
				}
			}
		}
	}
}