//input = id, array of tabs to hide
function showOneAndHideTheRest(showID, groupIDsToHide, displayValue)
{//showID can sanely appear in groupIDstoHide, displayValue is for the one to be shown...
	var element;
	element = document.getElementById(showID);
	if (element==null)
	{//cool story bro; but no such element
		return false;
	}
	
	if (displayValue == null)
	{
		displayValue = "block";//"inline";
	}
	element.style.display=displayValue;
	
	var len = groupIDsToHide.length;
	var i;
	var ret=true;
	for(i=0; i<len; i++)
	{
		var id = groupIDsToHide[i];
		if (id !=showID)//don't hide the one we want to show!
		{
			element = document.getElementById(id);
			if (element == null)
			{
				//element not found
				ret = false;
			}
			else
			{
				element.style.display = "none";
			}
		}
	}
	return ret;
}

function TimedTabs(groupOfIDs, startingArrayIndex, interval)
{
	this.group = groupOfIDs;
	this.index = startingArrayIndex;
	this.interval = interval;
	var self= this;
	this.timerID = null;
	this.displayValue = null;//use default of "inline"
	
	this.start = function()
	{
		if (self.timerID != null)
		{
			self.stop();
		}
		self.refresh();
		//self.timerID=window.setTimeout(self.onTimer, self.interval);
		self.timerID=window.setInterval(self.onTimer, self.interval);
	};
	
	this.stop = function()
	{
		if (self.timerID!=null)
		{
			//window.clearTimeout(self.timerID);
			window.clearInterval(self.timerID);
			self.timerID=null;
		}
	};
	
	this.onTimer = function()
	{
		if (self.isStopped())
		{
			return false;//stopped!
		}
		self.index+=1;
		self.index %= self.group.length;
		self.refresh();
	};
	
	this.isStopped = function()
	{
		return (self.timerID == null);
	};
	
	this.refresh=function()
	{
		var currentIndex = self.index % self.group.length;
		var currentID = self.group[currentIndex];
		var resultCode;
		resultCode=showOneAndHideTheRest(currentID, self.group, self.displayValue);
		if (!resultCode)
		{
			;//fyi, something went wrong if we've reached here!
		}
	};
	
	this.getGroupIDs=function()
	{
		return self.group;//please don't modify this return value!!! (PLEASE!)
	};
	
	return this;
}


function startTabs(firstID, groupOfIDs, interval)
{//returns a timedTabs object...; interval is in ms
	var indexOf = -1;
	var i, l = groupOfIDs.length;
	for(i=0; i<l; i++)
	{
		if (groupOfIDs[i]==firstID)
		{
			indexOf=i;
			break;
		}
	}
	if (indexOf==-1)
	{
		//firstID not in groupOfIds...!
		return false;
	}
	
	var timedTabs = new TimedTabs(groupOfIDs, indexOf, interval);
	timedTabs.start();
	return timedTabs;
}

function setTabToAndStop(timedTabs, toID)
{
	timedTabs.stop();
	var resultCode;
	resultCode = showOneAndHideTheRest(toID, timedTabs.getGroupIDs(), null);
	
	return resultCode;//false does NOT MEAN WE SUCEEEDED
}

function queueOnLoad(func)
{
	var newfunc = func;
	var oldLoadFunc = window.onload;
	window.onload=function()
	{
		//alert("abc123");
		try
		{
			if (oldLoadFunc!=null)
			{
				oldLoadFunc();
			}
		}
		finally
		{
			newfunc();
		}
	};
	//func();//hack
}
/*
//usage example for queueOnLoad:
queueOnLoad(function()
{
	{blah}//function call
});
*/

var eds = 
{
	tabs: new Array(),//replace me
	first_tab: null,//set this before you invoke startIt()
	interval: 15*1000, //in ms
	timedTabs: null,
	stopped: false
};

function startIt_helper()
{
	if (eds.stopped==true)
	{
		return;
	}
	eds.timedTabs = startTabs(eds.first_tab, eds.tabs, eds.interval);
}



function startIt()
{
	eds.stopped=false;//clear the flag
	queueOnLoad(startIt_helper);
}

function stopIt(andShowID)
{
	eds.stopped=true;
	if (eds.timedTabs!=null)
	{
		setTabToAndStop(eds.timedTabs, andShowID);//note: restarting things would require some updating from here for the indices...
	}
}

function setStartingTabID(startWithTabID)
{
	eds.first_tab = startWithTabID;
}

function setTheTabIDs(tabIDsArray)
{
	eds.tabs = tabIDsArray;
}
//public api:
	//startIt() -- starts the cycling
	//stopIt(andShowID) -- stops the cycling and shows the specified id
	
	////call before using the above two;
	//setStartingTabID(startWithTabID)	-- the tab id to start on with startIt
	//setTheTabIDs(tabIDsArray) -- the set of tabIDs to cycle through, MUST contain the starting tab before startIt is called!
