// fade-image.js version 2.2 2007-04-06
//
// A JavaScript class for displaying images with fade-in
//
// base: http://www.webaware.com.au/free/fade-image/
//
// 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:
// FadeIn		fade in the image one more increment, until 100%
// Stop			stop fading in image
// Restart		set opacity to 0, and start fading in again
//---------------------------------------------------------------------
// images need to have the following in their CSS style (e.g. class):
//    opacity:0.0; -moz-opacity:0.0; filter:alpha(opacity=0);
//---------------------------------------------------------------------

// ImageFader constructor
// image_id		reference to image element to be faded in
// delay		delay in microseconds before image fades in completely
function ImageFader(image_id, delay)
{
	// add members
	this.image_id = image_id;
	this.delay = delay;
	this.timer = "";		// handle to setInterval() timer
	this.opacity = 0;		// current opacity of image, in percent (0-100)

	// allocate an ID for this instance
	this.id = ImageFader.AllocateID(this);
	
	// hook it into the page load event
	var hook = Function('ImageFader.GetPointer(' + this.id + ').Install()');

	if (window.addEventListener)
		window.addEventListener("load", hook, false);
	else if (window.attachEvent)
		window.attachEvent("onload", hook);
}

// class variables

ImageFader.pointers = new Array();

// class methods

ImageFader.AllocateID = function(ptr)
{
	var i = ImageFader.pointers.length;
	ImageFader.pointers[i] = ptr;
	return i;
}

ImageFader.GetPointer = function(id)
{
	return ImageFader.pointers[parseInt(id, 10)];
}

// instance public methods

ImageFader.prototype.Install = function()
{
	this.image = document.getElementById ? document.getElementById(this.image_id) : eval("document.all." + this.image_id);
	this.Restart();
}

ImageFader.prototype.FadeIn = function()
{
	if (this.image && this.opacity < 100)
	{
		this.opacity += 10;
		this.image.style.opacity = this.opacity / 100;
		if (this.image.filters)
			this.image.filters.alpha.opacity = this.opacity;
		else if (this.image.style.MozOpacity)
			this.image.style.MozOpacity = this.opacity / 100;
	}
	else if (this.timer != "")
		this.Stop();
}

ImageFader.prototype.Stop = function()
{
	if (this.timer != "")
	{
		clearInterval(this.timer);
		this.timer = "";
	}
}

ImageFader.prototype.Restart = function()
{
	this.Stop();
	this.opacity = -10;
	this.FadeIn();
	this.timer = setInterval('ImageFader.GetPointer(' + this.id + ').FadeIn()', this.delay);
}
