// Requires SpryAccordion.js

Spry.Widget.Accordion.prototype.cycleThruPanels = function()
{
	var panelCount = this.getPanels().length;
	if( panelCount > 0 )
	{
		if( this.getCurrentPanelIndex() < panelCount - 1 )
			this.openNextPanel();
		else
			this.openFirstPanel();
	}
}

Spry.Widget.Accordion.prototype.startPanelCycle = function()
{
	if( this.enableAutomaticCycling )
	{
		if( !this.cycleInterval )
			this.cycleInterval = 2000;

		var self = this;
		this.cyclerID = setInterval( function(){ self.cycleThruPanels() }, this.cycleInterval );
	}
}

Spry.Widget.Accordion.prototype.stopPanelCycle = function()
{
	clearInterval( this.cyclerID );
}

Spry.Widget.Accordion.prototype.restartPanelCycle = function()
{
	this.stopPanelCycle();
	this.startPanelCycle();
}

Spry.Widget.Accordion.prototype.baseAttachBehaviors = Spry.Widget.Accordion.prototype.attachBehaviors;
Spry.Widget.Accordion.prototype.attachBehaviors = function()
{
	this.baseAttachBehaviors();

	if( this.enableAutomaticCycling )
	{
		var acc = Spry.Widget.Accordion;
		var self = this;
		acc.addEventListener( this.element, "mouseover", function() { self.stopPanelCycle() }, false );
		acc.addEventListener( this.element, "mouseout", function () { self.restartPanelCycle() }, false );
		acc.addEventListener( this.element, "focus", function() { self.stopPanelCycle() }, false );
		acc.addEventListener( this.element, "blur", function() { self.restartPanelCycle() }, false );
		
		this.startPanelCycle();
	}
}
