var tabLinks = new Array();
var contentDivs = new Array();

var tabsideLinks = new Array();
var contentsideDivs = new Array();

var curpos = 0;
var starttab = 0;

var divcount = 3;
var interval = 15000;



function init(start, header, sidebar) 
{
	if(start)
	{
		// call NextTab every interval to move the tabs
		setInterval('NextTab()', interval);
	}

	if(header)
	{	
		initheader();
	}

	if(sidebar)
	{
		initsidebar();
	}
}

function initsidebar() 
{
	// Grab the tab links and content divs from the page
	var tabListItems = document.getElementById('sidetabs').childNodes;
	for (var i = 0; i < tabListItems.length; i++) 
	{
		if (tabListItems[i].nodeName == "LI") 
		{
			var tabLink = getFirstChildWithTagName(tabListItems[i], 'A');
			var id = getHash(tabLink.getAttribute('href'));
			tabsideLinks[id] = tabLink;
			contentsideDivs[id] = document.getElementById(id);
		}
	}

	// Assign onclick events to the tab links, and
	// highlight the first tab
	var i = 0;

	for (var id in tabsideLinks) 
	{
		tabsideLinks[id].onclick = showSideTab;
		tabsideLinks[id].onfocus = function() { this.blur() };
		if (i == starttab) 
		{
			tabsideLinks[id].className = 'selected';
		}
		i++;
	}

	// Hide all content divs except the first
	var i = 0;

	for (var id in contentsideDivs) 
	{
		if (i != starttab) 
		{
			contentsideDivs[id].className = 'tabContent hide';
		}
		i++;
	}
}

function initheader()
{
	// Grab the tab links and content divs from the page
	var tabListItems = document.getElementById('tabs').childNodes;
	for ( var i = 0; i < tabListItems.length; i++ ) 
	{
		if ( tabListItems[i].nodeName == "LI" ) 
		{
			var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
			var id = getHash( tabLink.getAttribute('href') );
			tabLinks[id] = tabLink;
			contentDivs[id] = document.getElementById( id );
		}
	}

	// Assign onclick events to the tab links, and
	// highlight the first tab
	var i = 0;
	  
	for ( var id in tabLinks ) 
	{
		tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == starttab )
        { 
        	tabLinks[id].className = 'selected';
        }
        i++;
	}

	// Hide all content divs except the first
	var i = 0;

	for ( var id in contentDivs ) 
	{
		if ( i != starttab )
		{ 
			contentDivs[id].className = 'tabContent hide';
		}
        i++;
	}
}

function showTab() 
{
	var selectedId = getHash( this.getAttribute('href') );
	setHeaderTab(selectedId);

	// Stop the browser following the link
	return false;
}

function showSideTab() {
	var selectedId = getHash(this.getAttribute('href'));
	setSideTab(selectedId);

	// Stop the browser following the link
	return false;
}
    
function NextTab()
{
	curpos = ++curpos % divcount;
    	
	switch(curpos)
	{
		case 0:
			setHeaderTab('lead');
			setSideTab('one');
			break;

		case 1:
			setHeaderTab('recent');
			setSideTab('two');
			break;

		case 2:
			setHeaderTab('poll');
			setSideTab('three');
			break;
	}
}
    
function setHeaderTab(selectedId)
{
	for ( var id in contentDivs ) 
	{
		if ( id == selectedId ) 
		{
			tabLinks[id].className = 'selected';
			contentDivs[id].className = 'tabContent';
		} 
		else 
		{
			tabLinks[id].className = '';
			contentDivs[id].className = 'tabContent hide';
		}
	}
}

function setSideTab(selectedId) {
	for (var id in contentsideDivs) {
		if (id == selectedId) {
			tabsideLinks[id].className = 'selected';
			contentsideDivs[id].className = 'sidetabContent';
		}
		else {
			tabsideLinks[id].className = '';
			contentsideDivs[id].className = 'sidetabContent hide';
		}
	}
}

function getFirstChildWithTagName( element, tagName ) 
{
	for ( var i = 0; i < element.childNodes.length; i++ ) 
	{
		if ( element.childNodes[i].nodeName == tagName )
		{ 
			return element.childNodes[i];
		}
	}
}

function getHash( url ) 
{
	var hashPos = url.lastIndexOf ( '#' );
	return url.substring( hashPos + 1 );
}