function Fader( div_id, elem_id )
{
	this.elem_id = elem_id;
	this.elemText_id = elem_id + "Text";
	this.div_id = div_id;
	this.images = new Array();
	this.texts = new Array();
	this.timeout = 1000;
	this.transition = 'ALPHA';
	this.preload = false;

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

	this.Start = function()
	{
		this.images_cnt = this.images.length;
		if( this.preload )
			this.Preload();

		var self = this;
		this.timer = setTimeout( function(){self.TransitionBlend();}, this.timeout );
	}
	
	this.Stop = function()
	{
		if (this.timer)
		{
			clearTimeout(this.timer);
			this.timer = false;
		}
	}
	
	this.IsRunning = function()
	{
		return (this.timer != false);
	}

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

	this.Switch = function()
	{
		var elem = document.getElementById( this.elem_id );
		var elemText = document.getElementById( this.elemText_id );
		elem.src = this.imgs[ this.image_ct ].src;
		elemText.innerHTML = this.texts[ this.image_ct ];
	}

	this.PrepareNext = 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.PreparePrev = function()
	{
		this.image_ct--;
		if( this.image_ct < 0 )
			this.image_ct = this.images_cnt - 1;

		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.PrepareNext();

		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.Switch();
			this.transition_dir = 1;
		}

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