/**
 * Class Tabs
 *	Create a set of toggleable tabs.
 *
 * @author Dustin Lundebrek - dustin@lundebrek.com - Original creator
 * @author Ryan Sprake - ryan@eightdotthree.net - Added OOP to re-use the code on the same page
 */

function Tabs(tabNavigation_param)
{
	if(typeof tabNavigation_param == undefined)
	{
		return;
	}
	
	var tabNav;									// the base element of the navigation links
	var tabLinks;								// the navigation links
	var activeTab;								// the currently active tab
	var urlInternal = window.location + '';		// the url as a string
	
	var contentIDs = new Array();				// an array that will eventually hold IDs of the tabs
	var defaultTabId;
	
	var ACTIVE_CLASS = "active";
	var INACTIVE_CLASS = "inactive";
	
	// add the functions
	this.addTab = addTab;
	this.addTabNavigation = addTabNavigation;
	this.createTabNavigation = createTabNavigation;
	this.setActive = setActive;
	
	// if the tabNavigation was passed in, start the function.
	if(tabNavigation_param != null)
	{
		addTabNavigation(tabNavigation_param);
	};
	
	
	/**
	 * Function: add
	 *	Add a tab to the menu.
	 */
	function addTab()
	{
	};
	
	
	/**
	 * Function: addTabNavigation
	 *	Add the whole nav, and start it up.
	 */
	function addTabNavigation(tabNavigation_param)
	{		
		tabNav = document.getElementById(tabNavigation_param);
		tabLinks = tabNav.getElementsByTagName('a');
		
		createTabNavigation();
	};
	
	
	/**
	 * Function: setActive
	 *	Manage the classes on the active and inactive tabs, only two states, active, inactive.
	 */
	function setActive(active_param)
	{
		activeTab = active_param
		
		for(i = 0; i < tabLinks.length; i++)
		{			
			if(tabLinks[i] == activeTab)
			{
				tabLinks[i].parentNode.className = ACTIVE_CLASS;
			}
			else
			{
				tabLinks[i].parentNode.className = INACTIVE_CLASS;
			}
		}
	};
	
	
	/**
	 * Function: createTabNavigation
	 */
	function createTabNavigation()
	{
		// first, is there an internal link specified in the URL?
		if(urlInternal.indexOf('#') != -1)
		{	// if the url has a # sign...
			defaultTabId = urlInternal.replace(/.*#(.+)/,'$1');  // ...set defaultTabId to the value of the internal link
		}
		else
		{
			defaultTabId = null;  // otherwise, set defaultTabId to null
		}
	  
		// second, initialize each navigation link
		for(var i = 0; i < tabLinks.length; i++)
		{	// for each navigation link...
			contentIDs[i] = tabLinks[i].href.replace(/.*#(.+)/,'$1');	// set contentIDs[i] to the href of the value
			
			if(contentIDs[i] != defaultTabId)
			{  	// if this tab is NOT specified in the url...
				document.getElementById(contentIDs[i]).style.display = 'none';  // ...hide this tab
			}
			else
			{
				//tabLinks[i].className = 'selected';
			}
		
			tabLinks[i].onclick = function()
			{
				setActive(this);
				
				for(x = 0; x < contentIDs.length; x++)
				{  	// look at each tab ID
					if (contentIDs[x] == this.href.replace(/.*#(.+)/,'$1'))
					{  	// if the id is equal to the HREF of the link (minus the #)...
						document.getElementById(contentIDs[x]).style.display = 'block'  // ...show the element with that id
					}
					else
					{
						document.getElementById(contentIDs[x]).style.display = 'none'; // otherwise, hide the element with that id
					}
										
				}
				return false;  // stop the internal link
			};
		}
	  
		if(defaultTabId == null)
		{  	// if there is no default tab specified in the URL...
			document.getElementById(contentIDs[0]).style.display = 'block';  // ...show the first tab
			//tabLinks[0].className = 'selected';  // and give the first tab's nav link class 'selected' (this isn't necessary, but you can style 'selected' to indicate which tab is selected)
		}
		
		window.scrollTo(0, 0);  // scroll to the top of the page (for when there is an internal link)
		setActive(tabLinks[0]);
	};
};
