/* Copyright 2009-2010 Taco Software. All rights reserved.
 * http://tacosw.com
 *
 * This file is part of the Component Library included in Taco HTML Edit.
 * Licensed users of Taco HTML Edit may modify and use this source code 
 * for their web development (including commercial projects), as long as 
 * this copyright notice is retained.
 *
 * The contents of this file may not be published in a format intended
 * for access by other humans, so you may not put code examples on a
 * web site with all or part of the contents of this file, and you may
 * not publish the contents of this file in a printed format.
 */

//Invoked to display a particular panel for the rotating content.
function tswRotatingContentSelectPanel(id, panel)
{
	tswRotatingContentGetForId(id).selectPanel(panel);
}

function tswRotatingContentAutoSelectNextPanel(id)
{
	tswRotatingContentGetForId(id).autoSelectNextPanel();
}

function tswRotatingContentSelectNextPanel(id)
{
	tswRotatingContentGetForId(id).selectNextPanel(id);
}

var tswRotatingContentMap = new Object(); //maps rotating content id to TSWRotatingContent object;

//Returns the TSWRotatingContent object for an id, creating the object
//if necessary.
function tswRotatingContentGetForId(id)
{
	var rotatingContent = tswRotatingContentMap[id];
	if(rotatingContent == null)
	{
		rotatingContent = new TSWRotatingContent(id);
		tswRotatingContentMap[id] = rotatingContent;
	}
	return rotatingContent;
}

function _tswRotatingPanelAnimate(id)
{
	tswRotatingContentGetForId(id).animate();
}

//TSWRotatingContent is a javascript object that represents
//the rotating content component in the HTML document. The
//constructor takes the id of the object.
function TSWRotatingContent(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 = TSWDomUtils.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
};

TSWRotatingContent.prototype.getRotatingContentElement = function()
{
	return document.getElementById(this.id);
};

TSWRotatingContent.prototype.getRotatingContentBodyElement = function()
{
	return TSWDomUtils.getChildWithClassName(this.getRotatingContentElement(), 'tswRotatingContentBody');
};

TSWRotatingContent.prototype.getRotatingContentPanelElement = function(panel)
{
	return TSWDomUtils.getChildrenWithTagName(this.getRotatingContentBodyElement(), 'div')[panel];
};

TSWRotatingContent.prototype.getHeaderElement = function()
{
	return TSWDomUtils.getChildWithClassName(this.getRotatingContentElement(), 'tswRotatingContentHeader');
};

TSWRotatingContent.prototype.getPageMarkersDiv = function()
{
	return TSWDomUtils.getChildWithClassName(this.getHeaderElement(), 'tswRotatingContentPageMarkers');
};

TSWRotatingContent.prototype.getPageMarkerElement = function(panel)
{
	return TSWDomUtils.getChildrenWithTagName(this.getPageMarkersDiv(), 'div')[panel];
};

TSWRotatingContent.prototype.autoSelectNextPanel = function()
{
	if(this.timeout == null)
	{
		//no automatic rotation
		return;
	}
	
	clearTimeout(this.autoSelectNextPanelTimeout);
	this.autoSelectNextPanelTimeout = setTimeout("tswRotatingContentSelectNextPanel('"+this.id+"')", this.timeout);
};

TSWRotatingContent.prototype.selectNextPanel = function()
{
	var panel = (this.selectedPanel + 1) % this.numPanels;
	this.selectPanel(panel);
};

TSWRotatingContent.prototype.selectPanel = function(panel)
{
	//reset className of old previous panel
	this.getRotatingContentPanelElement(this.previousPanel).className = 'tswRotatingContentPanelHidden';
	
	this.previousPanel = this.selectedPanel;
	this.selectedPanel = panel;
	
	if(this.getPageMarkersDiv() != null)
	{
		this.getPageMarkerElement(this.previousPanel).className = 'tswRotatingContentPageMarkerUnselected';
		this.getPageMarkerElement(this.selectedPanel).className = 'tswRotatingContentPageMarkerSelected';
	}
	
	var previousPanelElement = this.getRotatingContentPanelElement(this.previousPanel);
	previousPanelElement.className = 'tswRotatingContentPanelFadingOut';
	//Previous panel must be behind visible panel, but in front of other panels.
	tswUtilsSetOpacity(previousPanelElement, 1.0);
	
	var selectedPanelElement = this.getRotatingContentPanelElement(this.selectedPanel);
	selectedPanelElement.className = 'tswRotatingContentPanelVisible';
	tswUtilsSetOpacity(selectedPanelElement, 0.0);
	
	this.animationStartDate = new Date;
	if(this.animationIntervalId == null)
	{
		this.animationIntervalId = setInterval("_tswRotatingPanelAnimate('"+this.id+"')", 25);
	}
	this.autoSelectNextPanel();
};

TSWRotatingContent.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);
	tswUtilsSetOpacity(selectedPanelElement, delta);
}

/* The checksum below is for internal use by Taco HTML Edit, 
   to detect if a component file has been modified.
   TacoHTMLEditChecksum: D215A497 */
