var TOUT_CAMBIO		= 5000 ;
var TOUT_FUNDIDO		= 50 ;
var RATIO_OPACIDAD	= 0.05 ;

var images_1 = 
	new Array(
		'img/intro/mod_1/01.jpg',
		'img/intro/mod_1/02.jpg',
		'img/intro/mod_1/03.jpg',
		'img/intro/mod_1/04.jpg',
		'img/intro/mod_1/05.jpg',
		'img/intro/mod_1/06.jpg',
		'img/intro/mod_1/07.jpg',
		'img/intro/mod_1/08.jpg',
		'img/intro/mod_1/09.jpg',
		'img/intro/mod_1/10.jpg',
		'img/intro/mod_1/11.jpg',
		'img/intro/mod_1/12.jpg',
		'img/intro/mod_1/13.jpg',
		'img/intro/mod_1/14.jpg',
		'img/intro/mod_1/15.jpg'
	);

var images_2 =
	new Array(
		'img/intro/mod_2/01.jpg',
		'img/intro/mod_2/02.jpg',
		'img/intro/mod_2/03.jpg',
		'img/intro/mod_2/04.jpg',
		'img/intro/mod_2/05.jpg',
		'img/intro/mod_2/06.jpg',
		'img/intro/mod_2/07.jpg',
		'img/intro/mod_2/08.jpg',
		'img/intro/mod_2/09.jpg',
		'img/intro/mod_2/10.jpg',
		'img/intro/mod_2/11.jpg',
		'img/intro/mod_2/12.jpg'
	);

var images_3 =
	new Array(
		'img/intro/mod_3/01.jpg',
		'img/intro/mod_3/02.jpg',
		'img/intro/mod_3/03.jpg',
		'img/intro/mod_3/04.jpg',
		'img/intro/mod_3/05.jpg',
		'img/intro/mod_3/06.jpg',
		'img/intro/mod_3/07.jpg',
		'img/intro/mod_3/08.jpg',
		'img/intro/mod_3/09.jpg'
	);

function cFrame( name, img_id, images ) 
{
	this.name	= name ;
	this.img_id	= img_id ;
	this.images = images ;

	this.img_1 ;
	this.img_2 ;
	this.cache ;

	this.actual		= 0 ;
	this.tout		= TOUT_FUNDIDO ;
	this.turno		= 0 ;
	this.op_x 		= RATIO_OPACIDAD ;

	this.op_1;
	this.op_2;
	this.sec ;

	this.cambio_turno = function()
	{
		this.turno ++ ;

		if ( this.turno >= this.images.length )
		{
			this.turno = 0 ;
		}
	}

	this.swap = function()
	{
		var tmp = this.img_1 ;
		this.img_1 = this.img_2 ;
		this.img_2 = tmp ;
	}

	this.cambio = function( sec )
	{
		this.sec = sec ;

		this.cambio_turno() ;

		this.cache		= new Image() ;
		this.cache.src = this.images[ this.turno ] ;

		this.wait_img() ;
	}

	this.wait_img = function()
	{
		if ( this.cache.complete )
		{
			this.swap() ;

			this.img_1.src = this.cache.src ;

			this.op_1 = 0 ;
			this.op_2 = 1 ;

			this.run() ;
		}
		else
		{
			setTimeout( name + '.wait_img()', 100 ) ;
		}
	}

	this.run = function()
	{
		setTimeout( name + '.fundir()', this.tout ) ;
	}

	this.fundir = function()
	{
		this.fundir_img( this.img_1, this.op_1 ) ;
		this.fundir_img( this.img_2, this.op_2 ) ;

		this.op_1 += this.op_x ;
		this.op_2 -= this.op_x ;

		if ( this.op_1 > 1 )
		{
			this.sec.run() ;
			return ;
		}

		this.run() ;
	}

	this.fundir_img = function( img, op )
	{
		img.style.opacity = op ;
	
		if ( img.filters )
		{
			img.filters.alpha.opacity = op * 100 ;
		}
	}
}

function cSecuenciadorImagenes( name, frames ) 
{
	this.name	= name ;
	this.frames	= frames ;
	this.tout	= TOUT_CAMBIO ;
	this.tout_f	= TOUT_FUNDIDO ;
	this.turno	= -1 ;

	this.iniciar = function( tout_cambio )
	{
		if ( tout_cambio != null )
		{
			this.tout = tout_cambio ;
		}

		for ( i= 0; i < this.frames.length; i++ )
		{
			this.frames[i].img_1 = 
				document.getElementById( this.frames[i].img_id + '_1' ) ;

			this.frames[i].img_2 = 
				document.getElementById( this.frames[i].img_id + '_2' ) ;
		}

		this.run() ;
	}

	this.run = function()
	{
		setTimeout( name + '.cambio()', this.tout ) ;
	}

	this.cambio = function()
	{
		this.turno ++ ;

		if ( this.turno >= this.frames.length )
		{
			this.turno = 0 ;
		}

		this.frames[ this.turno ].cambio( this ) ;
	}
}

var frames = 
	new Array(
		new cFrame( 'frames[0]', 'frame_1', images_1 ),
		new cFrame( 'frames[1]', 'frame_2', images_2 ),
		new cFrame( 'frames[2]', 'frame_3', images_3 )
	) ;

var secuenciador =
	new cSecuenciadorImagenes( 'secuenciador', frames ) ;

function intro_Init( tout_cambio )
{
	secuenciador.iniciar( tout_cambio * 1000 ) ;
}

