/*************************************************************************

  Implements the rotating highlight controller object for the BC Website.

  Original Author:  Brian Stucky, June 11, 2009

  Tested and works with:
	Firefox 3.0.10, WinXP
	Firefox 3.0.10, XUbuntu
	MS IE 8, WinXP
	MS IE 6, Win2000
	Opera 9.10, WinXP
	Konquerer 4.2.2, XUbuntu

  Tested and does not work with:

  Needs to be tested:
	Firefox 1.5, 2.0, WinXP
	Firefox 3, MacOS 10.5
	Safari, MacOS 10.5

  Copyright(c) Bethel College, 2009

**************************************************************************/

function HlController(divid, interval)
{
	this.hlelems = new Array();

	var container = document.getElementById(divid);

	// get all highlight elements
	var elems = container.childNodes;
	for (var cnt = 0; cnt < elems.length; cnt++)
	{
		if (elems[cnt].className == 'hlcontent')
		{
			if (elems[cnt].style.display == 'block')
				this.hlelems.unshift(elems[cnt]);
			else
				this.hlelems.push(elems[cnt]);
		}
	}

	this.count = 0;
	this.interval = interval * 1000;
	this.timer = 0;
	this.isplaying = false;
	this.hlcontroldisp = document.getElementById('hlcontroldisp');
	this.hlcontrolpause = document.getElementById('hlcontrolpause');
}

HlController.prototype.rotateContent = function(direction)
{
	var countold = this.count;

	switch (direction)
	{
	case 0:		//forward
		this.count = (this.count + 1) % this.hlelems.length;
		break;
	case 1:		//backward
	default:
		this.count = ((this.count - 1) + this.hlelems.length) % this.hlelems.length;
		break;
	}

	this.hlelems[this.count].style.display = 'block';
	this.hlelems[countold].style.display = '';

	this.hlcontroldisp.innerHTML = (this.count + 1) + ' of ' + this.hlelems.length;

	return false;
}

HlController.prototype.startAutoPlay = function()
{
	if (!this.isplaying)
	{
		var instance = this;
		this.timer = window.setInterval(function(){instance.rotateContent(0);}, this.interval);
		this.isplaying = true;
	}
}

HlController.prototype.stopAutoPlay = function()
{
	if (this.isplaying)
	{
		window.clearInterval(this.timer);
		this.timer = 0;
		this.isplaying = false;
	}
}

HlController.prototype.pauseHandler = function()
{
	if (this.isplaying)
	{
		this.stopAutoPlay();
		this.hlcontrolpause.src = "/lib/images/hlcontrol/hlcontrolpause-pressed.png";
	}
	else
	{
		this.startAutoPlay();
		this.hlcontrolpause.src = "/lib/images/hlcontrol/hlcontrolpause.png";
	}

	return false;
}

