function Fader( div_id, elem_id )
{
	this.elem_id = elem_id;
	this.div_id = div_id;
	this.images = new Array();
	this.images_timeouts = new Array();
	this.timeout = 1000;
	this.preload_timeout = 1000;
	this.transition = 'ALPHA';
	this.preload = true;
	this.start_delay = 500;
	this.default_timeout;

	this.image_ct = 1;
	this.images_cnt = 0;
	this.timer = false;
	this.preload_timer = false;
	this.imgs = new Array();
	this.current_transition = 100;
	this.transition_timeout = 100;
	this.transition_dir = -1;

	this.Start = function()
	{
		this.default_timeout = this.timeout;
		this.images_cnt = this.images.length;

		if( this.start_delay < -this.timeout )
			this.start_delay = -this.timeout;

		if( this.images_timeouts && this.images_timeouts[ 0 ] )
			this.timeout = this.images_timeouts[ 0 ];

		this.PseudoPreload();

		var self = this;
		if( this.preload )
			this.RealPreload();
		else
			this.timer = setTimeout( function(){self.TransitionBlend();}, this.start_delay + this.timeout );
	}

	this.PseudoPreload = function()
	{
		for( i=0; i<this.images_cnt; i++ )
		{
			this.imgs[ i ] = new Image();
			this.imgs[ i ].src = this.images[ i ];
		}
	}

	this.RealPreload = function()
	{
		var self = this;

		for( i=0; i<this.images_cnt; i++ )
		{
			if( !this.imgs[ i ].complete )
			{
				this.preload_timer = setTimeout( function(){self.RealPreload();}, this.preload_timeout );
				return;
			}
		}

		clearTimeout( this.preload_timer );
		this.timer = setTimeout( function(){self.TransitionBlend();}, this.start_delay + this.timeout );
	}

	this.Next = function()
	{
		var elem = document.getElementById( this.elem_id );
		elem.src = this.imgs[ this.image_ct ].src;
		this.timeout = this.default_timeout;

		if( this.images_timeouts && this.images_timeouts[ this.image_ct ] )
			this.timeout = this.images_timeouts[ this.image_ct ];
	}

	this.NextBg = function()
	{
		this.image_ct++;
		if( this.image_ct >= this.images_cnt )
			this.image_ct = 0;

		if( !this.imgs[ this.image_ct ] )
		{
			this.imgs[ this.image_ct ] = new Image();
			this.imgs[ this.image_ct ].src = this.images[ this.image_ct ];
		}

		var elem = document.getElementById( this.div_id );
		elem.style.backgroundImage = 'url('+this.imgs[ this.image_ct ].src+')';
	}

	this.TransitionBlend = function()
	{
		var step = 15;
		var max_transition = 100;

		var min_transition = 0;

		this.current_transition += step * this.transition_dir;

		if( this.transition_dir == 1 )
		{
			if( this.current_transition >= max_transition )
				this.current_transition = max_transition;
		}
		else
		{
			if( this.current_transition <= min_transition )
				this.current_transition = min_transition;
		}
		if( this.current_transition == max_transition )
			this.NextBg();

		var elem = document.getElementById( this.elem_id );
		if( elem.filters )
			elem.filters.alpha.opacity = this.current_transition;
		elem.style.MozOpacity = this.current_transition / 100;

		if( this.transition_dir == 1 && this.current_transition >= max_transition )
		{
			this.transition_dir = -1;
			var self = this;
			this.timer = setTimeout( function(){self.TransitionBlend();}, this.timeout );
			return;
		}
		else if( this.transition_dir == -1 && this.current_transition <= min_transition )
		{
			this.Next();
			this.transition_dir = 1;
		}

		var self = this;
		this.timer = setTimeout( function(){self.TransitionBlend();}, this.transition_timeout );
	}
}
