// slideshow-flex.js version 2.2 2007-04-06
//
// a flexible slideshow, of anthing you can fit into a <div></div>
//
// base: http://www.webaware.com.au/free/slideshow-flex/
//
// copyright © 2006-2007 WebAware Pty Ltd
//---------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// Full license: http://www.webaware.com.au/free/license.htm
//---------------------------------------------------------------------
// methods:
// 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.html
//---------------------------------------------------------------------

// SlideshowFlex constructor
function SlideshowFlex(div_name, delay)
{
	// add members
	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;
	
	// allocate an ID for this instance
	this.id = SlideshowFlex.AllocateID(this);
	
	// hook it into the page load event
	var hook = Function('SlideshowFlex.GetPointer(' + this.id + ').Install()');

	if (window.addEventListener)
		window.addEventListener("load", hook, false);
	else if (window.attachEvent)
		window.attachEvent("onload", hook);
}

// class variables

SlideshowFlex.pointers = new Array();

// class methods

SlideshowFlex.AllocateID = function(ptr)
{
	var i = SlideshowFlex.pointers.length;
	SlideshowFlex.pointers[i] = ptr;
	return i;
}

SlideshowFlex.GetPointer = function(id)
{
	return SlideshowFlex.pointers[parseInt(id, 10)];
}

// instance public methods

SlideshowFlex.prototype.Install = function()
{
	this.div_elem = document.getElementById ? document.getElementById(this.div_name) : eval("document.all." + this.div_name);
	this.Restart();
}

SlideshowFlex.prototype.NextPage = function()
{
	this.Stop();
	this.LoopForward();
	this.timer = setInterval('SlideshowFlex.GetPointer(' + this.id + ').LoopForward()', this.delay);
}

SlideshowFlex.prototype.PrevPage = function()
{
	this.Stop();
	this.LoopBackward();
	this.timer = setInterval('SlideshowFlex.GetPointer(' + this.id + ').LoopBackward()', this.delay);
}

SlideshowFlex.prototype.Stop = function()
{
	if (this.timer != "")
	{
		clearInterval(this.timer);
		this.timer = "";
	}
};

SlideshowFlex.prototype.Restart = function()
{
	if (document.getElementById || document.all)
	{
		this.Stop();
		this.page_index = -1;
		this.NextPage();
	}
};

// instance "private" methods

SlideshowFlex.prototype.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;
	}
}

SlideshowFlex.prototype.LoopBackward = function()
{
	try
	{
		var i = this.page_index;
		for (;;) 
		{
			if (--i < 0)
				i = this.div_elem.childNodes.length - 1;

			if (this.div_elem.childNodes[i].tagName == "DIV") 
			{
				this.ShowPage(i);
				break;
			}
		}
	}
	catch(e)
	{
		window.status = e.description;
	}
}

SlideshowFlex.prototype.ShowPage = function(page_index)
{
	try
	{
		for (var i = this.div_elem.childNodes.length; --i >= 0; )
		{
			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('SlideshowFlex.GetPointer(' + this.id + ').FadeIn(10)', 100);
				} else {
					el.style.display = "none";
				}
			}
		}
		this.page_index = page_index;
	}
	catch(e)
	{
		window.status = e.description;
	}
}

SlideshowFlex.prototype.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 += 10;
			window.setTimeout('SlideshowFlex.GetPointer(' + this.id + ').FadeIn(' + opacity + ')', 100);
		}
	}
	catch(e)
	{
		window.status = e.description;
	}
}

