var nTime = 23;	// num seconds of delay between (recommended 6)
var rotate = true;	// a flag, set false when tab is clicked to stop the rotating
var tabSelected = false;

var index = 0;

var tabs;
var tabRotationTimer;

function initRotation(){
	try{
		tabList = getTabList();
		for(i = 0; i < tabList.length; i++){
			//alert(tabRowTabs[i].getAttribute('id'));
		}
		addEvent(document.getElementById('tabBoxWrapper'), 'mouseover', stopRotating);
		addEvent(document.getElementById('tabBoxWrapper'), 'mouseout', restartRotating);
		tabRotationTimer = window.setInterval('rotateTabs(tabList)', nTime*1000);
		//rotateTabs(tabList);
	} catch(e){}
}

function getTabList(){
	return document.getElementById('tabBoxTabsAutoRotate').getElementsByTagName('div');
}

function hideTabBodies(tabList){
	for(i = 0; i < tabList.length; i++){
		var el = document.getElementById(tabList[i].getAttribute('id')+"Body");
		el.style.display = "none";
		var el = document.getElementById(tabList[i].getAttribute('id')+"");
		el.className = "inactive";						
	}
}


function rotateTabs(tabList){
	if(rotate){
		privateTabList = tabList;
		hideTabBodies(tabList);
		var el = document.getElementById(tabList[index].getAttribute('id')+"");
		el.className = "active";		
		var el = document.getElementById(tabList[index].getAttribute('id')+"Body");
		el.style.display = "block";				
		
		index++;
		index = index % tabList.length;
	}
}

function stopRotating(){
	window.clearInterval(tabRotationTimer);
}

function restartRotating(){
	if (!tabSelected){
		rotate = true;
		tabList = getTabList();
		tabRotationTimer = window.setInterval('rotateTabs(tabList)', nTime*1000);
	}
}

function selectTab(thisTab){
	rotate = false;
	tabSelected = true;
	window.clearInterval(tabRotationTimer);
	hideTabBodies(getTabList());
	var el = document.getElementById(thisTab+"Tab");
	el.className = "active";		
	var el = document.getElementById(thisTab+"TabBody");
	el.style.display = "block";				
	
	news_index = 0;
	nextHeadline(thisTab);
}		


// for the 'scroll' bars in the setanta rotating panels:

function nextHeadline(which){
	// that div/story exists...
	if(document.getElementById(which+"_"+(news_index+1))){
		// move on to the story:
		news_index++;
		this_story = document.getElementById(which+"_"+news_index);
		this_image = document.getElementById(which+"_"+news_index+"_img");

		// hide all the stories:
		for(i = 1; i < max_stories; i++){
			var el = document.getElementById(which+"_"+i);
			if(el){
				el.style.display = "none";
				
				try{
					var imger = document.getElementById(which+"_"+i+"_img");
					if(imger){
						imger.style.display = "none";
					}
				} catch(e){}
				
			}
		}

		// then show this one:
		this_story.style.display = "block";
		try{
			this_image.style.display = "block";
		} catch(e){}

	}
	// if there is no next story, do nothing
	rotate = false; // either way - stop
}


function previousHeadline(which){
	// that div/story exists...
	if(document.getElementById(which+"_"+(news_index-1))){
		// move on to the story:
		news_index--;
		this_story = document.getElementById(which+"_"+news_index);
		this_image = document.getElementById(which+"_"+news_index+"_img");

		// hide all the stories:
		for(i = 1; i < max_stories; i++){
			var el = document.getElementById(which+"_"+i);
			if(el){
				el.style.display = "none";
				try{
					var imger = document.getElementById(which+"_"+i+"_img");
					if(imger){
						imger.style.display = "none";
					}
				} catch(e){}
			}
		}

		// then show this one:
		try{
			this_story.style.display = "block";
			this_image.style.display = "block";
		} catch(e){}
	}
	// if there is no next story, do nothing
	rotate = false; // either way - stop
}


// for the non-rotating panels on the entertainment page

var subTab_index = 0;
			
function hide_the_sub_bodies(){
	for(i = 0; i < subTabs.length; i++){
			var el = document.getElementById(subTabs[i]+"TabBody");
		el.style.display = "none";
		var el = document.getElementById(subTabs[i]+"Tab");
		el.className = "inactive";						
	}
}

function switchTab(thisTab){
	rotate = false;
	hide_the_sub_bodies();
	var el = document.getElementById(thisTab+"Tab");
	el.className = "active";		
	var el = document.getElementById(thisTab+"TabBody");
	el.style.display = "block";				
}

var subTabSwitchDelay = nTime; //override this to change the subtab rotation delay (in seconds)
var subTabIndex = 0;
var subTabRotationTimer;
var subTabSelected = false;

function switchSubTab(thisTab){
	hide_the_sub_bodies();
	var el = document.getElementById(thisTab+"Tab");
	el.className = "active";		
	var el = document.getElementById(thisTab+"TabBody");
	el.style.display = "block";				
}

function selectSubTab(thisTab) {
	subTabSelected = true;
	switchSubTab(thisTab);
}

function stopSubTabRotating(){
	window.clearInterval(subTabRotationTimer);
}

function restartSubTabRotating(){
	if (!subTabSelected){
		subTabRotationTimer = window.setInterval('autoRotateSubTabs()', subTabSwitchDelay*1000);
	}
}


function autoRotateSubTabs() {
   
	switchSubTab(subTabs[subTabIndex]);
	subTabIndex++;
	if (subTabIndex >= subTabs.length)
		subTabIndex = 0;
}

function initSubTabs(){
	try{
		switchSubTab(subTabs[0]);
		
		if (document.getElementById('subTabBoxWrapper').getAttribute('autoRotate') == "true"){
			addEvent(document.getElementById('subTabBoxWrapper'), 'mouseover', stopSubTabRotating);
			addEvent(document.getElementById('subTabBoxWrapper'), 'mouseout', restartSubTabRotating);
			subTabRotationTimer = window.setInterval('autoRotateSubTabs()', subTabSwitchDelay*1000);
		}
	} catch(e){}
}

addEvent(window, 'load', initRotation);
addEvent(window, 'load', initSubTabs);
