// Desarrollado por:
// Ignacio Pérez Terradillos
// Centro Internacional de Tecnologías Avanzadas
// Fundación Germán Sánchez Ruipérez
// 37300 Peñaranda de Bracamonte
// Salamanca (España)

function runTicker( divId ) {
	this.url = null;
	this.content = null;
	this.interval = 50;	// 100 ms
	this.step = 2;
	this.width = 400;
	this.height = 250;
	this.stopOnMouseOver = false;
	this.mouseOver = false;
	
	this.start = function() {
		object = this;

		if( !this.url && !this.content ) {
			alert( 'No se ha especificado una url o un contenido a cargar' );
			return null;
		}
		if( $('#'+divId).length == 0 ) {
			alert( 'No existe el contenedor con ID "' + divId + '"' );
			return null;
		}
		
		if( object.stopOnMouseOver ) {
			$('#'+divId).mouseover( function() { object.mouseOver = true; } );
			$('#'+divId).mouseout( function() { object.mouseOver = false } );
		}
		
		$('#'+divId).css( { overflow:'hidden', width:this.width+'px', height:this.height+'px', position:'relative' } );
		$('#'+divId).html( '<div></div><div></div>' );
		
		if( this.url ) $('#'+divId+'>div:first').load( this.url, function() { object.doScroll( object ); } );
		else {
			$('#'+divId+'>div:first').html( this.content );
			this.doScroll( this );
		}
	}
	
	this.doScroll = function( obj ) {
		$('#'+divId+'>div:first').css( { position:'absolute', top:obj.height + 'px', left:'0px' } );

		if( $('#'+divId+'>div:first').height() <= $('#'+divId).height() ) return;
		if( obj.url ) $('#'+divId+'>div:last').load( obj.url );
		else $('#'+divId+'>div:last').html( obj.content );

		lpos = parseInt( $('#'+divId+'>div:first').css( 'top' ) ) + $('#'+divId+'>div:first').height();
		$('#'+divId+'>div:last').css( { position:'absolute', top:lpos+'px', left:'0px' } );

		setTimeout( obj.moveUp, obj.interval, obj );
	}
	
	this.moveUp = function( obj ) {
		if( !obj.mouseOver ) {
			fpos = parseInt( $('#'+divId+'>div:first').css( 'top' ) ) - obj.step;
			lpos = parseInt( $('#'+divId+'>div:last').css( 'top' ) ) - obj.step;
			
			$('#'+divId+'>div:first').css( 'top', fpos+'px' );
			$('#'+divId+'>div:last').css( 'top', lpos+'px' );
			
			if( ( fpos + $('#'+divId+'>div:first').height() ) < 0 ) {
				$('#'+divId+'>div:first').css( 'top', lpos+parseInt( $('#'+divId+'>div:last').height() )+'px' );
			}
			else if( ( lpos + $('#'+divId+'>div:last').height() ) < 0 ) {
				$('#'+divId+'>div:last').css( 'top', fpos+parseInt( $('#'+divId+'>div:first').height() )+'px' );
			}
		}

		setTimeout( obj.moveUp, obj.interval, obj );
	}
}
