﻿
function tabs(containerID, instanceID) {
	this.tabContainerID = containerID;
	this.tabInstanceID = instanceID;
	
	this.elements = {};
	this.elements.outer = document.getElementById(this.tabContainerID);
	this.elements.nav = "";
	this.elements.content = document.getElementById("div_TabsContent_" + this.tabContainerID);
	this.collections = {};
	this.collections.nav = {};
	this.collections.content = {};
	
	this.currentTab = 1;
	this.previousTab = -1;
	this.totalTabs = 0;
	this.defaultTab = 1;

	this.init = function () {
		this.collections.content = this.getChildNodes(this.elements.content, "DIV");
		this.totalTabs = this.collections.content.length;
		this.buildNav();
		for (i = 1; i < this.totalTabs; i++) {
			this.collections.content[i].style.display = "none";
		}
		this.gotoTab(this.defaultTab);
	}
	this.buildNav = function () {
		this.elements.nav = document.createElement("DIV");
		this.elements.nav.id = "div_TabsNav_" + this.tabContainerID;
		this.elements.nav.className = "div_TabsNav";
		var defaultTabName = window.location.href.replace(/([^#]*)#/gi, "").toLowerCase();
		for (var i = 0; i < this.totalTabs; i++) {
			var newNavButton = document.createElement("A");
			newNavButton.setAttribute("id", "a_TabNav_" + (i + 1));
			newNavButton.setAttribute("href", "#");
			newNavButton.setAttribute("name", this.tabInstanceID);
			//newNavButton.setAttribute("onclick", this.slideshowInstanceID + ".gotoSlide(this.innerHTML, true); return false;");
			newNavButton.onclick = function () { eval(this.name + ".gotoTab(" + this.id.replace("a_TabNav_", "") + ")"); return false; };
			newNavButton.setAttribute("title", "Jump to " + this.collections.content[i].title);
			//newNavButton.innerHTML = "<h2>" + this.collections.content[i].title.toString() + "</h2>";
			if (this.collections.content[i].title && this.collections.content[i].title.replace(/[^a-z0-9]/gi, "").toLowerCase() == defaultTabName) {
				this.defaultTab = i + 1;
			}
			var newNavButtonInner = document.createElement("H2");
			newNavButtonInner.innerHTML = this.collections.content[i].title;
			newNavButton.appendChild(newNavButtonInner);
			this.elements.nav.appendChild(newNavButton);
		}
		this.elements.outer.insertBefore(this.elements.nav, this.elements.content);
		this.collections.nav = this.elements.nav.getElementsByTagName("A");
	}
	this.gotoTab = function (tabNum) {
		this.previousTab = this.currentTab;
		this.currentTab = tabNum;
		if (this.previousTab > 0) this.hideTab(this.previousTab);
		this.showTab(this.currentTab);
	}
	this.showTab = function (tabNum) {
		this.collections.nav[tabNum - 1].className = "a_Current";
		this.collections.content[tabNum - 1].style.display = "block";
	}
	this.hideTab = function (tabNum) {
		this.collections.nav[tabNum - 1].className = "";
		this.collections.content[tabNum - 1].style.display = "none";
	}
	this.getChildNodes = function (parentNode, tagName) {
		if (parentNode.children) return parentNode.children;
		var childNodes = parentNode.childNodes;
		var childNodesOut = [];
		for (var i = 0; i < childNodes.length; i++) {
			if (childNodes[i].tagName) {
				if (!tagName || childNodes[i].tagName == tagName) {
					childNodesOut.push(childNodes[i]);
				}
			}
		}
		return childNodesOut;
	}

	this.init();
}
