
function cImagenDeslizador( x, y, w, h )
{
	this.div			= document.createElement( 'div' ) ;
	this.img			= document.createElement( 'img' ) ;
	this.loading	= false ;

	this.div.appendChild( this.img ) ;
	
	this.setPos( x, y ) ;
	this.setSize( w, h ) ;
}

cImagenDeslizador.prototype.getElement = cImagenDeslizador_getElement ;
cImagenDeslizador.prototype.setPos = cImagenDeslizador_setPos ;
cImagenDeslizador.prototype.setSize = cImagenDeslizador_setSize ;
cImagenDeslizador.prototype.setImage = cImagenDeslizador_setImage ;
cImagenDeslizador.prototype.isCompleted = cImagenDeslizador_isCompleted ;

function cImagenDeslizador_getElement()
{
	return this.div ;
}

function cImagenDeslizador_setPos( x, y )
{
	if ( x != this.x )
	{
		this.div.style.left	= this.x = x ;
	}

	if ( y != this.y )
	{
		this.div.style.top	= this.y = y ;
	}
}

function cImagenDeslizador_setSize( w, h )
{
	this.div.width	 = this.w = w ;
	this.div.height = this.h = h ;
	this.r = w / h ;
}

function cImagenDeslizador_setImage( source ) 
{
	if ( source )
	{
		this.cache		= new Image() ;
		this.cache.src = source ;
		this.loading 	= true ;
	}
}

function cImagenDeslizador_isCompleted()
{
	if ( this.loading && this.cache.complete )
	{
		this.img.src = this.cache.src ;
		
		var cw = this.cache.width ;
		var ch = this.cache.height ;
		var cr = cw / ch ;

		if ( this.r > cr )
		{
			ch = this.h ;
			cw = Math.round( ch * cr ) ;
		}
		else
		{
			cw = this.w ;
			ch = Math.round( cw / cr ) ;
		}
		
		this.img.width			= cw ;
		this.img.height		= ch ;
		this.img.style.top 	= Math.round( ( this.h / 2 ) - ( ch / 2 ) ) ; 
		this.img.style.left 	= Math.round( ( this.w / 2 ) - ( cw / 2 ) ) ; 
		
		this.loading			= false ;
	}

	return this.cache.complete ;
}

function cDeslizador( id, source, w, h )
{
	this.id		= id ;
	this.source = source ;
	this.width	= w ;
	this.height	= h ;

	this.div ;
	this.img_from ;
	this.img_to ;

	this.img_progress ;

	this.time	= 30 ;
	this.inc		= 25 ;
	this.inc_x ;
	this.inc_y ;
	this.dec		= 0 ;

	this.running = false ;

	Iniciador.add( this.id + '.init()' ) ;
}

cDeslizador.prototype.init = cDeslizador_init ;
cDeslizador.prototype.start = cDeslizador_start ;
cDeslizador.prototype.scroll = cDeslizador_scroll ;

function cDeslizador_init()
{
	this.div		= document.getElementById( this.id ) ;

	this.div.style.width		= this.width ;
	this.div.style.height	= this.height ;

	this.img_from	= new cImagenDeslizador( 
									-this.width, 0, this.width, this.height 
								) ;

	this.img_to		= new cImagenDeslizador( 
									this.width, 0, this.width, this.height 
								) ;

	this.div.appendChild( this.img_from.getElement() ) ;
	this.div.appendChild( this.img_to.getElement() ) ;

	this.img_progress = document.createElement( 'div' ) ;

	this.img_progress.className = 'progress' ;
	this.img_progress.style.top = this.height / 2 - 50 ;
	this.img_progress.style.left = this.width / 2 - 50 ;

	this.div.appendChild( this.img_progress ) ;

	var img = document.createElement( 'img' ) ;

	img.src			= 'img/progress.gif' ;

	this.img_progress.appendChild( img ) ;
	
	this.start( 1, this.source ) ;
}

function cDeslizador_start( dir, source )
{
	if ( ! this.running )
	{
		this.img_to.setImage( source ) ;

		switch ( dir )
		{
			case 1 :
				this.img_to.setPos( this.width, 0 ) ;
				this.dec			= this.width / this.inc ;
				this.inc_x		= - this.inc ;
				this.inc_y		= 0 ;
			break ;

			case 2 :
				this.img_to.setPos( -this.width, 0 ) ;
				this.dec			= this.width / this.inc ;
				this.inc_x		= this.inc ;
				this.inc_y		= 0 ;
			break ;

			case 3 :
				this.img_to.setPos( 0, this.height ) ;
				this.dec			= this.height / this.inc ;
				this.inc_x		= 0 ;
				this.inc_y		= - this.inc ;
			break ;

			case 4 :
				this.img_to.setPos( 0, -this.height ) ;
				this.dec			= this.height / this.inc ;
				this.inc_x		= 0 ;
				this.inc_y		= this.inc ;
			break ;

			default:
				return ;
		}

		this.running = true ;

		this.scroll() ;
	}
}

function cDeslizador_scroll()
{
	if ( !this.img_to.isCompleted() )
	{
		this.img_progress.style.display = 'block' ;

		setTimeout( this.id + '.scroll()', this.time ) ;

		return ;
	}

	this.img_progress.style.display = 'none' ;

	this.img_from.setPos( 
		this.img_from.x + this.inc_x,
		this.img_from.y + this.inc_y
	) ;

	this.img_to.setPos( 
		this.img_to.x + this.inc_x,
		this.img_to.y + this.inc_y
	) ;

	if ( ( --this.dec ) > 0  )
	{
		setTimeout( this.id + '.scroll()', this.time ) ;
	}
	else
	{
		var tmp = this.img_from ;

		this.img_from = this.img_to ;
		this.img_to = tmp ;

		this.running = false ;
	}
}

