var tabs = [];
var clickafter;
var bookMark;
function initTabs()
{
	checkBookMark();
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1 || sets[i].className.indexOf("moretabs") != -1)
		{
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					if (links[j].parentNode.className.indexOf("active") != -1 || links[j].hash.substr(links[j].hash.indexOf("#")+1) == bookMark)
					{
						var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));
						if (c){
							clickafter = links[j];
						}
					}
					links[j].onclick = function ()
					{
						// debugMsg(this.href.substr(this.href.indexOf("#")));
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								//This if statement makes sure to not reset the main tab is another main tab hasn't been clicked
									if (this.parentNode.parentNode.className == "tabset") {
									this.tabs[i].parentNode.className = this.tabs[i].parentNode.className.replace("active", "");
								}
							}

							
							//this if statement doesn't add extra active classes out there							
							if (this.parentNode.parentNode.className == "tabset") {
								this.parentNode.className += " active";
							} 
							
							/** else if (this.parentNode.parentNode.className == "moretabs") {
								_div = document.getElementById(this.href.substr(this.href.indexOf("#")));
								_div.parentNode.className += " active";
							} */
							
							c.style.display = "block";
							dhtmlHistory.add("old"+c.id, document.getElementById(c.id).innerHTML);
							return false;
						}
					}
				}
			}
		}
	}
	if(clickafter){
		clickafter.onclick();
	}
}

/* these are the functions for history and bookmarks */
function initialize() {

            // initialize our DHTML history
            dhtmlHistory.initialize();
            // subscribe to DHTML history change
            // events
            dhtmlHistory.addListener(historyChange);
            
            // if this is the first time we have
            // loaded the page...
            if (dhtmlHistory.isFirstLoad()) {
               // start adding history

            }
}		

		 /** Our callback to receive history 
             change events. */
function historyChange(newLocation,historyData) {
            var historyMsg = historyData;
            
            if (newLocation == "complexObject") {
               historyMsg =JSON.stringify(historyData);
               
            }
}

//usage:  debugMsg("what ever you want to see on the screen, e.g. "+links[j]);
//place in the javascript functions to determine flow and error source
function debugMsg(msg) {
	var debbugMsg = document.getElementById("debugMsg");
	debbugMsg.innerHTML += msg+"<br>";
	return;
}

function checkBookMark() {
	if (window.location.hash != "" && window.location.hash != "#oldtab1"){
		bookMark= window.location.hash.substr(window.location.hash.indexOf("d")+1);
	} else {
		bookMark = window.location.hash;
	}
	return;
	
}


/* this starts all the javascripts when the page is loaded */
if (window.addEventListener){
		window.addEventListener("load", initialize, false);
		window.addEventListener("load", initTabs, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initialize);
	window.attachEvent("onload", initTabs);
}
