var DEBUG_IMAGEFADERSET = false;// || navigator.userAgent.match(/firefox/i);

//function ImageFaderImage(src,w,h,margin,title,alt) 
function ImageFaderImage(src,w,h,title,alt) 
{
	this.src = src;
	this.w = w;
	this.h = h;
//	this.margin = margin;
	this.title = title;
	this.alt = alt;

	this.image = new Image();
	this.image.src = this.src;
}


function ImageFader(instance,img,autostart,delay,is_rand) 
{
	this.instance = instance;
	this.img = img;
	this.images = new Array();
	this.current = -1;
	this.delay = delay ? delay : 8000;
	this.is_rand = is_rand || 0;

	if (autostart) this.Start();
}

ImageFader.prototype.GetNumOfImages = function ()
{
	return this.images.length;
}

//ImageFader.prototype.AddImage = function (src,w,h,margin,title,alt) 
ImageFader.prototype.AddImage = function (src,w,h,title,alt) 
{
	var x = this.GetNumOfImages();
//	var temp_img = new ImageFaderImage(src,w,h,margin,title,alt);
	var temp_img = new ImageFaderImage(src,w,h,title,alt);
	this.images[x] = temp_img;
	if (this.current < 0) this.current = 0;
}

ImageFader.prototype.Rotate = function (no_recurse) 
{
	var n = this.GetNumOfImages();

	if (n <= 1) 
	{
		if (!no_recurse) this.Start(Math.min(1000,this.delay));
		return;
	}

	// get new current
		var x = this.current;

		// if we're getting random next image
			if (this.is_rand)
			{
				do
				{
					x = parseInt(Math.random() * n);
				}
				while (x == this.current);
			}
		// or is it sequential
			else
				x = (x + 1) % n;

	// rotate image...
		var img = document.getElementById(this.img); 
		
		if (img)
		{
			var the_filter = null;

			if (img.filters != null)
			{
				if (img.filters.length > 0)	// (ie mac check)
				{	
					if (img.filters[0]) the_filter = img.filters[0];
				}
			}

			if (the_filter)
				the_filter.apply();
			else
				img.style.visibility = "hidden";

if (DEBUG_IMAGEFADERSET) console.log(this.instance + ", changing " + this.img + " to " + this.images[x].image.src + "... ");

			img.src = this.images[x].image.src;

			if (the_filter) the_filter.play();

			// set margins and dimensions
				/*img.style.marginTop = this.images[x].margin[0] + "px";
				img.style.marginRight = this.images[x].margin[1] + "px";
				img.style.marginBottom = this.images[x].margin[2] + "px";
				img.style.marginLeft = this.images[x].margin[3] + "px";*/

				img.style.width = this.images[x].w + "px";
				img.style.height = this.images[x].h + "px";

			// set alt
				img.alt = this.images[x].alt;

			// set title
				img.title = this.images[x].title;

			if (!the_filter) img.style.visibility = "visible";

			
			this.current = x;
		}


	if (!no_recurse) this.Start();	
}

ImageFader.prototype.Start = function(delay) 
{
	if (!delay) delay = this.delay;

	setTimeout(this.instance + ".Rotate()",delay);

if (DEBUG_IMAGEFADERSET) console.log(this.instance + ", " + this.delay + " started... ");
}










function ImageFaderSet(instance,autostart,delay,is_rand) 
{
	this.instance = instance;
	this.faders = new Array();
	this.current = -1;
	this.delay = delay ? delay : 8000;
	this.is_rand = is_rand || 0;

	if (autostart) this.Start();
}


ImageFaderSet.prototype.GetNumOfImageFaders = function ()
{
	return this.faders.length;
}

ImageFaderSet.prototype.AddImageFader = function (img,autostart,delay,is_rand)
{
	var x = this.GetNumOfImageFaders();
	var temp_fader = new ImageFader(this.instance + ".faders[" + x + "]",img,autostart,delay,is_rand);
	this.faders[x] = temp_fader;
	if (this.current < 0) this.current = 0;

	return this.faders[x];
}

ImageFaderSet.prototype.Rotate = function () 
{
	var n = this.GetNumOfImageFaders();

	if (n <= 1) 
	{
		this.Start(Math.min(1000,this.delay));
		return;
	}

	// get new current
		var x = this.current;

		// if we're getting random next image
			if (this.is_rand)
			{
				do
				{
					x = parseInt(Math.random() * n);
				}
				while (x == this.current);
			}
		// or is it sequential
			else
				x = (x + 1) % n;

	// rotate image...
		this.faders[x].Rotate(true);

		this.current = x;

	this.Start();	
}



ImageFaderSet.prototype.Start = function(delay) 
{
	if (!delay) delay = this.delay;
	setTimeout(this.instance + ".Rotate()",delay);

if (DEBUG_IMAGEFADERSET) console.log(this.instance + ", " + this.delay + " started... ");
}