// slideshow-flex.js
// a flexible slideshow, anything you can fit into a <div></div>
// copyright © 2006 WebAware Pty Ltd
//---------------------------------------------------------------------
// functions:
// NextPage		show next page (wraps around to start)
// PrevPage		show previous page (wraps around to end)
// Stop			stop slideshow
// Restart		restart slideshow
//---------------------------------------------------------------------
// * IE requires the divs to have width &/or height set, or you must set
//   position: absolute, to give the div "layout" so that opacity works
//
// * IE needs background-color set on the divs - see this link for more:
//   http://jszen.blogspot.com/2005/04/ie-bold-text-opacity-problem.txt
//---------------------------------------------------------------------
// --------------------------------------------------------------------
// SlideshowFlex constructor
// --------------------------------------------------------------------
function SlideshowFlex(name, div_name, delay)
{
	// add members
	this.name = name;
	this.div_name = div_name;
	this.div_elem = document.getElementById ? document.getElementById(this.div_name) : eval("document.all." + this.div_name);
	this.delay = delay;
	this.timer = "";
	this.page_index = -1;
	
	// add public methods
	
	this.NextPage = function()
	{
		this.Stop();
		this.LoopForward();
		this.timer = setInterval(this.name + '.LoopForward()', this.delay);
	}
	
	this.PrevPage = function()
	{
		this.Stop();
		this.LoopBackward();
		this.timer = setInterval(this.name + '.LoopBackward()', this.delay);
	}
	
	this.Stop = function()
	{
		if (this.timer != "")
		{
			clearInterval(this.timer);
			this.timer = "";
		}
	};

	this.Restart = function()
	{
		if (document.getElementById || document.all)
		{
			this.Stop();
			this.page_index = -1;
			this.NextPage();
		}
	};

	// add "private" methods
	
	this.LoopForward = function()
	{
		try
		{
			var l = this.div_elem.childNodes.length;
			var i = this.page_index;
			for (;;) {
				if (++i == l)
					i = 0;
				if (this.div_elem.childNodes[i].tagName == "DIV") 
				{
					this.ShowPage(i);
					break;
				}
			}
		}
		catch(e)
		{
			window.status = e.description;
		}
	}
	
	this.LoopBackward = function()
	{
		try
		{
			var l = this.div_elem.childNodes.length;
			var i = this.page_index;
			for (;;) {
				if (--i < 0)
					i = l - 1;
				if (this.div_elem.childNodes[i].tagName == "DIV") 
				{
					this.ShowPage(i);
					break;
				}
			}
		}
		catch(e)
		{
			window.status = e.description;
		}
	}
	
	this.ShowPage = function(page_index)
	{
		try
		{
			var l = this.div_elem.childNodes.length;
			for (var i = 0; i < l; i++)
			{
				var el = this.div_elem.childNodes[i];
				if (el.tagName == "DIV") 
				{
					if (i == page_index)
					{
						el.style.opacity = 0.0
						if (el.filters)
							el.style.filter = "alpha(opacity=0)";
						else if (el.style.MozOpacity)
							el.style.MozOpacity = 0.0;
						el.style.display = "block";
						window.setTimeout(this.name + ".FadeIn(10)", 100);
					} else {
						el.style.display = "none";
					}
				}
			}
			this.page_index = page_index;
		}
		catch(e)
		{
			window.status = e.description;
		}
	}
	
	this.FadeIn = function(opacity)
	{
		try
		{
			var el = this.div_elem.childNodes[this.page_index];
			el.style.opacity = opacity / 100;
			if (el.filters)
				el.style.filter = "alpha(opacity=" + opacity + ")";
			else if (el.style.MozOpacity)
				el.style.MozOpacity = opacity / 100;
			if (opacity < 100)
			{
				opacity += 5;
				window.setTimeout(this.name + ".FadeIn(" + opacity + ")", 100);
			}
		}
		catch(e)
		{
			window.status = e.description;
		}
	}

	// kick it off
	this.Restart();
}
