function Fader()
{
	this.preload = false;
	this.preload_timeout = 1000;
	this.fader_images = new Array();
	this.fader_images_cnt = 0;
	this.start_delay = 0;

	this.next_fader_images = new Array();
	this.fader_timer = false;
	this.next_timeout = 0;
	this.child_wait_timeout = 50;
	this.min_timeout = 10;
	this.used_time = 0;

	this.AddFaderImage = function( faderImage )
	{
		this.fader_images[ this.fader_images_cnt ] = faderImage;
		this.fader_images_cnt++;
	}

	this.SetPreload = function( preload )
	{
		this.preload = ( preload ) ? true : false;
	}

	this.SetStartDelay = function( start_delay )
	{
		this.start_delay = start_delay;
	}

	this.Start = function()
	{
		if( this.fader_images_cnt <= 0 )
		{
			alert( 'NO FADER IMAGES' );
			return;
		}

		if( this.preload )
		{
			loaded = true;
			for( i=1; i<this.fader_images_cnt; i++ )
			{
				if( !this.fader_images[ i ].Preload())
					loaded = false;
			}

			var self = this;
			if( !loaded )
			{
				this.fader_timer = setTimeout( function(){self.Start();}, this.preload_timeout );
				return;
			}
		}

		this.Next();
	}

	this.Next = function()
	{
		this.next_fader_images = new Array();
		this.next_fader_images[ 0 ] = this.fader_images[ 0 ];
		this.next_timeout = this.fader_images[ 0 ].NextFade();

		for( i=1; i<this.fader_images_cnt; i++ )
		{
			var t = this.fader_images[ i ].NextFade();
			if( t < this.next_timeout )
			{
				this.next_timeout = t;
				this.next_fader_images = new Array();
				this.next_fader_images[ 0 ] = this.fader_images[ i ];
			}
			else if( t == this.next_timeout )
			{
				var ct = this.next_fader_images.length;
				this.next_fader_images[ ct ] = this.fader_images[ i ];
			}
		}

		for( i=0; i<this.fader_images_cnt; i++ )
			this.fader_images[ i ].SubstractNextFade( this.next_timeout );

		this.next_timeout -= this.used_time;
		this.used_time = 0;
		if( this.next_timeout < this.min_timeout )
			this.next_timeout = this.min_timeout;

		var self = this;

		if( this.start_delay )
		{
			if( this.start_delay <= -this.next_timeout )
				this.start_delay = -( this.next_timeout - 1 );

			tmp_start_delay = this.start_delay;
			this.start_delay = 0;

			this.fader_timer = setTimeout( function(){ self.Fade(); }, this.next_timeout + tmp_start_delay );
		}
		else
		{
			this.fader_timer = setTimeout( function(){ self.Fade(); }, this.next_timeout );
		}
	}

	this.Fade = function()
	{
		var lowest_state = 'TRANSITION_COMPLETED';

		for( i=0; i<this.next_fader_images.length; i++ )
		{
			var state = this.next_fader_images[ i ].state;
			if( state != 'TRANSITION_COMPLETED' )
			{
				lowest_state = state;
				break;
			}
		}

		if( lowest_state == 'TRANSITION_COMPLETED' )
		{
			for( i=0; i<this.next_fader_images.length; i++ )
				this.next_fader_images[ i ].state = 'UNDEFINED';

			this.Next();
			return;
		}

		var self = this;
		var lowest_timeout = false;

		for( i=0; i<this.next_fader_images.length; i++ )
		{
			this.next_fader_images[ i ].Fade();

			var timeout = this.min_timeout;
			if( i >= this.next_fader_images.length )
				i = this.next_fader_images.length - 1;

			if( lowest_state == 'TRANSITION_INIT' )
				timeout = this.next_fader_images[ i ].preload_timeout;
			else if( lowest_state == 'TRANSITION_EXECUTE' )
				timeout = this.next_fader_images[ i ].transition_timeout;

			if( !lowest_timeout )
				lowest_timeout = timeout;
			else if( timeout < lowest_timeout )
				lowest_timeout = timeout;
		}

		this.used_time += lowest_timeout;
		this.fader_timer = setTimeout( function(){self.Fade();}, lowest_timeout );
	}
}

function FaderImage( div_id, img_id )
{
	this.img_id = img_id;
	this.div_id = div_id;
	this.images_cnt = 0;
	this.images = new Array();
	this.images_timeouts = new Array();
	this.start_delay = 0;

	this.timer = false;
	this.preload_timer = false;
	this.imgs = new Array();
	this.current_transition = 100;

	this.default_timeout = 3000;
	this.transition_timeout = 100;
	this.preload_timeout = 1000;

	this.state = 'UNDEFINED';
	this.image_ct = 0;
	this.transition_step = 15;
	this.all_images_loaded = false;
	this.max_transition = 100;
	this.min_transition = 0;

	this.SetTransitionStep = function( step )
	{
		if( step <= 0 )
		{
			alert( 'INVALID TRANSITION STEP' );
			return;
		}
		this.transition_step = step;
	}

	this.SetTransitionTimeout = function( timeout )
	{
		if( timeout <= 0 )
		{
			alert( 'INVALID TRANSITION TIMEOUT' );
			return;
		}
		this.transition_timeout = timeout;
	}

	this.SetTimeout = function( timeout )
	{
		if( timeout <= 0 )
		{
			alert( 'INVALID TIMEOUT' );
			return;
		}
		this.default_timeout = timeout;
	}

	this.AddImage = function( img, showtime )
	{
		this.images[ this.images_cnt ] = img;
		if( !showtime || showtime < 0 )
		{
			if( !this.default_timeout || this.default_timeout <= 0 )
				alert( 'INVALID TIMEOUT' );
			showtime = this.default_timeout;
		}

		if( this.images_cnt <= 0 )
			this.next_timeout = showtime;

		this.images_timeouts[ this.images_cnt ] = showtime;
		this.images_cnt++;
	}

	this.SetStartDelay = function( start_delay )
	{
		this.start_delay = start_delay;
	}

	this.Preload = function()
	{
		if( this.AllImagesLoaded )
			return true;

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

		return false;
	}

	this.Fade = function()
	{
		if( this.state == 'UNDEFINED' )
			this.state = 'TRANSITION_INIT';

		if( this.state == 'TRANSITION_INIT' )
		{
			this.TransitionInit();
			return false;
		}

		if( this.state == 'TRANSITION_EXECUTE' )
		{
			this.TransitionExecute();
			return false;
		}

		if( this.state == 'TRANSITION_FINALIZE' )
		{
			this.TransitionFinalize();
			return false;
		}

		return true;
	}

	this.SubstractNextFade = function( timeout )
	{
		this.next_timeout -= timeout;
		if( this.next_timeout < 0 )
			alert( 'INVALID NEXT_TIMEOUT' );
	}

	this.NextFade = function()
	{
		return this.next_timeout;
	}

	this.TransitionInit = function()
	{
		this.current_transition = this.max_transition;

		if( !this.NextImageReady())
			return;

		this.Next();
		this.ChangeBackground();

		this.AllImagesLoaded();

		this.state = 'TRANSITION_EXECUTE';
	}

	this.AllImagesLoaded = function()
	{
		if( this.all_images_loaded )
			return true;

		for( i=0; i<this.images_cnt; i++ )
		{
			if( !this.imgs[ i ] )
				return false;

			if( !this.imgs[ i ].complete )
				return false;
		}

		this.all_images_loaded = true;
		return true;
	}

	this.TransitionExecute = function()
	{
		this.current_transition -= this.transition_step;
		if( this.current_transition <= this.min_transition )
			this.current_transition = this.min_transition;

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

		if( this.current_transition <= this.min_transition )
			this.state = 'TRANSITION_FINALIZE';
	}

	this.TransitionFinalize = function()
	{
		this.ChangeImage();
		this.state = 'TRANSITION_COMPLETED';
	}

	this.NextImageReady = function()
	{
		if( this.AllImagesLoaded())
			return true;

		next_image_ct = this.image_ct+1;
		if( next_image_ct >= this.images_cnt )
			next_image_ct = 0;

		if( this.imgs[ next_image_ct ] )
		{
			if( this.imgs[ next_image_ct ].complete )
				return true;
			return false;
		}

		this.imgs[ next_image_ct ] = new Image();
		this.imgs[ next_image_ct ].src = this.images[ next_image_ct ];
		if( this.imgs[ next_image_ct ].complete )
			return true;

		return false;
	}

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

		this.next_timeout = this.images_timeouts[ this.image_ct ];
	}

	this.ChangeImage = function()
	{
		var elem = document.getElementById( this.img_id );
		elem.src = this.imgs[ this.image_ct ].src;

		if( elem.filters )
			elem.filters.alpha.opacity = this.max_transition;
		elem.style.MozOpacity = this.max_transition / 100;
	}

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


