function RotatingContentSelectPanel(id, panel)
{
	RotatingContentGetForId(id).selectPanel(panel);
}

function RotatingContentAutoSelectNextPanel(id)
{
	RotatingContentGetForId(id).autoSelectNextPanel();
}

function RotatingContentSelectNextPanel(id)
{
	RotatingContentGetForId(id).selectNextPanel(id);
}

var RotatingContentMap = new Object(); //maps rotating content id to RotatingContent object;

//Returns the RotatingContent object for an id, creating the object
//if necessary.
function RotatingContentGetForId(id)
{
	var rotatingContent = RotatingContentMap[id];
	if(rotatingContent == null)
	{
		rotatingContent = new RotatingContent(id);
		RotatingContentMap[id] = rotatingContent;
	}
	return rotatingContent;
}

function _RotatingPanelAnimate(id)
{
	RotatingContentGetForId(id).animate();
}

//RotatingContent is a javascript object that represents
//the rotating content component in the HTML document. The
//constructor takes the id of the object.
function RotatingContent(id)
{
	this.id = id;
	this.selectedPanel = 0; //The panel that is currently selected
	
	//Default to null, which means no automatic rotation. Otherwise, the value
	//should be an int which represents the automatic rotation interval in 
	//milliseconds
	this.timeout = null; 
	
	//The total number of panels 
	this.numPanels = DomUtils.getChildrenWithTagName(this.getRotatingContentBodyElement(), 'div').length; 
	
	//Timeout 
	this.autoSelectNextPanelTimeout = null;
	
	//variables used in the transition's fade animation
	this.previousPanel = 0; //The panel that was previously selected
	this.animationStartDate; //date when the animation began
	this.animationIntervalId = null; //Identifies the interval timer being used for the animation
};

RotatingContent.prototype.getRotatingContentElement = function()
{
	return document.getElementById(this.id);
};

RotatingContent.prototype.getRotatingContentBodyElement = function()
{
	return DomUtils.getChildWithClassName(this.getRotatingContentElement(), 'RotatingContentBody');
};

RotatingContent.prototype.getRotatingContentPanelElement = function(panel)
{
	return DomUtils.getChildrenWithTagName(this.getRotatingContentBodyElement(), 'div')[panel];
};

RotatingContent.prototype.getHeaderElement = function()
{
	return DomUtils.getChildWithClassName(this.getRotatingContentElement(), 'RotatingContentHeader');
};

RotatingContent.prototype.getPageMarkersDiv = function()
{
	return DomUtils.getChildWithClassName(this.getHeaderElement(), 'RotatingContentPageMarkers');
};

RotatingContent.prototype.getPageMarkerElement = function(panel)
{
	return DomUtils.getChildrenWithTagName(this.getPageMarkersDiv(), 'div')[panel];
};

RotatingContent.prototype.autoSelectNextPanel = function()
{
	if(this.timeout == null)
	{
		//no automatic rotation
		return;
	}
	
	clearTimeout(this.autoSelectNextPanelTimeout);
	this.autoSelectNextPanelTimeout = setTimeout("RotatingContentSelectNextPanel('"+this.id+"')", this.timeout);
};

RotatingContent.prototype.selectNextPanel = function()
{
	var panel = (this.selectedPanel + 1) % this.numPanels;
	this.selectPanel(panel);
};

RotatingContent.prototype.selectPanel = function(panel)
{
	//reset className of old previous panel
	this.getRotatingContentPanelElement(this.previousPanel).className = 'RotatingContentPanelHidden';
	
	this.previousPanel = this.selectedPanel;
	this.selectedPanel = panel;
	
	if(this.getPageMarkersDiv() != null)
	{
		this.getPageMarkerElement(this.previousPanel).className = 'RotatingContentPageMarkerUnselected';
		this.getPageMarkerElement(this.selectedPanel).className = 'RotatingContentPageMarkerSelected';
	}
	
	var previousPanelElement = this.getRotatingContentPanelElement(this.previousPanel);
	previousPanelElement.className = 'RotatingContentPanelFadingOut';
	//Previous panel must be behind visible panel, but in front of other panels.
	UtilsSetOpacity(previousPanelElement, 1.0);
	
	var selectedPanelElement = this.getRotatingContentPanelElement(this.selectedPanel);
	selectedPanelElement.className = 'RotatingContentPanelVisible';
	UtilsSetOpacity(selectedPanelElement, 0.0);
	
	this.animationStartDate = new Date;
	if(this.animationIntervalId == null)
	{
		this.animationIntervalId = setInterval("_RotatingPanelAnimate('"+this.id+"')", 25);
	}
	this.autoSelectNextPanel();
};

RotatingContent.prototype.animate = function()
{
	var currentDate = new Date();
	var delta = (currentDate.getTime() - this.animationStartDate.getTime()) / 600.0;
	
	if(delta >= 1.0)
	{
		//complete the animation
		clearInterval(this.animationIntervalId);
		this.animationIntervalId = null;
		delta = 1.0
	}
	
	var selectedPanelElement = this.getRotatingContentPanelElement(this.selectedPanel);
	UtilsSetOpacity(selectedPanelElement, delta);
}

